Fixes dart-lang/markdowndart-lang/markdown#86. Linebreaks should not be treated as `TextSyntax` but should be a subclass of `InlineSyntax`. This also requires a small modification to HtmlRenderer to put a \n after each <br />; this makes all the unit tests pass.
diff --git a/pkgs/markdown/lib/src/html_renderer.dart b/pkgs/markdown/lib/src/html_renderer.dart index 184f9f9..03977ca 100644 --- a/pkgs/markdown/lib/src/html_renderer.dart +++ b/pkgs/markdown/lib/src/html_renderer.dart
@@ -85,6 +85,11 @@ if (element.isEmpty) { // Empty element like <hr/>. buffer.write(' />'); + + if (element.tag == 'br') { + buffer.write('\n'); + } + return false; } else { buffer.write('>');
diff --git a/pkgs/markdown/lib/src/inline_parser.dart b/pkgs/markdown/lib/src/inline_parser.dart index a008ca8..52ee24e 100644 --- a/pkgs/markdown/lib/src/inline_parser.dart +++ b/pkgs/markdown/lib/src/inline_parser.dart
@@ -14,6 +14,7 @@ static final List<InlineSyntax> _defaultSyntaxes = new List<InlineSyntax>.unmodifiable(<InlineSyntax>[ new AutolinkSyntax(), + new LineBreakSyntax(), new LinkSyntax(), new ImageLinkSyntax(), // Allow any punctuation to be escaped. @@ -29,10 +30,6 @@ new TextSyntax(r'&', sub: '&'), // Encode "<". (Why not encode ">" too? Gruber is toying with us.) new TextSyntax(r'<', sub: '<'), - // Escaped newlines become hard line breaks. - new TextSyntax(r'\\\n', sub: '<br />\n'), - // Two or more spaces at the end of a line become hard line breaks. - new TextSyntax(r' +\n', sub: '<br />\n'), // Parse "**strong**" tags. new TagSyntax(r'\*\*', tag: 'strong'), // Parse "__strong__" tags. @@ -191,6 +188,17 @@ bool onMatch(InlineParser parser, Match match); } +/// Represents a hard line break. +class LineBreakSyntax extends InlineSyntax { + LineBreakSyntax() : super(r'(?:\\| +)\n'); + + /// Create a void <br> element. + bool onMatch(InlineParser parser, Match match) { + parser.addNode(new Element.empty('br')); + return true; + } +} + /// Matches stuff that should just be passed through as straight text. class TextSyntax extends InlineSyntax { final String substitute;