Skip to content

Commit a7afa9b

Browse files
authored
Merge pull request #73 from yehudab/master
Add an option to ignore invalid languages. Fixes #70
2 parents f40329e + 61809e3 commit a7afa9b

File tree

5 files changed

+63
-4
lines changed

5 files changed

+63
-4
lines changed

src/HighlightPairedShortcode.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ module.exports = function (content, language, highlightNumbers, options = {}) {
1313
if( language === "text" ) {
1414
highlightedContent = content;
1515
} else {
16-
highlightedContent = Prism.highlight(content, PrismLoader(language), language);
16+
let loader = PrismLoader(language, options)
17+
if( !loader ) {
18+
if (options.ignoreInvalidLanguages == "md") {
19+
return content;
20+
}
21+
22+
highlightedContent = content;
23+
} else {
24+
highlightedContent = Prism.highlight(content, loader, language);
25+
}
1726
}
1827

1928
let group = new HighlightLinesGroup(highlightNumbers);

src/PrismLoader.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ PrismLoader.silent = true;
55

66
const PrismAlias = require("./PrismNormalizeAlias");
77

8-
module.exports = function(language) {
8+
module.exports = function(language, options = {}) {
99
let diffRemovedRawName = language;
1010
if(language.startsWith("diff-")) {
1111
diffRemovedRawName = language.substr("diff-".length);
@@ -17,7 +17,11 @@ module.exports = function(language) {
1717
PrismLoader(aliasedName);
1818
}
1919
if(!Prism.languages[ aliasedName ]) {
20-
throw new Error(`"${language}" is not a valid Prism.js language for eleventy-plugin-syntaxhighlight`);
20+
if (options.ignoreInvalidLanguages) {
21+
return null;
22+
} else {
23+
throw new Error(`"${language}" is not a valid Prism.js language for eleventy-plugin-syntaxhighlight`);
24+
}
2125
}
2226

2327
if(!language.startsWith("diff-")) {

src/markdownSyntaxHighlightOptions.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@ module.exports = function (options = {}) {
2020
if(language === "text") {
2121
html = str;
2222
} else {
23-
html = Prism.highlight(str, PrismLoader(language), language);
23+
let loader = PrismLoader(language, options)
24+
if(!loader) {
25+
if (options.ignoreInvalidLanguages == "md") {
26+
return str;
27+
}
28+
29+
html = str;
30+
} else {
31+
html = Prism.highlight(str, loader, language);
32+
}
2433
}
2534

2635
let hasHighlightNumbers = split.length > 0;

test/HighlightPairedShortcodeTest.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ test("Test loader invalid language", async t => {
9191
});
9292
});
9393

94+
test("Test loader invalid language with ignore", async t => {
95+
let src = `hello
96+
hello`
97+
t.is(await HighlightPairedShortcode(src, "asldkjflksdaj", "", { ignoreInvalidLanguages: "html" }), `<pre class="language-asldkjflksdaj"><code class="language-asldkjflksdaj">hello<br>hello</code></pre>`);
98+
t.is(await HighlightPairedShortcode(src, "asldkjflksdaj", "", { ignoreInvalidLanguages: "md" }), src);
99+
});
100+
94101
test("Trim content option (defaults true)", async t => {
95102
t.is(await HighlightPairedShortcode(` alert();
96103
alert(); `, "js", "", {}), `<pre class="language-js"><code class="language-js"><span class="token function">alert</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><br><span class="token function">alert</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>`);

test/MarkdownHighlightTest.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,36 @@ test("Test Nunjucks Alias", t => {
5858
\`\`\``).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>`);
5959
});
6060

61+
test("Test loader invalid language", t => {
62+
let mdLib = md();
63+
mdLib.set({
64+
highlight: markdownPrismJsOptions()
65+
});
66+
t.throws(() => {
67+
mdLib.render(`\`\`\`asldkjflksdaj
68+
hello
69+
\`\`\``);
70+
});
71+
});
72+
73+
test("Test loader invalid language with ignore", t => {
74+
let src = `\`\`\`asldkjflksdaj
75+
hello
76+
\`\`\``;
77+
78+
let mdLib = md();
79+
mdLib.set({
80+
highlight: markdownPrismJsOptions({ ignoreInvalidLanguages: "html" })
81+
});
82+
t.is(mdLib.render(src).trim(), `<pre class="language-asldkjflksdaj"><code class="language-asldkjflksdaj">hello</code></pre>`);
83+
84+
mdLib = md();
85+
mdLib.set({
86+
highlight: markdownPrismJsOptions({ ignoreInvalidLanguages: "md" })
87+
});
88+
t.is(mdLib.render(src).trim(), `<pre><code class="language-asldkjflksdaj">hello
89+
</code></pre>`);
90+
});
6191

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

0 commit comments

Comments
 (0)