mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
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:
+2
-1
@@ -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>'
|
||||
|
||||
@@ -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>
|
||||
@@ -0,0 +1,20 @@
|
||||
---
|
||||
renderExact: true
|
||||
---
|
||||
```
|
||||
```
|
||||
|
||||
sep
|
||||
|
||||
````;
|
||||
````
|
||||
|
||||
sep
|
||||
|
||||
```
|
||||
x
|
||||
```
|
||||
|
||||
sep
|
||||
|
||||
```
|
||||
Reference in New Issue
Block a user