From 3b776d5b0b56ed6502bebe2538870c5556f8c886 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Wed, 23 Dec 2020 00:45:17 -0600 Subject: [PATCH 1/3] fix: leave whitespace only lines alone --- src/Lexer.js | 4 +++- src/Tokenizer.js | 2 +- src/rules.js | 6 +++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Lexer.js b/src/Lexer.js index 493ce9e6ea..bb04b43101 100644 --- a/src/Lexer.js +++ b/src/Lexer.js @@ -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) { diff --git a/src/Tokenizer.js b/src/Tokenizer.js index d78202fac1..96b59418ca 100644 --- a/src/Tokenizer.js +++ b/src/Tokenizer.js @@ -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], diff --git a/src/rules.js b/src/rules.js index 69975e189a..b7f245e714 100644 --- a/src/rules.js +++ b/src/rules.js @@ -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+|$)/, @@ -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]+/ }; From a5a8e9198b3db6b69afb96015a11499a7e938dbe Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Wed, 23 Dec 2020 00:45:43 -0600 Subject: [PATCH 2/3] test: add whitepace_lines test --- test/specs/new/whiltespace_lines.html | 10 ++++++++++ test/specs/new/whiltespace_lines.md | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 test/specs/new/whiltespace_lines.html create mode 100644 test/specs/new/whiltespace_lines.md diff --git a/test/specs/new/whiltespace_lines.html b/test/specs/new/whiltespace_lines.html new file mode 100644 index 0000000000..84c31df565 --- /dev/null +++ b/test/specs/new/whiltespace_lines.html @@ -0,0 +1,10 @@ +

paragraph

+

test

+
a
+  
+b
+
+c
+
a
+  
+b
diff --git a/test/specs/new/whiltespace_lines.md b/test/specs/new/whiltespace_lines.md new file mode 100644 index 0000000000..ff645b3842 --- /dev/null +++ b/test/specs/new/whiltespace_lines.md @@ -0,0 +1,18 @@ +--- +renderExact: true +--- +paragraph + +test + + a + + b + + c + +``` +a + +b +``` From f3a067153d2bcc5184826791d9f256347cd86a5e Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Wed, 23 Dec 2020 00:55:02 -0600 Subject: [PATCH 3/3] fix: render code with trailing new line --- src/Renderer.js | 2 ++ test/specs/new/code_compensation_indent.html | 3 ++- test/specs/new/code_consistent_newline.html | 6 ++++-- test/specs/new/whiltespace_lines.html | 6 ++++-- test/unit/marked-spec.js | 18 ++++++++++++------ 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/Renderer.js b/src/Renderer.js index ace12381bb..1fa9714602 100644 --- a/src/Renderer.js +++ b/src/Renderer.js @@ -22,6 +22,8 @@ module.exports = class Renderer { } } + code = code.replace(/\n$/, '') + '\n'; + if (!lang) { return '
'
         + (escaped ? code : escape(code, true))
diff --git a/test/specs/new/code_compensation_indent.html b/test/specs/new/code_compensation_indent.html
index b6b7d227b4..4c2ebefb8a 100644
--- a/test/specs/new/code_compensation_indent.html
+++ b/test/specs/new/code_compensation_indent.html
@@ -2,6 +2,7 @@
 
  1. This is a list element.

    const x = 5;
    -const y = x + 5;
    +const y = x + 5; +
diff --git a/test/specs/new/code_consistent_newline.html b/test/specs/new/code_consistent_newline.html index f1ebc6fc8d..41a15c4ee8 100644 --- a/test/specs/new/code_consistent_newline.html +++ b/test/specs/new/code_consistent_newline.html @@ -1,3 +1,5 @@ -
const value = 42;
-
const value = 42;
+
const value = 42;
+
+
const value = 42;
+

Code blocks contain trailing new line.

diff --git a/test/specs/new/whiltespace_lines.html b/test/specs/new/whiltespace_lines.html index 84c31df565..97931fe26a 100644 --- a/test/specs/new/whiltespace_lines.html +++ b/test/specs/new/whiltespace_lines.html @@ -4,7 +4,9 @@ b -c +c +
a
   
-b
+b + diff --git a/test/unit/marked-spec.js b/test/unit/marked-spec.js index fcee51a3eb..7d81117288 100644 --- a/test/unit/marked-spec.js +++ b/test/unit/marked-spec.js @@ -335,12 +335,15 @@ text 1 fail(err); } - expect(html).toBe(`
async text 1
+ expect(html).toBe(`
async text 1
+
-
async text 2
+
async text 2
+
    -
  • async text 3
    +
  • async text 3
    +
`); @@ -378,12 +381,15 @@ text 1 fail(err); } - expect(html).toBe(`
async text 1
+ expect(html).toBe(`
async text 1
+
-
async text 2
+
async text 2
+
    -
  • async text 3
    +
  • async text 3
    +
`);