Properly normalize a collapsed reference link; fixes #281 (#282)
diff --git a/lib/src/block_parser.dart b/lib/src/block_parser.dart
index 151624d..b488093 100644
--- a/lib/src/block_parser.dart
+++ b/lib/src/block_parser.dart
@@ -34,9 +34,6 @@
/// SETEXT should win.
final _hrPattern = RegExp(r'^ {0,3}([-*_])[ \t]*\1[ \t]*\1(?:\1|[ \t])*$');
-/// One or more whitespace, for compressing.
-final _oneOrMoreWhitespacePattern = RegExp('[ \n\r\t]+');
-
/// A line starting with one of these markers: `-`, `*`, `+`. May have up to
/// three leading spaces before the marker and any number of spaces or tabs
/// after.
@@ -1100,8 +1097,7 @@
}
// References are case-insensitive, and internal whitespace is compressed.
- label =
- label.toLowerCase().trim().replaceAll(_oneOrMoreWhitespacePattern, ' ');
+ label = normalizeLinkLabel(label);
parser.document.linkReferences
.putIfAbsent(label, () => LinkReference(label, destination, title));
diff --git a/lib/src/inline_parser.dart b/lib/src/inline_parser.dart
index bb0d650..e416a9b 100644
--- a/lib/src/inline_parser.dart
+++ b/lib/src/inline_parser.dart
@@ -791,8 +791,7 @@
TagState state,
Map<String, LinkReference> linkReferences,
) {
- var normalizedLabel = label.toLowerCase();
- var linkReference = linkReferences[normalizedLabel];
+ var linkReference = linkReferences[normalizeLinkLabel(label)];
if (linkReference != null) {
return _createNode(state, linkReference.destination, linkReference.title);
} else {
diff --git a/lib/src/util.dart b/lib/src/util.dart
index ea17094..8a53657 100644
--- a/lib/src/util.dart
+++ b/lib/src/util.dart
@@ -8,9 +8,10 @@
String escapeHtmlAttribute(String text) =>
const HtmlEscape(HtmlEscapeMode.attribute).convert(text);
-// Escape the contents of [value], so that it may be used as an HTML attribute.
-
-// Based on http://spec.commonmark.org/0.28/#backslash-escapes.
+/// Escapes the contents of [value], so that it may be used as an HTML
+/// attribute.
+///
+/// Based on http://spec.commonmark.org/0.28/#backslash-escapes.
String escapeAttribute(String value) {
var result = StringBuffer();
int ch;
@@ -72,3 +73,12 @@
}
return result.toString();
}
+
+/// One or more whitespace, for compressing.
+final _oneOrMoreWhitespacePattern = RegExp('[ \n\r\t]+');
+
+/// "Normalizes" a link label, according to the [CommonMark spec].
+///
+/// [CommonMark spec] https://spec.commonmark.org/0.29/#link-label
+String normalizeLinkLabel(String label) =>
+ label.trim().replaceAll(_oneOrMoreWhitespacePattern, ' ').toLowerCase();
diff --git a/test/original/reference_links.unit b/test/original/reference_links.unit
index fe95fad..bc3e336 100644
--- a/test/original/reference_links.unit
+++ b/test/original/reference_links.unit
@@ -89,4 +89,12 @@
[coverage_page]:https://coveralls.io/github/yeradis/stay_points.dart?branch=master
[coverage_status]: https://coveralls.io/repos/github/yeradis/stay_points.dart/badge.svg?branch=master
<<<
-<p><a href="https://coveralls.io/github/yeradis/stay_points.dart?branch=master"><img src="https://coveralls.io/repos/github/yeradis/stay_points.dart/badge.svg?branch=master" alt="Coverage Status" /></a></p>
\ No newline at end of file
+<p><a href="https://coveralls.io/github/yeradis/stay_points.dart?branch=master"><img src="https://coveralls.io/repos/github/yeradis/stay_points.dart/badge.svg?branch=master" alt="Coverage Status" /></a></p>
+>>> compressed reference link label is normalized
+Text [foo
+bar][].
+
+[foo bar]: http://bar.com
+<<<
+<p>Text <a href="http://bar.com">foo
+bar</a>.</p>