Skip to content

Commit

Permalink
Fixes #68
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jun 28, 2022
1 parent 3b8e3a7 commit dbd3689
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/PrismNormalizeAlias.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
}
Expand Down
18 changes: 18 additions & 0 deletions test/LiquidHighlightTagTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,21 @@ test("Test Highlight Tag Render", async t => {
let rendered = await renderLiquid("{% highlight js %}var test;{% endhighlight %}", {}, engine);
t.is(`<pre class="language-js"><code class="language-js"><span class="token keyword">var</span> test<span class="token punctuation">;</span></code></pre>`, 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(`<pre class="language-njk"><code class="language-njk"><span class="token delimiter punctuation">{%</span> <span class="token tag keyword">raw</span> <span class="token operator">%</span><span class="token punctuation">}</span><span class="token variable">hello</span><span class="token punctuation">{</span><span class="token operator">%</span> <span class="token variable">endraw</span> <span class="token delimiter punctuation">%}</span></code></pre>`, 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(`<pre class="language-nunjucks"><code class="language-nunjucks"><span class="token delimiter punctuation">{%</span> <span class="token tag keyword">raw</span> <span class="token operator">%</span><span class="token punctuation">}</span><span class="token variable">hello</span><span class="token punctuation">{</span><span class="token operator">%</span> <span class="token variable">endraw</span> <span class="token delimiter punctuation">%}</span></code></pre>`, rendered);
});
20 changes: 20 additions & 0 deletions test/MarkdownHighlightTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ alert();
\`\`\``).trim(), `<pre class="not-a-lang-js"><code class="language-js"><span class="highlight-line"><span class="token function">alert</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span></code></pre>`);
});

test("Test Njk Alias", t => {
let mdLib = md();
mdLib.set({
highlight: markdownPrismJsOptions()
});
t.is(mdLib.render(`\`\`\`njk
{% raw %}hello{% endraw %}
\`\`\``).trim(), `<pre class="language-njk"><code class="language-njk"><span class="token delimiter punctuation">{%</span> <span class="token tag keyword">raw</span> <span class="token operator">%</span><span class="token punctuation">}</span><span class="token variable">hello</span><span class="token punctuation">{</span><span class="token operator">%</span> <span class="token variable">endraw</span> <span class="token operator">%</span><span class="token punctuation">}</span></code></pre>`);
});

test("Test Nunjucks Alias", t => {
let mdLib = md();
mdLib.set({
highlight: markdownPrismJsOptions()
});
t.is(mdLib.render(`\`\`\`nunjucks
{% raw %}hello{% endraw %}
\`\`\``).trim(), `<pre class="language-nunjucks"><code class="language-nunjucks"><span class="token delimiter punctuation">{%</span> <span class="token tag keyword">raw</span> <span class="token operator">%</span><span class="token punctuation">}</span><span class="token variable">hello</span><span class="token punctuation">{</span><span class="token operator">%</span> <span class="token variable">endraw</span> <span class="token operator">%</span><span class="token punctuation">}</span></code></pre>`);
});


// test("Test Markdown Highlighter Block Comment", t => {
// let mdLib = md();
Expand Down

0 comments on commit dbd3689

Please sign in to comment.