Skip to content

Commit 68aab24

Browse files
authored
Merge pull request #1023 from Feder1co5oave/new_blockquote
[commonmark] Make blockquotes commonmark compliant
2 parents 19f6afe + 399cf82 commit 68aab24

7 files changed

+480
-51
lines changed

lib/marked.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ var block = {
1818
hr: /^( *[-*_]){3,} *(?:\n+|$)/,
1919
heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,
2020
nptable: noop,
21-
lheading: /^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/,
22-
blockquote: /^( *>[^\n]+(\n(?!def)[^\n]+)*\n*)+/,
21+
blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
2322
list: /^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
2423
html: /^ *(?:comment *(?:\n|\s*$)|closed *(?:\n{2,}|\s*$)|closing *(?:\n{2,}|\s*$))/,
2524
def: /^ {0,3}\[(label)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)(title))? *(?:\n+|$)/,
2625
table: noop,
27-
paragraph: /^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag))+)\n*/,
26+
lheading: /^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/,
27+
paragraph: /^([^\n]+(?:\n?(?!hr|heading|lheading| {0,3}>|tag)[^\n]+)+)/,
2828
text: /^[^\n]+/
2929
};
3030

@@ -47,10 +47,6 @@ block.list = replace(block.list)
4747
('def', '\\n+(?=' + block.def.source + ')')
4848
();
4949

50-
block.blockquote = replace(block.blockquote)
51-
('def', block.def)
52-
();
53-
5450
block._tag = '(?!(?:'
5551
+ 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code'
5652
+ '|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo'
@@ -67,10 +63,13 @@ block.paragraph = replace(block.paragraph)
6763
('hr', block.hr)
6864
('heading', block.heading)
6965
('lheading', block.lheading)
70-
('blockquote', block.blockquote)
7166
('tag', '<' + block._tag)
7267
();
7368

69+
block.blockquote = replace(block.blockquote)
70+
('paragraph', block.paragraph)
71+
();
72+
7473
/**
7574
* Normal Block Grammar
7675
*/
@@ -245,17 +244,6 @@ Lexer.prototype.token = function(src, top) {
245244
continue;
246245
}
247246

