From 8ccc1f1547ac9ab5d492461b25dcb8470d548ff8 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Mon, 1 Jul 2019 08:47:06 -0500 Subject: [PATCH] improve paragraph --- lib/marked.js | 33 +++++++++++-------- test/specs/commonmark/commonmark.0.29.json | 18 ++++------ test/specs/gfm/commonmark.0.29.json | 18 ++++------ ...pedantic_heading_interrupts_paragraph.html | 5 +++ .../pedantic_heading_interrupts_paragraph.md | 10 ++++++ test/specs/new/toplevel_paragraphs.html | 6 ---- test/specs/new/toplevel_paragraphs.md | 7 ---- 7 files changed, 47 insertions(+), 50 deletions(-) create mode 100644 test/specs/new/pedantic_heading_interrupts_paragraph.html create mode 100644 test/specs/new/pedantic_heading_interrupts_paragraph.md diff --git a/lib/marked.js b/lib/marked.js index 8543c6b288..6145a8cf65 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -33,7 +33,9 @@ var block = { nptable: noop, table: noop, lheading: /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/, - paragraph: /^([^\n]+(?:\n(?!hr|heading|lheading| {0,3}>|<\/?(?:tag)(?: +|\n|\/?>)|<(?:script|pre|style|!--))[^\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]+)*)/, text: /^[^\n]+/ }; @@ -69,10 +71,14 @@ block.html = edit(block.html, 'i') .replace('attribute', / +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/) .getRegex(); -block.paragraph = edit(block.paragraph) +block.paragraph = edit(block._paragraph) .replace('hr', block.hr) - .replace('heading', block.heading) - .replace('lheading', block.lheading) + .replace('heading', ' {0,3}#{1,6} +') + .replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs + .replace('blockquote', ' {0,3}>') + .replace('fences', ' {0,3}(?:`{3,}|~{3,})[^`\\n]*\\n') + .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt + .replace('html', ')|<(?:script|pre|style|!--)') .replace('tag', block._tag) // pars can be interrupted by type (6) html blocks .getRegex(); @@ -91,16 +97,8 @@ block.normal = merge({}, block); */ block.gfm = merge({}, block.normal, { - paragraph: /^/, - heading: /^ *(#{1,6}) +([^\n]+?) *#* *(?:\n+|$)/ }); -block.gfm.paragraph = edit(block.paragraph) - .replace('(?!', '(?!' - + block.fences.source.replace('\\1', '\\2') + '|' - + block.list.source.replace('\\1', '\\3') + '|') - .getRegex(); - /** * GFM + Tables Block Grammar */ @@ -127,7 +125,16 @@ block.pedantic = merge({}, block.normal, { .getRegex(), def: /^ *\[([^\]]+)\]: *]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/, heading: /^ *(#{1,6}) *([^\n]+?) *(?:#+ *)?(?:\n+|$)/, - fences: noop // fences not supported + fences: noop, // fences not supported + paragraph: edit(block.normal._paragraph) + .replace('hr', block.hr) + .replace('heading', ' *#{1,6} *[^\n]') + .replace('lheading', block.lheading) + .replace('blockquote', ' {0,3}>') + .replace('|fences', '') + .replace('|list', '') + .replace('|html', '') + .getRegex() }); /** diff --git a/test/specs/commonmark/commonmark.0.29.json b/test/specs/commonmark/commonmark.0.29.json index e203324415..e4f58e8aec 100644 --- a/test/specs/commonmark/commonmark.0.29.json +++ b/test/specs/commonmark/commonmark.0.29.json @@ -505,8 +505,7 @@ "example": 63, "start_line": 1186, "end_line": 1196, - "section": "Setext headings", - "shouldFail": true + "section": "Setext headings" }, { "markdown": "- Foo\n---\n", @@ -883,8 +882,7 @@ "example": 110, "start_line": 1908, "end_line": 1919, - "section": "Fenced code blocks", - "shouldFail": true + "section": "Fenced code blocks" }, { "markdown": "foo\n---\n~~~\nbar\n~~~\n# baz\n", @@ -1648,8 +1646,7 @@ "example": 205, "start_line": 3464, "end_line": 3476, - "section": "Block quotes", - "shouldFail": true + "section": "Block quotes" }, { "markdown": "> foo\n bar\n", @@ -2210,8 +2207,7 @@ "example": 273, "start_line": 4928, "end_line": 4938, - "section": "Lists", - "shouldFail": true + "section": "Lists" }, { "markdown": "The number of windows in my house is\n14. The number of doors is 6.\n", @@ -2227,8 +2223,7 @@ "example": 275, "start_line": 5015, "end_line": 5023, - "section": "Lists", - "shouldFail": true + "section": "Lists" }, { "markdown": "- foo\n\n- bar\n\n\n- baz\n", @@ -2365,8 +2360,7 @@ "example": 291, "start_line": 5379, "end_line": 5397, - "section": "Lists", - "shouldFail": true + "section": "Lists" }, { "markdown": "- a\n", diff --git a/test/specs/gfm/commonmark.0.29.json b/test/specs/gfm/commonmark.0.29.json index f577c7c5cd..885da2d835 100644 --- a/test/specs/gfm/commonmark.0.29.json +++ b/test/specs/gfm/commonmark.0.29.json @@ -358,8 +358,7 @@ "example": 45, "start_line": 915, "end_line": 919, - "section": "ATX headings", - "shouldFail": true + "section": "ATX headings" }, { "markdown": "### foo \\###\n## foo #\\##\n# foo \\#\n", @@ -367,8 +366,7 @@ "example": 46, "start_line": 925, "end_line": 933, - "section": "ATX headings", - "shouldFail": true + "section": "ATX headings" }, { "markdown": "****\n## foo\n****\n", @@ -507,8 +505,7 @@ "example": 63, "start_line": 1186, "end_line": 1196, - "section": "Setext headings", - "shouldFail": true + "section": "Setext headings" }, { "markdown": "- Foo\n---\n", @@ -1649,8 +1646,7 @@ "example": 205, "start_line": 3464, "end_line": 3476, - "section": "Block quotes", - "shouldFail": true + "section": "Block quotes" }, { "markdown": "> foo\n bar\n", @@ -2219,8 +2215,7 @@ "example": 274, "start_line": 5005, "end_line": 5011, - "section": "Lists", - "shouldFail": true + "section": "Lists" }, { "markdown": "The number of windows in my house is\n1. The number of doors is 6.\n", @@ -2365,8 +2360,7 @@ "example": 291, "start_line": 5379, "end_line": 5397, - "section": "Lists", - "shouldFail": true + "section": "Lists" }, { "markdown": "- a\n", diff --git a/test/specs/new/pedantic_heading_interrupts_paragraph.html b/test/specs/new/pedantic_heading_interrupts_paragraph.html new file mode 100644 index 0000000000..9e232fd65d --- /dev/null +++ b/test/specs/new/pedantic_heading_interrupts_paragraph.html @@ -0,0 +1,5 @@ +

