import test from "ava"; import TemplateRender from "../src/TemplateRender"; import md from "markdown-it"; import mdEmoji from "markdown-it-emoji"; import UserConfig from "../src/UserConfig"; import eleventySyntaxHighlightPlugin from "@11ty/eleventy-plugin-syntaxhighlight"; // Markdown test("Markdown", t => { t.is(new TemplateRender("md").getEngineName(), "md"); }); test("Markdown Render: Parses base markdown, no data", async t => { let fn = await new TemplateRender("md").getCompiledTemplate("# My Title"); t.is((await fn()).trim(), "

My Title

"); }); test("Markdown Render: Markdown should work with HTML too", async t => { let fn = await new TemplateRender("md").getCompiledTemplate( "

My Title

" ); t.is((await fn()).trim(), "

My Title

"); }); test("Markdown Render: Parses markdown using liquid engine (default, with data)", async t => { let fn = await new TemplateRender("md").getCompiledTemplate("# {{title}}"); t.is((await fn({ title: "My Title" })).trim(), "

My Title

"); }); test("Markdown Render: Parses markdown using ejs engine", async t => { let tr = new TemplateRender("md"); tr.setMarkdownEngine("ejs"); let fn = await tr.getCompiledTemplate("<%=title %>"); t.is((await fn({ title: "My Title" })).trim(), "

My Title

"); }); test("Markdown Render: Ignore markdown, use only preprocess engine (useful for variable resolution in permalinks)", async t => { let tr = new TemplateRender("md"); tr.setUseMarkdown(false); let fn = await tr.getCompiledTemplate("{{title}}"); t.is((await fn({ title: "My Title" })).trim(), "My Title"); }); test("Markdown Render: Skip markdown and preprocess engine (issue #466)", async t => { let tr = new TemplateRender("md"); tr.setMarkdownEngine(false); tr.setUseMarkdown(false); let fn = await tr.getCompiledTemplate("404.html"); t.is((await fn({ title: "My Title" })).trim(), "404.html"); }); test("Markdown Render: Set markdown engine to false, don’t parse", async t => { let tr = new TemplateRender("md"); tr.setMarkdownEngine(false); let fn = await tr.getCompiledTemplate("# {{title}}"); t.is((await fn()).trim(), "

{{title}}

"); }); test("Markdown Render: Set markdown engine to false, don’t parse (test with HTML input)", async t => { let tr = new TemplateRender("md"); tr.setMarkdownEngine(false); let fn = await tr.getCompiledTemplate("

{{title}}

"); t.is((await fn()).trim(), "

{{title}}

"); }); test("Markdown Render: Pass in engine override (ejs)", async t => { let tr = new TemplateRender("md"); tr.setMarkdownEngine("ejs"); let fn = await tr.getCompiledTemplate("# <%= title %>"); t.is((await fn({ title: "My Title" })).trim(), "

My Title

"); }); test("Markdown Render: Pass in an override (liquid)", async t => { let tr = new TemplateRender("md"); tr.setMarkdownEngine("liquid"); let fn = await tr.getCompiledTemplate("# {{title}}"); t.is((await fn({ title: "My Title" })).trim(), "

My Title

"); }); test("Markdown Render: Strikethrough", async t => { let fn = await new TemplateRender("md").getCompiledTemplate("~~No~~"); t.is((await fn()).trim(), "

No

"); }); test("Markdown Render: Strikethrough in a Header", async t => { let fn = await new TemplateRender("md").getCompiledTemplate("# ~~No~~"); t.is((await fn()).trim(), "

No

"); }); test("Markdown Render: with Library Override", async t => { let tr = new TemplateRender("md"); let mdLib = md(); tr.engine.setLibrary(mdLib); t.is(mdLib.render(":)").trim(), "

:)

"); let fn = await tr.getCompiledTemplate(":)"); t.is((await fn()).trim(), "

:)

"); }); test("Markdown Render: with Library Override and a Plugin", async t => { let tr = new TemplateRender("md"); let mdLib = md().use(mdEmoji); tr.engine.setLibrary(mdLib); t.is(mdLib.render(":)").trim(), "

😃

"); let fn = await tr.getCompiledTemplate(":)"); t.is((await fn()).trim(), "

😃

