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: 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(), "No
:)
"); 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(),
`