Skip to content

Commit 8ccc1f1

Browse files
committed
improve paragraph
1 parent ba2eedd commit 8ccc1f1

7 files changed

+47
-50
lines changed

lib/marked.js

+20-13
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ var block = {
3333
nptable: noop,
3434
table: noop,
3535
lheading: /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/,
36-
paragraph: /^([^\n]+(?:\n(?!hr|heading|lheading| {0,3}>|<\/?(?:tag)(?: +|\n|\/?>)|<(?:script|pre|style|!--))[^\n]+)*)/,
36+
// regex template, placeholders will be replaced according to different paragraph
37+
// interruption rules of commonmark and the original markdown spec:
38+
_paragraph: /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html)[^\n]+)*)/,
3739
text: /^[^\n]+/
3840
};
3941

@@ -69,10 +71,14 @@ block.html = edit(block.html, 'i')
6971
.replace('attribute', / +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/)
7072
.getRegex();
7173

72-
block.paragraph = edit(block.paragraph)
74+
block.paragraph = edit(block._paragraph)
7375
.replace('hr', block.hr)
74-
.replace('heading', block.heading)
75-
.replace('lheading', block.lheading)
76+
.replace('heading', ' {0,3}#{1,6} +')
77+
.replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs
78+
.replace('blockquote', ' {0,3}>')
79+
.replace('fences', ' {0,3}(?:`{3,}|~{3,})[^`\\n]*\\n')
80+
.replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
81+
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|!--)')
7682
.replace('tag', block._tag) // pars can be interrupted by type (6) html blocks
7783
.getRegex();
7884

@@ -91,16 +97,8 @@ block.normal = merge({}, block);
9197
*/
9298