248-
// lheading
249-
if (cap = this.rules.lheading.exec(src)) {
250-
src = src.substring(cap[0].length);
251-
this.tokens.push({
252-
type: 'heading',
253-
depth: cap[2] === '=' ? 1 : 2,
254-
text: cap[1]
255-
});
256-
continue;
257-
}
258-
259247
// hr
260248
if (cap = this.rules.hr.exec(src)) {
261249
src = src.substring(cap[0].length);
@@ -423,6 +411,17 @@ Lexer.prototype.token = function(src, top) {
423411
continue;
424412
}
425413

414+
// lheading
415+
if (cap = this.rules.lheading.exec(src)) {
416+
src = src.substring(cap[0].length);
417+
this.tokens.push({
418+
type: 'heading',
419+
depth: cap[2] === '=' ? 1 : 2,
420+
text: cap[1]
421+
});
422+
continue;
423+
}
424+
426425
// top-level paragraph
427426
if (top && (cap = this.rules.paragraph.exec(src))) {
428427
src = src.substring(cap[0].length);

test/new/cm_blockquotes.html

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
<h3 id="example-191">Example 191</h3>
2+
3+
<blockquote>
4+
<h1 id="foo">Foo</h1>
5+
<p>bar
6+
baz</p>
7+
</blockquote>
8+
9+
<h3 id="example-192">Example 192</h3>
10+
11+
<p>The spaces after the <code>&gt;</code> characters can be omitted:</p>
12+
13+
<blockquote>
14+
<h1 id="foo">Foo</h1>
15+
<p>bar
16+
baz</p>
17+
</blockquote>
18+
19+
<h3 id="example-193">Example 193</h3>
20+
21+
<p>The <code>&gt;</code> characters can be indented 1-3 spaces:</p>
22+
23+
<blockquote>
24+
<h1 id="foo">Foo</h1>
25+
<p>bar
26+
baz</p>
27+
</blockquote>
28+
29+
<h3 id="example-194">Example 194</h3>
30+
31+
<p>Four spaces gives us a code block:</p>
32+
33+
<pre><code>&gt; # Foo
34+
&gt; bar
35+
&gt; baz
36+
</code></pre>
37+
38+
<h3 id="example-195">Example 195</h3>
39+
40+
<p>The Laziness clause allows us to omit the <code>&gt;</code> before paragraph continuation text:</p>
41+
42+
<blockquote>
43+
<h1 id="foo">Foo</h1>
44+
<p>bar
45+
baz</p>
46+
</blockquote>
47+
48+
<h3 id="example-196">Example 196</h3>
49+
50+
<p>A block quote can contain some lazy and some non-lazy continuation lines:</p>
51+
52+
<blockquote>
53+
<p>bar
54+
baz
55+
foo</p>
56+
</blockquote>
57+
58+
<h3 id="example-197">Example 197</h3>
59+
60+
<p>Laziness only applies to lines that would have been continuations of paragraphs had they been prepended with block quote markers. For example, the <code>&gt;</code> cannot be omitted in the second line of</p>
61+
62+
<blockquote>
63+
<p>foo</p>
64+
</blockquote>
65+
<hr>
66+
67+
<p>without changing the meaning.</p>
68+
69+
<h3 id="example-198">Example 198</h3>
70+
71+
<pre><code>Similarly, if we omit the `&gt;` in the second line then the block quote ends after the first line:
72+
73+
&gt; - foo
74+
- bar</code></pre>
75+
76+
<h3 id="example-199">Example 199</h3>
77+
78+
<p>For the same reason, we can’t omit the <code>&gt;</code> in front of subsequent lines of an indented or fenced code block:</p>
79+
80+
<blockquote>
81+
<pre><code>foo
82+
</code></pre>
83+
</blockquote>
84+
<pre><code>bar
85+
</code></pre>
86+
87+
<h3 id="example-200">Example 200</h3>
88+
89+
<pre><code>&gt; ```
90+
foo
91+
```
92+
93+
&lt;blockquote&gt;
94+
&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
95+
&lt;/blockquote&gt;
96+
&lt;p&gt;foo&lt;/p&gt;
97+
&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
98+
</code></pre>
99+
100+
<h3 id="example-201">Example 201</h3>
101+
<pre><code>&gt; foo
102+
- bar
103+
104+
&lt;blockquote&gt;
105+
&lt;p&gt;foo
106+
- bar&lt;/p&gt;
107+
&lt;/blockquote&gt;
108+
</code></pre>
109+
110+
<h3 id="example-202">Example 202</h3>
111+
112+
<p>A block quote can be empty:</p>
113+
114+
<blockquote>
115+
</blockquote>
116+
117+
<h3 id="example-203">Example 203</h3>
118+
119+
<blockquote>
120+
</blockquote>
121+
122+
<h3 id="example-204">Example 204</h3>
123+
124+
<p>A block quote can have initial or final blank lines:</p>
125+
126+
<blockquote>
127+
<p>foo</p>
128+
</blockquote>
129+
130+
131+
<h3 id="example-205">Example 205</h3>
132+
133+
<p>A blank line always separates block quotes:</p>
134+
135+
<blockquote>
136+
<p>foo</p>
137+
</blockquote>
138+
<blockquote>
139+
<p>bar</p>
140+
</blockquote>
141+
142+
<h3 id="example-206">Example 206</h3>
143+
144+
<p>Consecutiveness means that if we put these block quotes together, we get a single block quote:</p>
145+
146+
<blockquote>
147+
<p>foo
148+
bar</p>
149+
</blockquote>
150+
151+
<h3 id="example-207">Example 207</h3>
152+
153+
<p>To get a block quote with two paragraphs, use:</p>
154+
155+
<blockquote>
156+
<p>foo</p>
157+
<p>bar</p>
158+
</blockquote>
159+
160+
<h3 id="example-208">Example 208</h3>
161+
162+
<p>Block quotes can interrupt paragraphs:</p>
163+
164+
<p>foo</p>
165+
<blockquote>
166+
<p>bar</p>
167+
</blockquote>
168+
169+
<h3 id="example-209">Example 209</h3>
170+
171+
<p>In general, blank lines are not needed before or after block quotes:</p>
172+
173+
<blockquote>
174+
<p>aaa</p>
175+
</blockquote>
176+
<hr>
177+
<blockquote>
178+
<p>bbb</p>
179+
</blockquote>
180+
181+
<h3 id="example-210">Example 210</h3>
182+
183+
<p>However, because of laziness, a blank line is needed between a block quote and a following paragraph:</p>
184+
185+
<blockquote>
186+
<p>bar
187+
baz</p>
188+
</blockquote>
189+
190+
<h3 id="example-211">Example 211</h3>
191+
192+
<blockquote>
193+
<p>bar</p>
194+
</blockquote>
195+
<p>baz</p>
196+
197+
<h3 id="example-212">Example 212</h3>
198+
199+
<blockquote>
200+
<p>bar</p>
201+
</blockquote>
202+
<p>baz</p>
203+
204+
<h3 id="example-213">Example 213</h3>
205+
206+
<p>It is a consequence of the Laziness rule that any number of initial <code>&gt;</code>s may be omitted on a continuation line of a nested block quote:</p>
207+
208+
<blockquote>
209+
<blockquote>
210+
<blockquote>
211+
<p>foo
212+
bar</p>
213+
</blockquote>
214+
</blockquote>
215+
</blockquote>
216+
217+
<h3 id="example-214">Example 214</h3>
218+
219+
<blockquote>
220+
<blockquote>
221+
<blockquote>
222+
<p>foo
223+
bar
224+
baz</p>
225+
</blockquote>
226+
</blockquote>
227+
</blockquote>
228+
229+
<h3 id="example-215">Example 215</h3>
230+
231+
<p>When including an indented code block in a block quote, remember that the block quote marker includes both the <code>&gt;</code> and a following space. So five spaces are needed after the <code>&gt;</code>:</p>
232+
233+
<blockquote>
234+
<pre><code>code
235+
</code></pre>
236+
</blockquote>
237+
<blockquote>
238+
<p>not code</p>
239+
</blockquote>

0 commit comments

Comments
 (0)