Skip to content

Commit 52eed9d

Browse files
committed
Fixes #77
1 parent de2b733 commit 52eed9d

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

syntaxHighlight.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import PrismLoader from "./src/PrismLoader.js";
44
import hasTemplateFormat from "./src/hasTemplateFormat.js";
55
import HighlightPairedShortcode from "./src/HighlightPairedShortcode.js";
66

7+
const TRIPLE_BACKTICK = "```";
8+
79
export default function(eleventyConfig, options){
810
try {
911
eleventyConfig.versionCheck(pkg["11ty"].compatibility);
@@ -13,7 +15,6 @@ export default function(eleventyConfig, options){
1315
options = Object.assign({
1416
init: function({Prism}){},
1517
lineSeparator: "\n",
16-
lineHighlightSeparator: " ",
1718
errorOnInvalidLanguage: false,
1819
alwaysWrapLineHighlights: false,
1920
preAttributes: {},
@@ -25,20 +26,27 @@ export default function(eleventyConfig, options){
2526
PrismLoader(language)
2627
}
2728

29+
function shortcode(content, args) {
30+
// {% highlight "js 0 2-3" %}
31+
let [language, ...highlightNumbers] = (args || "").split(" ");
32+
33+
// Issue #77 avoid issues when nesting code blocks inside other elements (e.g. <div>{% highlight %}…</div>), introducing extra paragraphs
34+
if(this.page.inputPath.endsWith(".md")) {
35+
let highlightNumbersStr = highlightNumbers.length > 0 ? `/${highlightNumbers.join("/")}` : "";
36+
return `\n\n${TRIPLE_BACKTICK}${language || ""}${highlightNumbersStr}
37+
${content}
38+
${TRIPLE_BACKTICK}\n\n`;
39+
}
40+
41+
return HighlightPairedShortcode(content, language, highlightNumbers.join(" "), options);
42+
}
43+
2844
if( hasTemplateFormat(options.templateFormats, "liquid") ) {
29-
eleventyConfig.addPairedLiquidShortcode("highlight", function(content, args) {
30-
// {% highlight "js 0 2-3" %}
31-
let [language, ...highlightNumbers] = (args || "").split(" ");
32-
return HighlightPairedShortcode(content, language, highlightNumbers.join(" "), options);
33-
});
45+
eleventyConfig.addPairedLiquidShortcode("highlight", shortcode);
3446
}
3547

3648
if( hasTemplateFormat(options.templateFormats, "njk") ) {
37-
eleventyConfig.addPairedNunjucksShortcode("highlight", function(content, args) {
38-
// {% highlight "js 0 2-3" %}
39-
let [language, ...highlightNumbers] = (args || "").split(" ");
40-
return HighlightPairedShortcode(content, language, highlightNumbers.join(" "), options);
41-
});
49+
eleventyConfig.addPairedNunjucksShortcode("highlight", shortcode);
4250
}
4351

4452
if( hasTemplateFormat(options.templateFormats, "md") ) {

test/EleventyTest.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,14 @@ test("diff-javascript #80", async t => {
3333
t.is(json.length, 1);
3434
t.is(json[0].content.trim(), `<pre class="language-diff-javascript"><code class="language-diff-javascript"><span class="token deleted-sign deleted language-javascript"><span class="token prefix deleted">-</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span></code></pre>`);
3535
});
36+
37+
test("Issue #77", async t => {
38+
let json = await render("issue-77.md", `<div>{% highlight "js" %}
39+
// line 1
40+
41+
// line 3
42+
{% endhighlight %}</div>`);
43+
44+
let content = json[0].content.trim();
45+
t.is(content, `<div>\n<pre class="language-js"><code class="language-js"><span class="token comment">// line 1</span>\n\n<span class="token comment">// line 3</span></code></pre>\n</div>`);
46+
});

0 commit comments

Comments
 (0)