diff --git a/packages/gatsby-remark-embed-snippet/src/__tests__/__snapshots__/index.js.snap b/packages/gatsby-remark-embed-snippet/src/__tests__/__snapshots__/index.js.snap index 19a759e5ab8f3..0b57603e95506 100644 --- a/packages/gatsby-remark-embed-snippet/src/__tests__/__snapshots__/index.js.snap +++ b/packages/gatsby-remark-embed-snippet/src/__tests__/__snapshots__/index.js.snap @@ -84,8 +84,7 @@ Object { * { box-sizing: border-box; -} - +} ", }, ], @@ -145,8 +144,7 @@ Object {
html {
 height: 100%;
 width: 100%;
-}
-
+} ", }, ], @@ -267,8 +265,7 @@ Object { highlighted </p> </body> -</html> - +</html> ", }, ], @@ -331,8 +328,7 @@ Object { highlighted </p> </body> -</html> - +</html> ", }, ], @@ -455,8 +451,7 @@ Object { transitionLeaveTimeout={300}> {items} </ReactCSSTransitionGroup> -</div> - +</div> ", }, ], @@ -580,8 +575,7 @@ Object {
ReactDOM.render(
 <h1>Hello, world!</h1>,
 document.getElementById('root')
-);
-
+); ", }, ], @@ -644,8 +638,7 @@ Object { <li>Highlighted</li> <li>Highlighted</li> <li>Not highlighted</li> -</div> - +</div> ", }, ], @@ -885,8 +878,7 @@ Object {
foo: \\"highlighted\\"
 bar: \\"not highlighted\\"
 baz: \\"highlighted\\"
-qux: \\"not highlighted\\"
-
+qux: \\"not highlighted\\" ", }, ], @@ -1127,8 +1119,7 @@ Object {
echo \\"not highlighted\\"
 echo \\"highlighted\\"
 echo \\"highlighted\\"
-echo \\"not highlighted\\"
-
+echo \\"not highlighted\\" ", }, ], diff --git a/packages/gatsby-remark-prismjs/src/__tests__/__snapshots__/highlight-code.js.snap b/packages/gatsby-remark-prismjs/src/__tests__/__snapshots__/highlight-code.js.snap index f548d6d028e57..44ece9ba63567 100644 --- a/packages/gatsby-remark-prismjs/src/__tests__/__snapshots__/highlight-code.js.snap +++ b/packages/gatsby-remark-prismjs/src/__tests__/__snapshots__/highlight-code.js.snap @@ -5,7 +5,6 @@ exports[`highlight code and lines with PrismJS for language cpp 1`] = ` int sum(a, b) { return a + b; } - " `; @@ -36,6 +35,5 @@ exports[`highlight code and lines with PrismJS for language jsx 1`] = ` } export default Counter - " `; diff --git a/packages/gatsby-remark-prismjs/src/__tests__/highlight-code.js b/packages/gatsby-remark-prismjs/src/__tests__/highlight-code.js index c0a834938b5ab..eeb4d7093c8b7 100644 --- a/packages/gatsby-remark-prismjs/src/__tests__/highlight-code.js +++ b/packages/gatsby-remark-prismjs/src/__tests__/highlight-code.js @@ -70,4 +70,40 @@ export default Counter expect(highlightCode(language, code)).toMatch(code) }) }) + + describe(`with non-highlight-lines`, () => { + it(`does not add trailing newlines`, () => { + const highlightCode = require(`../highlight-code`) + const language = `javascript` + const code = `const a = 1\nconst b = 2` + expect(highlightCode(language, code)).not.toMatch(/\n$/) + }) + + it(`a trailing newline is preserved`, () => { + const highlightCode = require(`../highlight-code`) + const language = `javascript` + const code = `const a = 1\nconst b = 2\n` + expect(highlightCode(language, code)).toMatch(/[^\n]\n$/) + }) + }) + + describe(`with non-highlight-lines`, () => { + it(`does not add trailing newlines`, () => { + const highlightCode = require(`../highlight-code`) + const language = `javascript` + const linesToHighlight = [1] + const code = `const a = 1\nconst b = 2` + expect(highlightCode(language, code, linesToHighlight)).not.toMatch(/\n$/) + }) + + it(`a trailing newline is preserved`, () => { + const highlightCode = require(`../highlight-code`) + const language = `javascript` + const linesToHighlight = [1] + const code = `const a = 1\nconst b = 2\n` + expect(highlightCode(language, code, linesToHighlight)).toMatch( + /[^\n]\n$/ + ) + }) + }) }) diff --git a/packages/gatsby-remark-prismjs/src/highlight-code.js b/packages/gatsby-remark-prismjs/src/highlight-code.js index 351dca4b921ea..d9bcdcbefdb70 100644 --- a/packages/gatsby-remark-prismjs/src/highlight-code.js +++ b/packages/gatsby-remark-prismjs/src/highlight-code.js @@ -34,12 +34,13 @@ module.exports = (language, code, lineNumbersHighlight = []) => { }) highlightedCode = `` - // Don't add a new line character after highlighted lines as they - // need to be display: block and full-width. - codeSplits.forEach(split => { + const lastIdx = codeSplits.length - 1 + // Don't add back the new line character after highlighted lines + // as they need to be display: block and full-width. + codeSplits.forEach((split, idx) => { split.highlighted ? (highlightedCode += split.code) - : (highlightedCode += `${split.code}\n`) + : (highlightedCode += `${split.code}${idx == lastIdx ? `` : `\n`}`) }) }