Repository: 11ty/eleventy
Pull request #643 (2019-08-06 05:41:42)

Pass template context to the Markdown engine
Good morning,

The project I am using eleventy.js in makes extensive use of markdown-it plugins to customise the behaviour, and add special handlers. It would be extremely useful for those plugins to have access to the context of the template they are processing. Markdown-it allows an external context to be passed to its `render`method, so all that is missing is for eleventy to actually pass that context. Turns out, that context is available right there - it just needs to be used.

So this is a very small pull request, with a minor change, and two tests to check the functionality. I have added the tests in their own file - I wasn't sure if that was correct, or if you would want me to add them to an existing file. 

Thanks


Patch:
diff --git a/src/Engines/Markdown.js b/src/Engines/Markdown.js
index f064c5f2..8bb5db65 100644
--- a/src/Engines/Markdown.js
+++ b/src/Engines/Markdown.js
@@ -69,7 +69,7 @@ class Markdown extends TemplateEngine {
       } else {
         return async function(data) {
           let preTemplateEngineRender = await fn(data);
-          let finishedRender = mdlib.render(preTemplateEngineRender);
+          let finishedRender = mdlib.render(preTemplateEngineRender, data);
           return finishedRender;
         };
       }
@@ -79,9 +79,8 @@ class Markdown extends TemplateEngine {
           return str;
         };
       } else {
-        return function() {
-          // throw away data if preTemplateEngine is falsy
-          return mdlib.render(str);
+        return function(data) {
+          return mdlib.render(str, data);
         };
       }
     }


Test patch:
diff --git a/test/TemplateRenderMarkdownPluginTest.js b/test/TemplateRenderMarkdownPluginTest.js
new file mode 100644
index 00000000..74918a3e
--- /dev/null
+++ b/test/TemplateRenderMarkdownPluginTest.js
@@ -0,0 +1,47 @@
+import test from "ava";
+import TemplateRender from "../src/TemplateRender";
+import md from "markdown-it";
+
+const createTestMarkdownPlugin = () => {
+  const plugin = md => {
+    md.core.ruler.after("inline", "replace-link", function(state) {
+      plugin.environment = state.env;
+      const link = state.tokens[1].children[0].attrs[0][1];
+      state.tokens[1].children[0].attrs[0][1] = `${link}?data=${state.env.some}`;
+      return false;
+    });
+  };
+  plugin.environment = {};
+  return plugin;
+};
+
+test("Markdown Render: with HTML prerender, sends context data to the markdown library", async t => {
+  let tr = new TemplateRender("md");
+
+  const plugin = createTestMarkdownPlugin();
+  let mdLib = md().use(plugin);
+  tr.engine.setLibrary(mdLib);
+
+  const data = { some: "data" };
+
+  let fn = await tr.getCompiledTemplate("[link text](http://link.com)");
+  let result = await fn(data);
+  t.deepEqual(plugin.environment, data);
+  t.is(result, '<p><a href="http://link.com?data=data">link text</a></p>\n');
+});
+
+test("Markdown Render: without HTML prerender, sends context data to the markdown library", async t => {
+  let tr = new TemplateRender("md");
+
+  const plugin = createTestMarkdownPlugin();
+  let mdLib = md().use(plugin);
+  tr.engine.setLibrary(mdLib);
+  tr.setHtmlEngine(false);
+
+  const data = { some: "data" };
+
+  let fn = await tr.getCompiledTemplate("[link text](http://link.com)");
+  let result = await fn(data);
+  t.deepEqual(plugin.environment, data);
+  t.is(result, '<p><a href="http://link.com?data=data">link text</a></p>\n');
+});