9399
block.gfm = merge({}, block.normal, {
94-
paragraph: /^/,
95-
heading: /^ *(#{1,6}) +([^\n]+?) *#* *(?:\n+|$)/
96100
});
97101

98-
block.gfm.paragraph = edit(block.paragraph)
99-
.replace('(?!', '(?!'
100-
+ block.fences.source.replace('\\1', '\\2') + '|'
101-
+ block.list.source.replace('\\1', '\\3') + '|')
102-
.getRegex();
103-
104102
/**
105103
* GFM + Tables Block Grammar
106104
*/
@@ -127,7 +125,16 @@ block.pedantic = merge({}, block.normal, {
127125
.getRegex(),
128126
def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,
129127
heading: /^ *(#{1,6}) *([^\n]+?) *(?:#+ *)?(?:\n+|$)/,
130-
fences: noop // fences not supported
128+
fences: noop, // fences not supported
129+
paragraph: edit(block.normal._paragraph)
130+
.replace('hr', block.hr)
131+
.replace('heading', ' *#{1,6} *[^\n]')
132+
.replace('lheading', block.lheading)
133+
.replace('blockquote', ' {0,3}>')
134+
.replace('|fences', '')
135+
.replace('|list', '')
136+
.replace('|html', '')
137+
.getRegex()
131138
});
132139

133140
/**

test/specs/commonmark/commonmark.0.29.json

+6-12
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,7 @@
505505
"example": 63,
506506
"start_line": 1186,
507507
"end_line": 1196,
508-
"section": "Setext headings",
509-
"shouldFail": true
508+
"section": "Setext headings"
510509
},
511510
{
512511
"markdown": "- Foo\n---\n",
@@ -883,8 +882,7 @@
883882
"example": 110,
884883
"start_line": 1908,
885884
"end_line": 1919,
886-
"section": "Fenced code blocks",
887-
"shouldFail": true
885+
"section": "Fenced code blocks"
888886
},
889887
{
890888
"markdown": "foo\n---\n~~~\nbar\n~~~\n# baz\n",
@@ -1648,8 +1646,7 @@
16481646
"example": 205,
16491647
"start_line": 3464,
16501648
"end_line": 3476,
1651-
"section": "Block quotes",
1652-
"shouldFail": true
1649+
"section": "Block quotes"
16531650
},
16541651
{
16551652
"markdown": "> foo\n bar\n",
@@ -2210,8 +2207,7 @@
22102207
"example": 273,
22112208
"start_line": 4928,
22122209
"end_line": 4938,
2213-
"section": "Lists",
2214-
"shouldFail": true
2210+
"section": "Lists"
22152211
},
22162212
{
22172213
"markdown": "The number of windows in my house is\n14. The number of doors is 6.\n",
@@ -2227,8 +2223,7 @@
22272223
"example": 275,
22282224
"start_line": 5015,
22292225
"end_line": 5023,
2230-
"section": "Lists",
2231-
"shouldFail": true
2226+
"section": "Lists"
22322227
},
22332228
{
22342229
"markdown": "- foo\n\n- bar\n\n\n- baz\n",
@@ -2365,8 +2360,7 @@
23652360
"example": 291,
23662361
"start_line": 5379,
23672362
"end_line": 5397,
2368-
"section": "Lists",
2369-
"shouldFail": true
2363+
"section": "Lists"
23702364
},
23712365
{
23722366
"markdown": "- a\n",

test/specs/gfm/commonmark.0.29.json

+6-12
Original file line numberDiff line numberDiff line change
@@ -358,17 +358,15 @@
358358
"example": 45,
359359
"start_line": 915,
360360
"end_line": 919,
361-
"section": "ATX headings",
362-
"shouldFail": true
361+
"section": "ATX headings"
363362
},
364363
{
365364
"markdown": "### foo \\###\n## foo #\\##\n# foo \\#\n",
366365
"html": "<h3>foo ###</h3>\n<h2>foo ###</h2>\n<h1>foo #</h1>\n",
367366
"example": 46,
368367
"start_line": 925,
369368
"end_line": 933,
370-
"section": "ATX headings",
371-
"shouldFail": true
369+
"section": "ATX headings"
372370
},
373371
{
374372
"markdown": "****\n## foo\n****\n",
@@ -507,8 +505,7 @@
507505
"example": 63,
508506
"start_line": 1186,
509507
"end_line": 1196,
510-
"section": "Setext headings",
511-
"shouldFail": true
508+
"section": "Setext headings"
512509
},
513510
{
514511
"markdown": "- Foo\n---\n",
@@ -1649,8 +1646,7 @@
16491646
"example": 205,
16501647
"start_line": 3464,
16511648
"end_line": 3476,
1652-
"section": "Block quotes",
1653-
"shouldFail": true
1649+
"section": "Block quotes"
16541650
},
16551651
{
16561652
"markdown": "> foo\n bar\n",
@@ -2219,8 +2215,7 @@
22192215
"example": 274,
22202216
"start_line": 5005,
22212217
"end_line": 5011,
2222-
"section": "Lists",
2223-
"shouldFail": true
2218+
"section": "Lists"
22242219
},
22252220
{
22262221
"markdown": "The number of windows in my house is\n1. The number of doors is 6.\n",
@@ -2365,8 +2360,7 @@
23652360
"example": 291,
23662361
"start_line": 5379,
23672362
"end_line": 5397,
2368-
"section": "Lists",
2369-
"shouldFail": true
2363+
"section": "Lists"
23702364
},
23712365
{
23722366
"markdown": "- a\n",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<p>paragraph before head with hash</p>
2+
<h1 id="how-are-you">how are you</h1>
3+
4+
<p>paragraph before head with equals</p>
5+
<h1 id="how-are-you-again">how are you again</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
pedantic: true
3+
---
4+
5+
paragraph before head with hash
6+
#how are you
7+
8+
paragraph before head with equals
9+
how are you again
10+
===========

test/specs/new/toplevel_paragraphs.html

-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@
88
<p>paragraph before hr</p>
99
<hr>
1010

11-
<p>paragraph before head with hash</p>
12-
<h1 id="how-are-you">how are you</h1>
13-
14-
<p>paragraph before head with equals</p>
15-
<h1 id="how-are-you-again">how are you again</h1>
16-
1711
<p>paragraph before blockquote</p>
1812
<blockquote><p>text for blockquote</p></blockquote>
1913

test/specs/new/toplevel_paragraphs.md

-7
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ text inside block code
1313
paragraph before hr
1414
* * *
1515

16-
paragraph before head with hash
17-
# how are you
18-
19-
paragraph before head with equals
20-
how are you again
21-
===========
22-
2316
paragraph before blockquote
2417
> text for blockquote
2518

0 commit comments

Comments
 (0)