Update table syntax to fit GitHub better
diff --git a/pkgs/markdown/lib/src/block_parser.dart b/pkgs/markdown/lib/src/block_parser.dart index 6d9924d..280411f 100644 --- a/pkgs/markdown/lib/src/block_parser.dart +++ b/pkgs/markdown/lib/src/block_parser.dart
@@ -49,7 +49,8 @@ new RegExp(r'^([ ]{0,3})(\d{1,9})([\.)])(([ \t])([ \t]*)(.*))?$'); /// A line of hyphens separated by at least one pipe. -final _tablePattern = new RegExp(r'^[ ]{0,3}\|?(:?\-+:?\|)+(:?\-+:?)?$'); +final _tablePattern = + new RegExp(r'^[ ]{0,3}\|?( *:?\-+:? *\|)+( *:?\-+:? *)?$'); /// Maintains the internal state needed to parse a series of lines into blocks /// of Markdown suitable for further inline parsing. @@ -763,6 +764,8 @@ /// Parses tables. class TableSyntax extends BlockSyntax { static final _pipePattern = new RegExp(r'\s*\|\s*'); + static final _openingPipe = new RegExp(r'^\|\s*'); + static final _closingPipe = new RegExp(r'\s*\|$'); bool get canEndBlock => false; @@ -796,10 +799,9 @@ } List<String> parseAlignments(String line) { - line = line - .replaceFirst(new RegExp(r'^\|'), '') - .replaceFirst(new RegExp(r'\|$'), ''); + line = line.replaceFirst(_openingPipe, '').replaceFirst(_closingPipe, ''); return line.split('|').map((column) { + column = column.trim(); if (column.startsWith(':') && column.endsWith(':')) return 'center'; if (column.startsWith(':')) return 'left'; if (column.endsWith(':')) return 'right'; @@ -809,8 +811,8 @@ Node parseRow(BlockParser parser, List<String> alignments, String cellType) { var line = parser.current - .replaceFirst(new RegExp(r'^\|\s*'), '') - .replaceFirst(new RegExp(r'\s*\|$'), ''); + .replaceFirst(_openingPipe, '') + .replaceFirst(_closingPipe, ''); var cells = line.split(_pipePattern); parser.advance(); var row = <Element>[];
diff --git a/pkgs/markdown/test/extensions/tables.unit b/pkgs/markdown/test/extensions/tables.unit index c1d6934..c3fa911 100644 --- a/pkgs/markdown/test/extensions/tables.unit +++ b/pkgs/markdown/test/extensions/tables.unit
@@ -20,6 +20,13 @@ <<< <table><thead><tr><th>head</th><th>cells</th></tr></thead><tbody><tr><td>body</td><td>cells</td></tr></tbody></table> +>>> rows wrapped in pipes, whitespace alignment row +| head | cells | +| -- | --- | +| body | cells | + +<<< +<table><thead><tr><th>head</th><th>cells</th></tr></thead><tbody><tr><td>body</td><td>cells</td></tr></tbody></table> >>> cells with inline syntax head `code` | _cells_ ------------|-------- @@ -61,3 +68,11 @@ <<< <table><thead><tr><th style="text-align: left;">head</th><th style="text-align: center;">cells</th><th style="text-align: right;">here</th></tr></thead><tbody><tr><td style="text-align: left;">body</td><td style="text-align: center;">cells</td><td style="text-align: right;">here</td></tr><tr><td style="text-align: left;">too</td><td style="text-align: center;">many</td><td style="text-align: right;">cells</td><td>here</td></tr></tbody></table> +>>> left, center, and right alignment, with whitespace +head | cells | here + :-- | :---: | ---: +body | cells | here +too | many | cells | here + +<<< +<table><thead><tr><th style="text-align: left;">head</th><th style="text-align: center;">cells</th><th style="text-align: right;">here</th></tr></thead><tbody><tr><td style="text-align: left;">body</td><td style="text-align: center;">cells</td><td style="text-align: right;">here</td></tr><tr><td style="text-align: left;">too</td><td style="text-align: center;">many</td><td style="text-align: right;">cells</td><td>here</td></tr></tbody></table>