diff --git a/src/HighlightPairedShortcode.js b/src/HighlightPairedShortcode.js index cb79a6c..1a689ee 100644 --- a/src/HighlightPairedShortcode.js +++ b/src/HighlightPairedShortcode.js @@ -13,7 +13,16 @@ module.exports = function (content, language, highlightNumbers, options = {}) { if( language === "text" ) { highlightedContent = content; } else { - highlightedContent = Prism.highlight(content, PrismLoader(language), language); + let loader = PrismLoader(language, options) + if( !loader ) { + if (options.ignoreInvalidLanguages == "md") { + return content; + } + + highlightedContent = content; + } else { + highlightedContent = Prism.highlight(content, loader, language); + } } let group = new HighlightLinesGroup(highlightNumbers); diff --git a/src/PrismLoader.js b/src/PrismLoader.js index a3515a2..78cf247 100644 --- a/src/PrismLoader.js +++ b/src/PrismLoader.js @@ -5,7 +5,7 @@ PrismLoader.silent = true; const PrismAlias = require("./PrismNormalizeAlias"); -module.exports = function(language) { +module.exports = function(language, options = {}) { let diffRemovedRawName = language; if(language.startsWith("diff-")) { diffRemovedRawName = language.substr("diff-".length); @@ -17,7 +17,11 @@ module.exports = function(language) { PrismLoader(aliasedName); } if(!Prism.languages[ aliasedName ]) { - throw new Error(`"${language}" is not a valid Prism.js language for eleventy-plugin-syntaxhighlight`); + if (options.ignoreInvalidLanguages) { + return null; + } else { + throw new Error(`"${language}" is not a valid Prism.js language for eleventy-plugin-syntaxhighlight`); + } } if(!language.startsWith("diff-")) { diff --git a/src/markdownSyntaxHighlightOptions.js b/src/markdownSyntaxHighlightOptions.js index befb7c2..59f2bc1 100644 --- a/src/markdownSyntaxHighlightOptions.js +++ b/src/markdownSyntaxHighlightOptions.js @@ -20,7 +20,16 @@ module.exports = function (options = {}) { if(language === "text") { html = str; } else { - html = Prism.highlight(str, PrismLoader(language), language); + let loader = PrismLoader(language, options) + if(!loader) { + if (options.ignoreInvalidLanguages == "md") { + return str; + } + + html = str; + } else { + html = Prism.highlight(str, loader, language); + } } let hasHighlightNumbers = split.length > 0; diff --git a/test/HighlightPairedShortcodeTest.js b/test/HighlightPairedShortcodeTest.js index 59c8b44..512c672 100644 --- a/test/HighlightPairedShortcodeTest.js +++ b/test/HighlightPairedShortcodeTest.js @@ -91,6 +91,13 @@ test("Test loader invalid language", async t => { }); }); +test("Test loader invalid language with ignore", async t => { + let src = `hello +hello` + t.is(await HighlightPairedShortcode(src, "asldkjflksdaj", "", { ignoreInvalidLanguages: "html" }), `
hello
hello
`);
+ t.is(await HighlightPairedShortcode(src, "asldkjflksdaj", "", { ignoreInvalidLanguages: "md" }), src);
+});
+
test("Trim content option (defaults true)", async t => {
t.is(await HighlightPairedShortcode(` alert();
alert(); `, "js", "", {}), `alert();
alert();
`);
diff --git a/test/MarkdownHighlightTest.js b/test/MarkdownHighlightTest.js
index 1dced51..1217f67 100644
--- a/test/MarkdownHighlightTest.js
+++ b/test/MarkdownHighlightTest.js
@@ -58,6 +58,36 @@ test("Test Nunjucks Alias", t => {
\`\`\``).trim(), `{% raw %}hello{% endraw %}
`);
});
+test("Test loader invalid language", t => {
+ let mdLib = md();
+ mdLib.set({
+ highlight: markdownPrismJsOptions()
+ });
+ t.throws(() => {
+ mdLib.render(`\`\`\`asldkjflksdaj
+hello
+\`\`\``);
+ });
+});
+
+test("Test loader invalid language with ignore", t => {
+ let src = `\`\`\`asldkjflksdaj
+hello
+\`\`\``;
+
+ let mdLib = md();
+ mdLib.set({
+ highlight: markdownPrismJsOptions({ ignoreInvalidLanguages: "html" })
+ });
+ t.is(mdLib.render(src).trim(), `hello
`);
+
+ mdLib = md();
+ mdLib.set({
+ highlight: markdownPrismJsOptions({ ignoreInvalidLanguages: "md" })
+ });
+ t.is(mdLib.render(src).trim(), `hello
+
`);
+});
// test("Test Markdown Highlighter Block Comment", t => {
// let mdLib = md();