Skip to content

Commit

Permalink
Adds errorOnInvalidLanguage which is defaulted to false. #73
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Apr 12, 2023
1 parent a7afa9b commit 2159800
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ module.exports = {
}

options = Object.assign({
errorOnInvalidLanguage: false,
alwaysWrapLineHighlights: false,
// eligible to change the default to \n in a new major version.
lineSeparator: "<br>",
preAttributes: {},
codeAttributes: {}
}, options);

// TODO hbs?
if( hasTemplateFormat(options.templateFormats, "liquid") ) {
eleventyConfig.addLiquidTag("highlight", (liquidEngine) => {
// {% highlight js 0 2 %}
Expand Down
8 changes: 2 additions & 6 deletions src/HighlightPairedShortcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ module.exports = function (content, language, highlightNumbers, options = {}) {
if( language === "text" ) {
highlightedContent = content;
} else {
let loader = PrismLoader(language, options)
if( !loader ) {
if (options.ignoreInvalidLanguages == "md") {
return content;
}

let loader = PrismLoader(language, options);
if(!loader) {
highlightedContent = content;
} else {
highlightedContent = Prism.highlight(content, loader, language);
Expand Down
9 changes: 3 additions & 6 deletions src/PrismLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ module.exports = function(language, options = {}) {
if(!Prism.languages[ aliasedName ]) {
PrismLoader(aliasedName);
}
if(!Prism.languages[ aliasedName ]) {
if (options.ignoreInvalidLanguages) {
return null;
} else {
throw new Error(`"${language}" is not a valid Prism.js language for eleventy-plugin-syntaxhighlight`);
}

if(options.errorOnInvalidLanguage && !Prism.languages[ aliasedName ]) {
throw new Error(`"${language}" is not a valid Prism.js language for eleventy-plugin-syntaxhighlight`);
}

if(!language.startsWith("diff-")) {
Expand Down
4 changes: 0 additions & 4 deletions src/markdownSyntaxHighlightOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ module.exports = function (options = {}) {
} else {
let loader = PrismLoader(language, options)
if(!loader) {
if (options.ignoreInvalidLanguages == "md") {
return str;
}

html = str;
} else {
html = Prism.highlight(str, loader, language);
Expand Down
23 changes: 14 additions & 9 deletions test/HighlightPairedShortcodeTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ alert();`, "js", "", { alwaysWrapLineHighlights: true }), `<pre class="language-
});

test("Base with LF EOL, always wrap highlights", async t => {
t.is(await HighlightPairedShortcode('alert();\nalert();',
t.is(await HighlightPairedShortcode('alert();\nalert();',
"js", "", { alwaysWrapLineHighlights: true }), `<pre class="language-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><br><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("Base with LF EOL, no wrap highlights", async t => {
t.is(await HighlightPairedShortcode('alert();\nalert();',
t.is(await HighlightPairedShortcode('alert();\nalert();',
"js", "", { alwaysWrapLineHighlights: false }), `<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("Base with CRLF EOL, always wrap highlights", async t => {
t.is(await HighlightPairedShortcode('alert();\r\nalert();',
t.is(await HighlightPairedShortcode('alert();\r\nalert();',
"js", "", { alwaysWrapLineHighlights: true }), `<pre class="language-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><br><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("Base with CRLF EOL, no wrap highlights", async t => {
t.is(await HighlightPairedShortcode('alert();\r\nalert();',
t.is(await HighlightPairedShortcode('alert();\r\nalert();',
"js", "", { alwaysWrapLineHighlights: false }), `<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>`);
});

Expand Down Expand Up @@ -85,17 +85,22 @@ document.body.textContent = greeter(user);`
t.is(await HighlightPairedShortcode(script, "ts"), `<pre class="language-ts"><code class="language-ts"><span class="token keyword">function</span> <span class="token function">greeter</span><span class="token punctuation">(</span>person<span class="token punctuation">)</span> <span class="token punctuation">{</span><br> <span class="token keyword">return</span> <span class="token string">"Hello, "</span> <span class="token operator">+</span> person<span class="token punctuation">;</span><br><span class="token punctuation">}</span><br><br><span class="token keyword">let</span> user <span class="token operator">=</span> <span class="token string">"Jane User"</span><span class="token punctuation">;</span><br><br>document<span class="token punctuation">.</span>body<span class="token punctuation">.</span>textContent <span class="token operator">=</span> <span class="token function">greeter</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>`);
});

test("Test loader invalid language", async t => {
test("Test loader invalid language, with errorOnInvalidLanguage option", async t => {
await t.throwsAsync(async () => {
await HighlightPairedShortcode("", "asldkjflksdaj");
});
await HighlightPairedShortcode("", "asldkjflksdaj", null, {
errorOnInvalidLanguage: true
});
}, { message: `"asldkjflksdaj" is not a valid Prism.js language for eleventy-plugin-syntaxhighlight` });
});

test("Test loader invalid language (should pass)", async t => {
t.is(await HighlightPairedShortcode("test test test", "asldkjflksdaj"), `<pre class="language-asldkjflksdaj"><code class="language-asldkjflksdaj">test test test</code></pre>`)
});

test("Test loader invalid language with ignore", async t => {
let src = `hello
hello`
t.is(await HighlightPairedShortcode(src, "asldkjflksdaj", "", { ignoreInvalidLanguages: "html" }), `<pre class="language-asldkjflksdaj"><code class="language-asldkjflksdaj">hello<br>hello</code></pre>`);
t.is(await HighlightPairedShortcode(src, "asldkjflksdaj", "", { ignoreInvalidLanguages: "md" }), src);
t.is(await HighlightPairedShortcode(src, "asldkjflksdaj"), `<pre class="language-asldkjflksdaj"><code class="language-asldkjflksdaj">hello<br>hello</code></pre>`);
});

test("Trim content option (defaults true)", async t => {
Expand Down
16 changes: 6 additions & 10 deletions test/MarkdownHighlightTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@ test("Test Nunjucks Alias", t => {
\`\`\``).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 loader invalid language", t => {
test("Markdown Invalid language", t => {
let mdLib = md();
mdLib.set({
highlight: markdownPrismJsOptions()
highlight: markdownPrismJsOptions({
errorOnInvalidLanguage: true
})
});

t.throws(() => {
mdLib.render(`\`\`\`asldkjflksdaj
hello
Expand All @@ -77,16 +80,9 @@ hello

let mdLib = md();
mdLib.set({
highlight: markdownPrismJsOptions({ ignoreInvalidLanguages: "html" })
highlight: markdownPrismJsOptions()
});
t.is(mdLib.render(src).trim(), `<pre class="language-asldkjflksdaj"><code class="language-asldkjflksdaj">hello</code></pre>`);

mdLib = md();
mdLib.set({
highlight: markdownPrismJsOptions({ ignoreInvalidLanguages: "md" })
});
t.is(mdLib.render(src).trim(), `<pre><code class="language-asldkjflksdaj">hello
</code></pre>`);
});

// test("Test Markdown Highlighter Block Comment", t => {
Expand Down

0 comments on commit 2159800

Please sign in to comment.