Skip to content

Commit

Permalink
Fixes #2438. Adds new amendLibrary configuration API method to alte…
Browse files Browse the repository at this point in the history
…r both default or custom library instances (set via setLibrary)
  • Loading branch information
zachleat committed Jun 16, 2022
1 parent 682d016 commit 1bdbd24
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/Engines/Markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class Markdown extends TemplateEngine {
});
}

// Disable indented code blocks by default (Issue #2438)
this.mdLib.disable("code");

this.setEngineLib(this.mdLib);
}

Expand Down
5 changes: 5 additions & 0 deletions src/Engines/TemplateEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ class TemplateEngine {

setEngineLib(engineLib) {
this.engineLib = engineLib;

// Run engine amendments (via issue #2438)
for (let amendment of this.config.libraryAmendments[this.name] || []) {
amendment(engineLib);
}
}

getEngineLib() {
Expand Down
13 changes: 13 additions & 0 deletions src/UserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class UserConfig {

this.useTemplateCache = true;
this.dataFilterSelectors = new Set();

this.libraryAmendments = {};
}

versionCheck(expected) {
Expand Down Expand Up @@ -460,6 +462,16 @@ class UserConfig {
this.libraryOverrides[engineName.toLowerCase()] = libraryInstance;
}

/* These callbacks run on both libraryOverrides and default library instances */
amendLibrary(engineName, callback) {
let name = engineName.toLowerCase();
if (!this.libraryAmendments[name]) {
this.libraryAmendments[name] = [];
}

this.libraryAmendments[name].push(callback);
}

setPugOptions(options) {
this.pugOptions = options;
}
Expand Down Expand Up @@ -838,6 +850,7 @@ class UserConfig {
useTemplateCache: this.useTemplateCache,
precompiledCollections: this.precompiledCollections,
dataFilterSelectors: this.dataFilterSelectors,
libraryAmendments: this.libraryAmendments,
};
}
}
Expand Down
63 changes: 57 additions & 6 deletions test/TemplateRenderMarkdownTest.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
const test = require("ava");
const TemplateRender = require("../src/TemplateRender");
const TemplateConfig = require("../src/TemplateConfig");
const EleventyExtensionMap = require("../src/EleventyExtensionMap");
const md = require("markdown-it");
const mdEmoji = require("markdown-it-emoji");
const UserConfig = require("../src/UserConfig");
const eleventySyntaxHighlightPlugin = require("@11ty/eleventy-plugin-syntaxhighlight");

function getNewTemplateRender(name, inputDir) {
let eleventyConfig = new TemplateConfig();
const TemplateRender = require("../src/TemplateRender");
const TemplateConfig = require("../src/TemplateConfig");
const EleventyExtensionMap = require("../src/EleventyExtensionMap");
const normalizeNewLines = require("./Util/normalizeNewLines");

function getNewTemplateRender(name, inputDir, eleventyConfig) {
if (!eleventyConfig) {
eleventyConfig = new TemplateConfig();
}

let tr = new TemplateRender(name, inputDir, eleventyConfig);
tr.extensionMap = new EleventyExtensionMap([], eleventyConfig);
return tr;
Expand Down Expand Up @@ -349,3 +353,50 @@ test("Markdown Render: use Markdown inside of a Nunjucks paired shortcode (Issue
<h2>My Other Title</h2>`
);
});

test("Markdown Render: Disable indented code blocks by default. Issue #2438", async (t) => {
let fn = await getNewTemplateRender("md").getCompiledTemplate(
" This is a test"
);
t.is((await fn()).trim(), "<p>This is a test</p>");
});

test("Markdown Render: setLibrary does not have disabled indented code blocks either. Issue #2438", async (t) => {
let tr = getNewTemplateRender("md");

let mdLib = md();
tr.engine.setLibrary(mdLib);

let fn = await tr.getCompiledTemplate(" This is a test");
let content = await fn();
t.is(content.trim(), "<p>This is a test</p>");
});

test("Markdown Render: use amendLibrary to re-enable indented code blocks. Issue #2438", async (t) => {
let eleventyConfig = new TemplateConfig();
let userConfig = eleventyConfig.userConfig;
userConfig.amendLibrary("md", (lib) => lib.enable("code"));

let tr = getNewTemplateRender("md", null, eleventyConfig);

let mdLib = md();
tr.engine.setLibrary(mdLib);

let fn = await tr.getCompiledTemplate(" This is a test");
let content = await fn();
t.is(
normalizeNewLines(content.trim()),
`<pre><code>This is a test
</code></pre>`
);
});

test("Markdown Render: use amendLibrary to add a Plugin", async (t) => {
let eleventyConfig = new TemplateConfig();
let userConfig = eleventyConfig.userConfig;
userConfig.amendLibrary("md", (mdLib) => mdLib.use(mdEmoji));

let tr = getNewTemplateRender("md", null, eleventyConfig);
let fn = await tr.getCompiledTemplate(":)");
t.is((await fn()).trim(), "<p>😃</p>");
});

0 comments on commit 1bdbd24

Please sign in to comment.