Skip to content

Commit

Permalink
improve paragraph
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Jul 1, 2019
1 parent ba2eedd commit 8ccc1f1
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 50 deletions.
33 changes: 20 additions & 13 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]+/
};

Expand Down Expand Up @@ -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', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|!--)')
.replace('tag', block._tag) // pars can be interrupted by type (6) html blocks
.getRegex();

Expand All @@ -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
*/
Expand All @@ -127,7 +125,16 @@ block.pedantic = merge({}, block.normal, {
.getRegex(),
def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\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()
});

/**
Expand Down
18 changes: 6 additions & 12 deletions test/specs/commonmark/commonmark.0.29.json
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,7 @@
"example": 63,
"start_line": 1186,
"end_line": 1196,
"section": "Setext headings",
"shouldFail": true
"section": "Setext headings"
},
{
"markdown": "- Foo\n---\n",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -2365,8 +2360,7 @@
"example": 291,
"start_line": 5379,
"end_line": 5397,
"section": "Lists",
"shouldFail": true
"section": "Lists"
},
{
"markdown": "- a\n",
Expand Down
18 changes: 6 additions & 12 deletions test/specs/gfm/commonmark.0.29.json
Original file line number Diff line number Diff line change
Expand Up @@ -358,17 +358,15 @@
"example": 45,
"start_line": 915,
"end_line": 919,
"section": "ATX headings",
"shouldFail": true
"section": "ATX headings"
},
{
"markdown": "### foo \\###\n## foo #\\##\n# foo \\#\n",
"html": "<h3>foo ###</h3>\n<h2>foo ###</h2>\n<h1>foo #</h1>\n",
"example": 46,
"start_line": 925,
"end_line": 933,
"section": "ATX headings",
"shouldFail": true
"section": "ATX headings"
},
{
"markdown": "****\n## foo\n****\n",
Expand Down Expand Up @@ -507,8 +505,7 @@
"example": 63,
"start_line": 1186,
"end_line": 1196,
"section": "Setext headings",
"shouldFail": true
"section": "Setext headings"
},
{
"markdown": "- Foo\n---\n",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -2365,8 +2360,7 @@
"example": 291,
"start_line": 5379,
"end_line": 5397,
"section": "Lists",
"shouldFail": true
"section": "Lists"
},
{
"markdown": "- a\n",
Expand Down
5 changes: 5 additions & 0 deletions test/specs/new/pedantic_heading_interrupts_paragraph.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>paragraph before head with hash</p>
<h1 id="how-are-you">how are you</h1>

<p>paragraph before head with equals</p>
<h1 id="how-are-you-again">how are you again</h1>
10 changes: 10 additions & 0 deletions test/specs/new/pedantic_heading_interrupts_paragraph.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
pedantic: true
---

paragraph before head with hash
#how are you

paragraph before head with equals
how are you again
===========
6 changes: 0 additions & 6 deletions test/specs/new/toplevel_paragraphs.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
<p>paragraph before hr</p>
<hr>

<p>paragraph before head with hash</p>
<h1 id="how-are-you">how are you</h1>

<p>paragraph before head with equals</p>
<h1 id="how-are-you-again">how are you again</h1>

<p>paragraph before blockquote</p>
<blockquote><p>text for blockquote</p></blockquote>

Expand Down
7 changes: 0 additions & 7 deletions test/specs/new/toplevel_paragraphs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8ccc1f1

Please sign in to comment.