Merge pull request dart-lang/markdown#103 from srawlins/loose-lists
Using CommonMark's 'tight' and 'loose' lists
diff --git a/pkgs/markdown/lib/src/block_parser.dart b/pkgs/markdown/lib/src/block_parser.dart
index 6111f55..6e1cf9f 100644
--- a/pkgs/markdown/lib/src/block_parser.dart
+++ b/pkgs/markdown/lib/src/block_parser.dart
@@ -219,7 +219,14 @@
bool canParse(BlockParser parser) {
// Note: matches *next* line, not the current one. We're looking for the
// underlining after this line.
- return parser.matchesNext(_setextPattern);
+ return parser.matchesNext(_setextPattern) &&
+ // The current line must look like a paragraph.
+ !(parser.matches(_codePattern) ||
+ parser.matches(_headerPattern) ||
+ parser.matches(_blockquotePattern) ||
+ parser.matches(_hrPattern) ||
+ parser.matches(_ulPattern) ||
+ parser.matches(_olPattern));
}
Node parse(BlockParser parser) {
@@ -278,6 +285,36 @@
const BlockquoteSyntax();
+ List<String> parseChildLines(BlockParser parser) {
+ // Grab all of the lines that form the blockquote, stripping off the ">".
+ var childLines = <String>[];
+
+ while (!parser.isDone) {
+ var match = pattern.firstMatch(parser.current);
+ if (match != null) {
+ childLines.add(match[1]);
+ parser.advance();
+ continue;
+ }
+
+ // A paragraph continuation is OK. This is content that cannot be parsed
+ // as any other syntax except Paragraph, and it doesn't match the bar in
+ // a Setext header.
+ if (parser.blockSyntaxes.firstWhere((s) => s.canParse(parser))
+ is ParagraphSyntax) {
+ var continuedLine = childLines.last + parser.current;
+ childLines
+ ..removeLast()
+ ..add(continuedLine);
+ parser.advance();
+ } else {
+ break;
+ }
+ }
+
+ return childLines;
+ }
+
Node parse(BlockParser parser) {
var childLines = parseChildLines(parser);
@@ -547,11 +584,15 @@
// Done with the list.
break;
} else {
- // Anything else is paragraph text or other stuff that can be in a list
- // item. However, if the previous item is a blank line, this means we're
- // done with the list and are starting a new top-level paragraph.
- if ((childLines.length > 0) && (childLines.last == '')) break;
- childLines.add(parser.current);
+ // If the previous item is a blank line, this means we're done with the
+ // list and are starting a new top-level paragraph.
+ if ((childLines.isNotEmpty) && (childLines.last == '')) break;
+
+ // Anything else is paragraph continuation text.
+ var continuedLine = childLines.last + parser.current;
+ childLines
+ ..removeLast()
+ ..add(continuedLine);
}
parser.advance();
}
diff --git a/pkgs/markdown/test/original/block_quotes.unit b/pkgs/markdown/test/original/block_quotes.unit
index 26d8ed7..14107ef 100644
--- a/pkgs/markdown/test/original/block_quotes.unit
+++ b/pkgs/markdown/test/original/block_quotes.unit
@@ -25,3 +25,18 @@
<p>two</p>
<blockquote>
<p>three</p></blockquote></blockquote></blockquote>
+>>> quote turns what might be an h1 into nothing
+> quote
+===
+
+<<<
+<blockquote>
+<p>quote===</p></blockquote>
+>>> quote turns what might be an h2 into an hr
+> quote
+---
+
+<<<
+<blockquote>
+<p>quote</p></blockquote>
+<hr />
diff --git a/pkgs/markdown/test/original/setext_headers.unit b/pkgs/markdown/test/original/setext_headers.unit
index 6e610cf..65038f0 100644
--- a/pkgs/markdown/test/original/setext_headers.unit
+++ b/pkgs/markdown/test/original/setext_headers.unit
@@ -20,27 +20,3 @@
<<<
<p>-</p>
->>> h1 turns preceding list into text
-- list
-===
-
-<<<
-<h1>- list</h1>
->>> h2 turns preceding list into text
-- list
-===
-
-<<<
-<h1>- list</h1>
->>> h1 turns preceding blockquote into text
-> quote
-===
-
-<<<
-<h1>> quote</h1>
->>> h2 turns preceding blockquote into text
-> quote
-===
-
-<<<
-<h1>> quote</h1>
diff --git a/pkgs/markdown/test/original/unordered_lists.unit b/pkgs/markdown/test/original/unordered_lists.unit
index db40868..4729287 100644
--- a/pkgs/markdown/test/original/unordered_lists.unit
+++ b/pkgs/markdown/test/original/unordered_lists.unit
@@ -80,3 +80,16 @@
<ul><li>
<p>one</p><ul><li>nested one</li><li>nested two</li></ul></li><li>
<p>two</p></li></ul>
+>>> list item turns what might be an h1 into nothing
+- list
+===
+
+<<<
+<ul><li>list===</li></ul>
+>>> list item turns what might be an h2 into nothing
+- list
+---
+
+<<<
+<ul><li>list</li></ul>
+<hr />