Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass template context to the Markdown engine #643

Merged
merged 5 commits into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you drop the comment?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can tell the comment is not relevant anymore

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @MadeByMike mentioned, the comment says, "throw away data", but we're not throwing away the data any more.

return mdlib.render(str);
return function(data) {
return mdlib.render(str, data);
};
}
}
Expand Down
43 changes: 43 additions & 0 deletions test/TemplateRenderMarkdownPluginTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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;
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);
});

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);
});