Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: leave whitespace only lines alone #1889

Merged
merged 3 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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|$))*)?)+/,
calculuschild marked this conversation as resolved.
Show resolved Hide resolved
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