Refactor CodeSyntax (dart-lang/markdown#487)
* Refactor CodeSyntax
* Update stats
diff --git a/pkgs/markdown/lib/src/inline_syntaxes/code_syntax.dart b/pkgs/markdown/lib/src/inline_syntaxes/code_syntax.dart
index afd6371..6eb22a8 100644
--- a/pkgs/markdown/lib/src/inline_syntaxes/code_syntax.dart
+++ b/pkgs/markdown/lib/src/inline_syntaxes/code_syntax.dart
@@ -46,9 +46,42 @@
@override
bool onMatch(InlineParser parser, Match match) {
- var code = match[2]!.trim().replaceAll('\n', ' ');
- if (parser.encodeHtml) code = escapeHtml(code);
+ final markerLength = match[1]!.length;
+ final contentLength = match.match.length - markerLength * 2;
+ final contentStart = parser.pos + markerLength;
+ final contentEnd = contentStart + contentLength;
+
+ var code = parser.source.substring(contentStart, contentEnd);
+ if (_shouldStrip(code)) {
+ code = code.substring(1, code.length - 1);
+ }
+ code = code.replaceAll('\n', ' ');
+
+ if (parser.encodeHtml) {
+ code = escapeHtml(code);
+ }
+
parser.addNode(Element.text('code', code));
+ return true;
+ }
+
+ bool _shouldStrip(String code) {
+ // No stripping occurs if the code span contains only spaces:
+ // https://spec.commonmark.org/0.30/#example-334.
+ if (code.trim().isEmpty) {
+ return false;
+ }
+
+ // Only spaces, and not unicode whitespace in general, are stripped in this
+ // way, see https://spec.commonmark.org/0.30/#example-333.
+ final startsWithSpace = code.startsWith(' ') || code.startsWith('\n');
+ final endsWithSpace = code.endsWith(' ') || code.endsWith('\n');
+
+ // The stripping only happens if the space is on both sides of the string:
+ // https://spec.commonmark.org/0.30/#example-332.
+ if (!startsWithSpace || !endsWithSpace) {
+ return false;
+ }
return true;
}
diff --git a/pkgs/markdown/test/common_mark/code_spans.unit b/pkgs/markdown/test/common_mark/code_spans.unit
index 4c664ab..f0abc0f 100644
--- a/pkgs/markdown/test/common_mark/code_spans.unit
+++ b/pkgs/markdown/test/common_mark/code_spans.unit
@@ -13,21 +13,21 @@
>>> Code spans - 331
` `` `
<<<
-<p><code>``</code></p>
+<p><code> `` </code></p>
>>> Code spans - 332
` a`
<<<
-<p><code>a</code></p>
+<p><code> a</code></p>
>>> Code spans - 333
` b `
<<<
-<p><code>b</code></p>
+<p><code> b </code></p>
>>> Code spans - 334
` `
` `
<<<
-<p><code></code>
-<code></code></p>
+<p><code> </code>
+<code> </code></p>
>>> Code spans - 335
``
foo
@@ -41,7 +41,7 @@
foo
``
<<<
-<p><code>foo</code></p>
+<p><code>foo </code></p>
>>> Code spans - 337
`foo bar
baz`
diff --git a/pkgs/markdown/test/common_mark/fenced_code_blocks.unit b/pkgs/markdown/test/common_mark/fenced_code_blocks.unit
index 6148ae3..06dec58 100644
--- a/pkgs/markdown/test/common_mark/fenced_code_blocks.unit
+++ b/pkgs/markdown/test/common_mark/fenced_code_blocks.unit
@@ -163,7 +163,7 @@
``` ```
aaa
<<<
-<p><code></code>
+<p><code> </code>
aaa</p>
>>> Fenced code blocks - 139
~~~~~~
diff --git a/pkgs/markdown/test/gfm/code_spans.unit b/pkgs/markdown/test/gfm/code_spans.unit
index ebf1f64..68a4b3a 100644
--- a/pkgs/markdown/test/gfm/code_spans.unit
+++ b/pkgs/markdown/test/gfm/code_spans.unit
@@ -13,21 +13,21 @@
>>> Code spans - 341
` `` `
<<<
-<p><code>``</code></p>
+<p><code> `` </code></p>
>>> Code spans - 342
` a`
<<<
-<p><code>a</code></p>
+<p><code> a</code></p>
>>> Code spans - 343
` b `
<<<
-<p><code>b</code></p>
+<p><code> b </code></p>
>>> Code spans - 344
` `
` `
<<<
-<p><code></code>
-<code></code></p>
+<p><code> </code>
+<code> </code></p>
>>> Code spans - 345
``
foo
@@ -41,7 +41,7 @@
foo
``
<<<
-<p><code>foo</code></p>
+<p><code>foo </code></p>
>>> Code spans - 347
`foo bar
baz`
diff --git a/pkgs/markdown/test/gfm/fenced_code_blocks.unit b/pkgs/markdown/test/gfm/fenced_code_blocks.unit
index dd13ca0..f6fa843 100644
--- a/pkgs/markdown/test/gfm/fenced_code_blocks.unit
+++ b/pkgs/markdown/test/gfm/fenced_code_blocks.unit
@@ -163,7 +163,7 @@
``` ```
aaa
<<<
-<p><code></code>
+<p><code> </code>
aaa</p>
>>> Fenced code blocks - 109
~~~~~~
diff --git a/pkgs/markdown/tool/common_mark_stats.json b/pkgs/markdown/tool/common_mark_stats.json
index 65181ec..1cfe950 100644
--- a/pkgs/markdown/tool/common_mark_stats.json
+++ b/pkgs/markdown/tool/common_mark_stats.json
@@ -89,12 +89,12 @@
"328": "strict",
"329": "strict",
"330": "strict",
- "331": "loose",
- "332": "loose",
- "333": "loose",
- "334": "loose",
+ "331": "strict",
+ "332": "strict",
+ "333": "strict",
+ "334": "strict",
"335": "strict",
- "336": "loose",
+ "336": "strict",
"337": "strict",
"338": "strict",
"339": "strict",
@@ -281,7 +281,7 @@
"135": "strict",
"136": "strict",
"137": "strict",
- "138": "loose",
+ "138": "strict",
"139": "strict",
"140": "strict",
"141": "strict",
diff --git a/pkgs/markdown/tool/common_mark_stats.txt b/pkgs/markdown/tool/common_mark_stats.txt
index 72b98de..a42dfb3 100644
--- a/pkgs/markdown/tool/common_mark_stats.txt
+++ b/pkgs/markdown/tool/common_mark_stats.txt
@@ -25,4 +25,4 @@
3 of 3 – 100.0% Textual content
19 of 19 – 100.0% Thematic breaks
633 of 652 – 97.1% TOTAL
- 604 of 633 – 95.4% TOTAL Strict
+ 610 of 633 – 96.4% TOTAL Strict
diff --git a/pkgs/markdown/tool/gfm_stats.json b/pkgs/markdown/tool/gfm_stats.json
index 657aaff..5e5aae3 100644
--- a/pkgs/markdown/tool/gfm_stats.json
+++ b/pkgs/markdown/tool/gfm_stats.json
@@ -102,12 +102,12 @@
"338": "strict",
"339": "strict",
"340": "strict",
- "341": "loose",
- "342": "loose",
- "343": "loose",
- "344": "loose",
+ "341": "strict",
+ "342": "strict",
+ "343": "strict",
+ "344": "strict",
"345": "strict",
- "346": "loose",
+ "346": "strict",
"347": "strict",
"348": "strict",
"349": "strict",
@@ -297,7 +297,7 @@
"105": "strict",
"106": "strict",
"107": "strict",
- "108": "loose",
+ "108": "strict",
"109": "strict",
"110": "strict",
"111": "strict",
diff --git a/pkgs/markdown/tool/gfm_stats.txt b/pkgs/markdown/tool/gfm_stats.txt
index aea55ba..cc1d67f 100644
--- a/pkgs/markdown/tool/gfm_stats.txt
+++ b/pkgs/markdown/tool/gfm_stats.txt
@@ -29,4 +29,4 @@
3 of 3 – 100.0% Textual content
19 of 19 – 100.0% Thematic breaks
651 of 671 – 97.0% TOTAL
- 621 of 651 – 95.4% TOTAL Strict
+ 627 of 651 – 96.3% TOTAL Strict