paragraph before head with hash

+

how are you

+ +

paragraph before head with equals

+

how are you again

diff --git a/test/specs/new/pedantic_heading_interrupts_paragraph.md b/test/specs/new/pedantic_heading_interrupts_paragraph.md new file mode 100644 index 0000000000..13300a7a1b --- /dev/null +++ b/test/specs/new/pedantic_heading_interrupts_paragraph.md @@ -0,0 +1,10 @@ +--- +pedantic: true +--- + +paragraph before head with hash +#how are you + +paragraph before head with equals +how are you again +=========== diff --git a/test/specs/new/toplevel_paragraphs.html b/test/specs/new/toplevel_paragraphs.html index 7fd509470e..dec8d1d772 100644 --- a/test/specs/new/toplevel_paragraphs.html +++ b/test/specs/new/toplevel_paragraphs.html @@ -8,12 +8,6 @@

paragraph before hr


-

paragraph before head with hash

-

how are you

- -

paragraph before head with equals

-

how are you again

-

paragraph before blockquote

text for blockquote

diff --git a/test/specs/new/toplevel_paragraphs.md b/test/specs/new/toplevel_paragraphs.md index f344fbe349..9e92e44f9c 100644 --- a/test/specs/new/toplevel_paragraphs.md +++ b/test/specs/new/toplevel_paragraphs.md @@ -13,13 +13,6 @@ text inside block code paragraph before hr * * * -paragraph before head with hash -# how are you - -paragraph before head with equals -how are you again -=========== - paragraph before blockquote > text for blockquote