From dbd36899654ff7e5a80f784c0c3122620a2955fe Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Tue, 28 Jun 2022 14:59:10 -0500 Subject: [PATCH] Fixes #68 --- src/PrismNormalizeAlias.js | 10 ++++++++++ test/LiquidHighlightTagTest.js | 18 ++++++++++++++++++ test/MarkdownHighlightTest.js | 20 ++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/src/PrismNormalizeAlias.js b/src/PrismNormalizeAlias.js index 2dba6ee..40e9bb3 100644 --- a/src/PrismNormalizeAlias.js +++ b/src/PrismNormalizeAlias.js @@ -1,5 +1,10 @@ const Prism = require("prismjs"); +const HARDCODED_ALIASES = { + njk: "jinja2", + nunjucks: "jinja2", +}; + // This was added to make `ts` resolve to `typescript` correctly. // The Prism loader doesn’t seem to always handle aliasing correctly. module.exports = function(language) { @@ -9,6 +14,11 @@ module.exports = function(language) { const PrismComponents = require("prismjs/components.json"); let langs = PrismComponents.languages; + // Manual override + if(HARDCODED_ALIASES[language]) { + language = HARDCODED_ALIASES[language]; + } + if(langs[ language ]) { return language; } diff --git a/test/LiquidHighlightTagTest.js b/test/LiquidHighlightTagTest.js index 9cbe036..8d6eaa4 100644 --- a/test/LiquidHighlightTagTest.js +++ b/test/LiquidHighlightTagTest.js @@ -23,3 +23,21 @@ test("Test Highlight Tag Render", async t => { let rendered = await renderLiquid("{% highlight js %}var test;{% endhighlight %}", {}, engine); t.is(`
var test;
`, rendered); }); + +test("Njk Alias", async t => { + let engine = new Liquid(); + let tag = new LiquidHighlightTag(engine); + engine.registerTag("highlight", tag.getObject()); + + let rendered = await renderLiquid("{% highlight njk %}{% raw %}hello{% endraw %}{% endhighlight %}", {}, engine); + t.is(`
{% raw %}hello{% endraw %}
`, rendered); +}); + +test("Nunjucks alias", async t => { + let engine = new Liquid(); + let tag = new LiquidHighlightTag(engine); + engine.registerTag("highlight", tag.getObject()); + + let rendered = await renderLiquid("{% highlight nunjucks %}{% raw %}hello{% endraw %}{% endhighlight %}", {}, engine); + t.is(`
{% raw %}hello{% endraw %}
`, rendered); +}); diff --git a/test/MarkdownHighlightTest.js b/test/MarkdownHighlightTest.js index 6ac1c52..1dced51 100644 --- a/test/MarkdownHighlightTest.js +++ b/test/MarkdownHighlightTest.js @@ -38,6 +38,26 @@ alert(); \`\`\``).trim(), `
alert();
`); }); +test("Test Njk Alias", t => { + let mdLib = md(); + mdLib.set({ + highlight: markdownPrismJsOptions() + }); + t.is(mdLib.render(`\`\`\`njk +{% raw %}hello{% endraw %} +\`\`\``).trim(), `
{% raw %}hello{% endraw %}
`); +}); + +test("Test Nunjucks Alias", t => { + let mdLib = md(); + mdLib.set({ + highlight: markdownPrismJsOptions() + }); + t.is(mdLib.render(`\`\`\`nunjucks +{% raw %}hello{% endraw %} +\`\`\``).trim(), `
{% raw %}hello{% endraw %}
`); +}); + // test("Test Markdown Highlighter Block Comment", t => { // let mdLib = md();