From f305fc5d8b9e13d242694fc767ef5db3e37a16fd Mon Sep 17 00:00:00 2001 From: Mathias Bynens Date: Sun, 23 Sep 2018 04:13:37 +0200 Subject: [PATCH 1/3] Support multiple consecutive line breaks in code snippets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, repeated consecutive line breaks got eaten by this syntax highlighter plugin. Consider this imaginary code snippet: ``` foo bar ``` The empty line got turned into an empty `
`, which is a form of data loss — the actual line break is no longer part of the resulting HTML. This caused it to render differently, too. This patch ensures newlines are preserved by explicitly inserting them in the resulting HTML when needed. --- src/markdownSyntaxHighlight.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/markdownSyntaxHighlight.js b/src/markdownSyntaxHighlight.js index 71be334..660429f 100644 --- a/src/markdownSyntaxHighlight.js +++ b/src/markdownSyntaxHighlight.js @@ -37,7 +37,7 @@ module.exports = function(str, language) { (highlights.isHighlightedAdd(j) ? " highlight-line-add" : "") + (highlights.isHighlightedRemove(j) ? " highlight-line-remove" : "") + "\">" + - line + + (line || '\n') + ""; }); From 5d6696895c313a38156dac708be7572297b28b87 Mon Sep 17 00:00:00 2001 From: Mathias Bynens Date: Sat, 22 Sep 2018 22:35:25 -0400 Subject: [PATCH 2/3] Add link to issue --- src/markdownSyntaxHighlight.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/markdownSyntaxHighlight.js b/src/markdownSyntaxHighlight.js index 660429f..b87f841 100644 --- a/src/markdownSyntaxHighlight.js +++ b/src/markdownSyntaxHighlight.js @@ -37,7 +37,7 @@ module.exports = function(str, language) { (highlights.isHighlightedAdd(j) ? " highlight-line-add" : "") + (highlights.isHighlightedRemove(j) ? " highlight-line-remove" : "") + "\">" + - (line || '\n') + + (line || '\n') + // https://github.com/11ty/eleventy-plugin-syntaxhighlight/pull/5 ""; }); From a8de1613555a00fda9f0a0151d3d07c5430d10cc Mon Sep 17 00:00:00 2001 From: Mathias Bynens Date: Sun, 23 Sep 2018 07:10:33 -0400 Subject: [PATCH 3/3] Implement alternate approach The benefit of this approach is that the resulting HTML more closely matches the input, since newlines are now added unconditionally. Instead of `
` we could also use `\n`, but `
` is more explicit and looks better in source code IMHO. It ensures that when running html-minifier afterwards, the output remains a single line. --- src/markdownSyntaxHighlight.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/markdownSyntaxHighlight.js b/src/markdownSyntaxHighlight.js index b87f841..d909d7c 100644 --- a/src/markdownSyntaxHighlight.js +++ b/src/markdownSyntaxHighlight.js @@ -26,20 +26,16 @@ module.exports = function(str, language) { let highlights = new HighlightLinesGroup(split.join("/"), "/"); - let lines = html.split("\n"); + let lines = html.split("\n").slice(0, -1); // The last line is empty. let highlightedLines = lines.map(function(line, j) { - if( j + 1 === lines.length ) { - return ""; - } - - return "
" + - (line || '\n') + // https://github.com/11ty/eleventy-plugin-syntaxhighlight/pull/5 - "
"; + line + + ""; }); - return `
${highlightedLines.join("")}
`; + return `
${highlightedLines.join("
")}
`; };