Skip to content

Commit

Permalink
fix: leave whitespace only lines alone (#1889)
Browse files Browse the repository at this point in the history
* fix: leave whitespace only lines alone

* test: add whitepace_lines test

* fix: render code with trailing new line
  • Loading branch information
UziTech authored Jan 26, 2021
1 parent 42a18f1 commit 53c79ee
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/Lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ module.exports = class Lexer {
* Lexing
*/
blockTokens(src, tokens = [], top = true) {
src = src.replace(/^ +$/gm, '');
if (this.options.pedantic) {
src = src.replace(/^ +$/gm, '');
}
let token, i, l, lastToken;

while (src) {
Expand Down
2 changes: 2 additions & 0 deletions src/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module.exports = class Renderer {
}
}

code = code.replace(/\n$/, '') + '\n';

if (!lang) {
return '<pre><code>'
+ (escaped ? code : escape(code, true))
Expand Down
2 changes: 1 addition & 1 deletion src/Tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ module.exports = class Tokenizer {
};
}

const text = cap[0].replace(/^ {4}/gm, '');
const text = cap[0].replace(/^ {1,4}/gm, '');
return {
type: 'code',
raw: cap[0],
Expand Down
6 changes: 3 additions & 3 deletions src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const {
* Block-Level Grammar
*/
const block = {
newline: /^\n+/,
code: /^( {4}[^\n]+\n*)+/,
newline: /^(?: *(?:\n|$))+/,
code: /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/,
fences: /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?:\n+|$)|$)/,
hr: /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/,
heading: /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/,
Expand All @@ -31,7 +31,7 @@ const block = {
lheading: /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/,
// regex template, placeholders will be replaced according to different paragraph
// interruption rules of commonmark and the original markdown spec:
_paragraph: /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html)[^\n]+)*)/,
_paragraph: /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html| +\n)[^\n]+)*)/,
text: /^[^\n]+/
};

Expand Down
3 changes: 2 additions & 1 deletion test/specs/new/code_compensation_indent.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<ol>
<li><p>This is a list element.</p>
<pre><code>const x = 5;
const y = x + 5;</code></pre>
const y = x + 5;
</code></pre>
</li>
</ol>
6 changes: 4 additions & 2 deletions test/specs/new/code_consistent_newline.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<pre><code class="language-js">const value = 42;</code></pre>
<pre><code>const value = 42;</code></pre>
<pre><code class="language-js">const value = 42;
</code></pre>
<pre><code>const value = 42;
</code></pre>
<p>Code blocks contain trailing new line.</p>
12 changes: 12 additions & 0 deletions test/specs/new/whiltespace_lines.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<p>paragraph</p>
<p>test</p>
<pre><code>a

b

c
</code></pre>
<pre><code>a

b
</code></pre>
18 changes: 18 additions & 0 deletions test/specs/new/whiltespace_lines.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
renderExact: true
---
paragraph

test

a
b

c

```
a
b
```
18 changes: 12 additions & 6 deletions test/unit/marked-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,15 @@ text 1
fail(err);
}

expect(html).toBe(`<pre><code class="language-lang1">async text 1</code></pre>
expect(html).toBe(`<pre><code class="language-lang1">async text 1
</code></pre>
<blockquote>
<pre><code class="language-lang2">async text 2</code></pre>
<pre><code class="language-lang2">async text 2
</code></pre>
</blockquote>
<ul>
<li><pre><code class="language-lang3">async text 3</code></pre>
<li><pre><code class="language-lang3">async text 3
</code></pre>
</li>
</ul>
`);
Expand Down Expand Up @@ -378,12 +381,15 @@ text 1
fail(err);
}

expect(html).toBe(`<pre><code class="language-lang1">async text 1</code></pre>
expect(html).toBe(`<pre><code class="language-lang1">async text 1
</code></pre>
<blockquote>
<pre><code class="language-lang2">async text 2</code></pre>
<pre><code class="language-lang2">async text 2
</code></pre>
</blockquote>
<ul>
<li><pre><code class="language-lang3">async text 3</code></pre>
<li><pre><code class="language-lang3">async text 3
</code></pre>
</li>
</ul>
`);
Expand Down

1 comment on commit 53c79ee

@vercel
Copy link

@vercel vercel bot commented on 53c79ee Jan 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.