Skip to content

Commit dbd3689

Browse files
committed
Fixes #68
1 parent 3b8e3a7 commit dbd3689

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/PrismNormalizeAlias.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
const Prism = require("prismjs");
22

3+
const HARDCODED_ALIASES = {
4+
njk: "jinja2",
5+
nunjucks: "jinja2",
6+
};
7+
38
// This was added to make `ts` resolve to `typescript` correctly.
49
// The Prism loader doesn’t seem to always handle aliasing correctly.
510
module.exports = function(language) {
@@ -9,6 +14,11 @@ module.exports = function(language) {
914
const PrismComponents = require("prismjs/components.json");
1015
let langs = PrismComponents.languages;
1116

17+
// Manual override
18+
if(HARDCODED_ALIASES[language]) {
19+
language = HARDCODED_ALIASES[language];
20+
}
21+
1222
if(langs[ language ]) {
1323
return language;
1424
}

test/LiquidHighlightTagTest.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,21 @@ test("Test Highlight Tag Render", async t => {
2323
let rendered = await renderLiquid("{% highlight js %}var test;{% endhighlight %}", {}, engine);
2424
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);
2525
});
26+
27+
test("Njk Alias", async t => {
28+
let engine = new Liquid();
29+
let tag = new LiquidHighlightTag(engine);
30+
engine.registerTag("highlight", tag.getObject());
31+
32+
let rendered = await renderLiquid("{% highlight njk %}{% raw %}hello{% endraw %}{% endhighlight %}", {}, engine);
33+
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);
34+
});
35+
36+
test("Nunjucks alias", async t => {
37+
let engine = new Liquid();
38+
let tag = new LiquidHighlightTag(engine);
39+
engine.registerTag("highlight", tag.getObject());
40+
41+
let rendered = await renderLiquid("{% highlight nunjucks %}{% raw %}hello{% endraw %}{% endhighlight %}", {}, engine);
42+
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);
43+
});

test/MarkdownHighlightTest.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@ alert();
3838
\`\`\``).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>`);
3939
});
4040

41+
test("Test Njk Alias", t => {
42+
let mdLib = md();
43+
mdLib.set({
44+
highlight: markdownPrismJsOptions()
45+
});
46+
t.is(mdLib.render(`\`\`\`njk
47+
{% raw %}hello{% endraw %}
48+
\`\`\``).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>`);
49+
});
50+
51+
test("Test Nunjucks Alias", t => {
52+
let mdLib = md();
53+
mdLib.set({
54+
highlight: markdownPrismJsOptions()
55+
});
56+
t.is(mdLib.render(`\`\`\`nunjucks
57+
{% raw %}hello{% endraw %}
58+
\`\`\``).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>`);
59+
});
60+
4161

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

0 commit comments

Comments
 (0)