"); }); test("Markdown Render: use a custom highlighter", async t => { let tr = new TemplateRender("md"); let mdLib = md(); mdLib.set({ highlight: function(str, lang) { return "This is overrrrrrride"; } }); tr.engine.setLibrary(mdLib); let fn = await tr.getCompiledTemplate(`\`\`\` This is some code. \`\`\``); t.is((await fn()).trim(), "
This is overrrrrrride
"); }); test("Markdown Render: use prism highlighter (no language)", async t => { let tr = new TemplateRender("md"); let userConfig = new UserConfig(); userConfig.addPlugin(eleventySyntaxHighlightPlugin); let markdownHighlight = userConfig.getMergingConfigObject() .markdownHighlighter; let mdLib = md(); mdLib.set({ highlight: markdownHighlight }); tr.engine.setLibrary(mdLib); let fn = await tr.getCompiledTemplate(`\`\`\` This is some code. \`\`\``); t.is( (await fn()).trim(), `
This is some code.
` ); }); test("Markdown Render: use prism highlighter", async t => { let tr = new TemplateRender("md"); let userConfig = new UserConfig(); userConfig.addPlugin(eleventySyntaxHighlightPlugin); let markdownHighlight = userConfig.getMergingConfigObject() .markdownHighlighter; let mdLib = md(); mdLib.set({ highlight: markdownHighlight }); tr.engine.setLibrary(mdLib); let fn = await tr.getCompiledTemplate(`\`\`\` js var key = "value"; \`\`\``); t.is( (await fn()).trim(), `
var key = "value";
` ); }); test("Markdown Render: use prism highlighter (no space before language)", async t => { let tr = new TemplateRender("md"); let userConfig = new UserConfig(); userConfig.addPlugin(eleventySyntaxHighlightPlugin); let markdownHighlight = userConfig.getMergingConfigObject() .markdownHighlighter; let mdLib = md(); mdLib.set({ highlight: markdownHighlight }); tr.engine.setLibrary(mdLib); let fn = await tr.getCompiledTemplate(`\`\`\`js var key = "value"; \`\`\``); t.is( (await fn()).trim(), `
var key = "value";
` ); }); test("Markdown Render: use prism highlighter, line highlighting", async t => { let tr = new TemplateRender("md"); let userConfig = new UserConfig(); userConfig.addPlugin(eleventySyntaxHighlightPlugin); let markdownHighlight = userConfig.getMergingConfigObject() .markdownHighlighter; let mdLib = md(); mdLib.set({ highlight: markdownHighlight }); tr.engine.setLibrary(mdLib); let fn = await tr.getCompiledTemplate(`\`\`\`js/0 var key = "value"; \`\`\``); t.is( (await fn()).trim(), `
var key = "value";
` ); }); test("Markdown Render: use prism highlighter, line highlighting with fallback `text` language.", async t => { let tr = new TemplateRender("md"); let userConfig = new UserConfig(); userConfig.addPlugin(eleventySyntaxHighlightPlugin); let markdownHighlight = userConfig.getMergingConfigObject() .markdownHighlighter; let mdLib = md(); mdLib.set({ highlight: markdownHighlight }); tr.engine.setLibrary(mdLib); let fn = await tr.getCompiledTemplate(`\`\`\` text/0 var key = "value"; \`\`\``); t.is( (await fn()).trim(), `
var key = "value";
` ); }); test("Markdown Render: use Markdown inside of a Liquid shortcode (Issue #536)", async t => { let tr = new TemplateRender("md"); let cls = require("../src/Engines/Liquid"); let liquidEngine = new cls("liquid", tr.getIncludesDir()); liquidEngine.addShortcode("testShortcode", function() { return "## My Other Title"; }); tr.setMarkdownEngine(liquidEngine); let fn = await tr.getCompiledTemplate(`# {{title}} {% testShortcode %}`); t.is( (await fn({ title: "My Title", otherTitle: "My Other Title" })).trim(), `

My Title

My Other Title

` ); }); test("Markdown Render: use Markdown inside of a Nunjucks shortcode (Issue #536)", async t => { let tr = new TemplateRender("md"); let cls = require("../src/Engines/Nunjucks"); let nunjucksEngine = new cls("njk", tr.getIncludesDir()); nunjucksEngine.addShortcode("testShortcode", function() { return "## My Other Title"; }); tr.setMarkdownEngine(nunjucksEngine); let fn = await tr.getCompiledTemplate(`# {{title}} {% testShortcode %}`); t.is( (await fn({ title: "My Title", otherTitle: "My Other Title" })).trim(), `

My Title

My Other Title

` ); }); test("Markdown Render: use Markdown inside of a Liquid paired shortcode (Issue #536)", async t => { let tr = new TemplateRender("md"); let cls = require("../src/Engines/Liquid"); let liquidEngine = new cls("liquid", tr.getIncludesDir()); liquidEngine.addPairedShortcode("testShortcode", function(content) { return content; }); tr.setMarkdownEngine(liquidEngine); let fn = await tr.getCompiledTemplate(`# {{title}} {% testShortcode %}## My Other Title{% endtestShortcode %}`); t.is( (await fn({ title: "My Title", otherTitle: "My Other Title" })).trim(), `

My Title

My Other Title

` ); }); test("Markdown Render: use Markdown inside of a Nunjucks paired shortcode (Issue #536)", async t => { let tr = new TemplateRender("md"); let cls = require("../src/Engines/Nunjucks"); let nunjucksEngine = new cls("njk", tr.getIncludesDir()); nunjucksEngine.addPairedShortcode("testShortcode", function(content) { return content; }); tr.setMarkdownEngine(nunjucksEngine); let fn = await tr.getCompiledTemplate(`# {{title}} {% testShortcode %}## My Other Title{% endtestShortcode %}`); t.is( (await fn({ title: "My Title", otherTitle: "My Other Title" })).trim(), `

My Title

My Other Title

` ); });