Skip to content

Commit

Permalink
Merge pull request #643 from blacksquaresa/master
Browse files Browse the repository at this point in the history
Pass template context to the Markdown engine
  • Loading branch information
zachleat authored Jan 23, 2020
2 parents 7843929 + a689764 commit 0c130eb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/Engines/Markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
Expand All @@ -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);
};
}
}
Expand Down
47 changes: 47 additions & 0 deletions test/TemplateRenderMarkdownPluginTest.js
Original file line number Diff line number Diff line change
@@ -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');
});

0 comments on commit 0c130eb

Please sign in to comment.