linkDefinitionAt function
A link reference definition occupying the whole of line, or null.
[label]: destination "optional title" — up to three spaces of indent, a
label that is not blank and holds no unescaped bracket, and nothing after
the title. A label starting with ^ is a footnote, not a link.
Implementation
({String label, MdLinkDefinition definition})? linkDefinitionAt(String line) {
if (!startsAfterIndent(line, 0x5B /* [ */)) {
return null;
}
final t = line.trim();
if (t.length < 4 || t.codeUnitAt(0) != 0x5B /* [ */ ) {
return null;
}
final close = _labelEnd(t, 1);
if (close == -1 || close + 1 >= t.length || t[close + 1] != ':') {
return null;
}
final label = t.substring(1, close);
if (label.trim().isEmpty || label.startsWith('^') || label.length > 999) {
return null;
}
final target = parseLinkTarget(t.substring(close + 2), strict: true);
if (target == null || target.url.isEmpty) {
return null;
}
return (
label: label,
definition: MdLinkDefinition(url: target.url, title: target.title),
);
}