fix: do not add a newline to an empty code block (#4073)

* fix: do not add a newline to an empty code block

Renderer.code appended a newline unconditionally, so a code block with no
content rendered as <pre><code>\n</code></pre> instead of
<pre><code></code></pre>.

This fixes CommonMark examples 126, 130, 144 and 237. The spec suite did not
catch it because htmlIsEqual leaves html-differ's ignoreWhitespaces at its
default of true, and whitespace inside pre/code is significant, so all four
were already reported as passing.

* test: move the empty code block tests to test/specs/new

Uses renderExact so the fixture compares exactly, which htmlIsEqual
does not, and keeps the assertions out of marked.test.js.
This commit is contained in:
Miodrag Obradovic
2026-09-04 14:39:12 -06:00
committed by GitHub
parent df57534194
commit 23b1706a0e
3 changed files with 30 additions and 1 deletions
+2 -1
View File
@@ -25,7 +25,8 @@ export class _Renderer<ParserOutput = string, RendererOutput = string> {
code({ text, lang, escaped }: Tokens.Code): RendererOutput {
const langString = (lang || '').match(other.notSpaceStart)?.[0];
const code = text.replace(other.endingNewline, '') + '\n';
// An empty code block has no content, so it must not gain a newline.
const code = text ? text.replace(other.endingNewline, '') + '\n' : '';
if (!langString) {
return '<pre><code>'
+8
View File
@@ -0,0 +1,8 @@
<pre><code></code></pre>
<p>sep</p>
<pre><code class="language-;"></code></pre>
<p>sep</p>
<pre><code>x
</code></pre>
<p>sep</p>
<pre><code></code></pre>
+20
View File
@@ -0,0 +1,20 @@
---
renderExact: true
---
```
```
sep
````;
````
sep
```
x
```
sep
```