Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[commonmark] Make blockquotes commonmark compliant #1023

Merged
merged 5 commits into from
Jan 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ var block = {
hr: /^( *[-*_]){3,} *(?:\n+|$)/,
heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,
nptable: noop,
lheading: /^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/,
blockquote: /^( *>[^\n]+(\n(?!def)[^\n]+)*\n*)+/,
blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
list: /^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
html: /^ *(?:comment *(?:\n|\s*$)|closed *(?:\n{2,}|\s*$)|closing *(?:\n{2,}|\s*$))/,
def: /^ {0,3}\[(label)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)(title))? *(?:\n+|$)/,
table: noop,
paragraph: /^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag))+)\n*/,
lheading: /^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/,
paragraph: /^([^\n]+(?:\n?(?!hr|heading|lheading| {0,3}>|tag)[^\n]+)+)/,
text: /^[^\n]+/
};

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

block.blockquote = replace(block.blockquote)
('def', block.def)
();

block._tag = '(?!(?:'
+ 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code'
+ '|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo'
Expand All @@ -67,10 +63,13 @@ block.paragraph = replace(block.paragraph)
('hr', block.hr)
('heading', block.heading)
('lheading', block.lheading)
('blockquote', block.blockquote)
('tag', '<' + block._tag)
();

block.blockquote = replace(block.blockquote)
('paragraph', block.paragraph)
();

/**
* Normal Block Grammar
*/
Expand Down Expand Up @@ -245,17 +244,6 @@ Lexer.prototype.token = function(src, top) {
continue;
}

// lheading
if (cap = this.rules.lheading.exec(src)) {
src = src.substring(cap[0].length);
this.tokens.push({
type: 'heading',
depth: cap[2] === '=' ? 1 : 2,
text: cap[1]
});
continue;
}

// hr
if (cap = this.rules.hr.exec(src)) {
src = src.substring(cap[0].length);
Expand Down Expand Up @@ -423,6 +411,17 @@ Lexer.prototype.token = function(src, top) {
continue;
}

// lheading
if (cap = this.rules.lheading.exec(src)) {
src = src.substring(cap[0].length);
this.tokens.push({
type: 'heading',
depth: cap[2] === '=' ? 1 : 2,
text: cap[1]
});
continue;
}

// top-level paragraph
if (top && (cap = this.rules.paragraph.exec(src))) {
src = src.substring(cap[0].length);
Expand Down
239 changes: 239 additions & 0 deletions test/new/cm_blockquotes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
<h3 id="example-191">Example 191</h3>

<blockquote>
<h1 id="foo">Foo</h1>
<p>bar
baz</p>
</blockquote>

<h3 id="example-192">Example 192</h3>

<p>The spaces after the <code>&gt;</code> characters can be omitted:</p>

<blockquote>
<h1 id="foo">Foo</h1>
<p>bar
baz</p>
</blockquote>

<h3 id="example-193">Example 193</h3>

<p>The <code>&gt;</code> characters can be indented 1-3 spaces:</p>

<blockquote>
<h1 id="foo">Foo</h1>
<p>bar
baz</p>
</blockquote>

<h3 id="example-194">Example 194</h3>

<p>Four spaces gives us a code block:</p>

<pre><code>&gt; # Foo
&gt; bar
&gt; baz
</code></pre>

<h3 id="example-195">Example 195</h3>

<p>The Laziness clause allows us to omit the <code>&gt;</code> before paragraph continuation text:</p>

<blockquote>
<h1 id="foo">Foo</h1>
<p>bar
baz</p>
</blockquote>

<h3 id="example-196">Example 196</h3>

<p>A block quote can contain some lazy and some non-lazy continuation lines:</p>

<blockquote>
<p>bar
baz
foo</p>
</blockquote>

<h3 id="example-197">Example 197</h3>

<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>

<blockquote>
<p>foo</p>
</blockquote>
<hr>

<p>without changing the meaning.</p>

<h3 id="example-198">Example 198</h3>

<pre><code>Similarly, if we omit the `&gt;` in the second line then the block quote ends after the first line:

&gt; - foo
- bar</code></pre>

<h3 id="example-199">Example 199</h3>

<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>

<blockquote>
<pre><code>foo
</code></pre>
</blockquote>
<pre><code>bar
</code></pre>

<h3 id="example-200">Example 200</h3>

<pre><code>&gt; ```
foo
```

&lt;blockquote&gt;
&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;foo&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
</code></pre>

<h3 id="example-201">Example 201</h3>
<pre><code>&gt; foo
- bar

&lt;blockquote&gt;
&lt;p&gt;foo
- bar&lt;/p&gt;
&lt;/blockquote&gt;
</code></pre>

<h3 id="example-202">Example 202</h3>

<p>A block quote can be empty:</p>

<blockquote>
</blockquote>

<h3 id="example-203">Example 203</h3>

<blockquote>
</blockquote>

<h3 id="example-204">Example 204</h3>

<p>A block quote can have initial or final blank lines:</p>

<blockquote>
<p>foo</p>
</blockquote>


<h3 id="example-205">Example 205</h3>

<p>A blank line always separates block quotes:</p>

<blockquote>
<p>foo</p>
</blockquote>
<blockquote>
<p>bar</p>
</blockquote>

<h3 id="example-206">Example 206</h3>

<p>Consecutiveness means that if we put these block quotes together, we get a single block quote:</p>

<blockquote>
<p>foo
bar</p>
</blockquote>

<h3 id="example-207">Example 207</h3>

<p>To get a block quote with two paragraphs, use:</p>

<blockquote>
<p>foo</p>
<p>bar</p>
</blockquote>

<h3 id="example-208">Example 208</h3>

<p>Block quotes can interrupt paragraphs:</p>

<p>foo</p>
<blockquote>
<p>bar</p>
</blockquote>

<h3 id="example-209">Example 209</h3>

<p>In general, blank lines are not needed before or after block quotes:</p>

<blockquote>
<p>aaa</p>
</blockquote>
<hr>
<blockquote>
<p>bbb</p>
</blockquote>

<h3 id="example-210">Example 210</h3>

<p>However, because of laziness, a blank line is needed between a block quote and a following paragraph:</p>

<blockquote>
<p>bar
baz</p>
</blockquote>

<h3 id="example-211">Example 211</h3>

<blockquote>
<p>bar</p>
</blockquote>
<p>baz</p>

<h3 id="example-212">Example 212</h3>

<blockquote>
<p>bar</p>
</blockquote>
<p>baz</p>

<h3 id="example-213">Example 213</h3>

<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>

<blockquote>
<blockquote>
<blockquote>
<p>foo
bar</p>
</blockquote>
</blockquote>
</blockquote>

<h3 id="example-214">Example 214</h3>

<blockquote>
<blockquote>
<blockquote>
<p>foo
bar
baz</p>
</blockquote>
</blockquote>
</blockquote>

<h3 id="example-215">Example 215</h3>

<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>

<blockquote>
<pre><code>code
</code></pre>
</blockquote>
<blockquote>
<p>not code</p>
</blockquote>
Loading