Merge pull request dart-lang/markdown#94 from srawlins/shortcut-reference-links
Support shortuct reference links and images
diff --git a/pkgs/markdown/lib/src/inline_parser.dart b/pkgs/markdown/lib/src/inline_parser.dart
index 5875094..d8b8c0d 100644
--- a/pkgs/markdown/lib/src/inline_parser.dart
+++ b/pkgs/markdown/lib/src/inline_parser.dart
@@ -308,12 +308,16 @@
: super(pattern, end: linkPattern);
Node createNode(InlineParser parser, Match match, TagState state) {
- // If we didn't match refLink or inlineLink, then it means there was
- // nothing after the first square bracket, so it isn't a normal Markdown
- // link at all. Instead, we allow users of the library to specify a special
- // resolver function ([linkResolver]) that may choose to handle
- // this. Otherwise, it's just treated as plain text.
if (match[1] == null) {
+ // Try for a shortcut reference link, like `[foo]`.
+ var element = _createElement(parser, match, state);
+ if (element != null) return element;
+
+ // If we didn't match refLink or inlineLink, and it's not a _shortcut_
+ // reflink, then it means it isn't a normal Markdown link at all. Instead,
+ // we allow users of the library to specify a special resolver function
+ // ([linkResolver]) that may choose to handle this. Otherwise, it's just
+ // treated as plain text.
if (linkResolver == null) return null;
// Treat the contents as unparsed text even if they happen to match. This
@@ -362,10 +366,18 @@
return new Link(null, url, title);
} else {
var id;
+ String _contents() {
+ int offset = pattern.pattern.length - 1;
+ return parser.source.substring(state.startPos + offset, parser.pos);
+ }
// Reference link like [foo][bar].
- if (match[2] == '') {
+ if (match[1] == null) {
+ // There are no reference brackets ("shortcut reference link"), so infer
+ // the id from the contents.
+ id = _contents();
+ } else if (match[2] == '') {
// The id is empty ("[]") so infer it from the contents.
- id = parser.source.substring(state.startPos + 1, parser.pos);
+ id = _contents();
} else {
id = match[2];
}
diff --git a/pkgs/markdown/test/original/reference_images.unit b/pkgs/markdown/test/original/reference_images.unit
index 4f00072..b060da0 100644
--- a/pkgs/markdown/test/original/reference_images.unit
+++ b/pkgs/markdown/test/original/reference_images.unit
@@ -22,3 +22,9 @@
<<<
<p><img src="http://foo.com/foo.png" title="optional title" /></p>
+>>> shortcut reference image
+![foo]
+
+[foo]: http://foo.com/foo.png
+<<<
+<p><img alt="foo" src="http://foo.com/foo.png" /></p>
diff --git a/pkgs/markdown/test/original/reference_links.unit b/pkgs/markdown/test/original/reference_links.unit
index 74735cd..0449da7 100644
--- a/pkgs/markdown/test/original/reference_links.unit
+++ b/pkgs/markdown/test/original/reference_links.unit
@@ -57,3 +57,9 @@
<<<
<p>links <a href="http://foo.com">ARE</a> awesome</p>
+>>> shortcut reference links
+links [are] awesome
+
+[are]: http://foo.com
+<<<
+<p>links <a href="http://foo.com">are</a> awesome</p>