tryMatch method

  1. @override
bool tryMatch(
  1. InlineParser parser, [
  2. int? startMatchPos
])

Tries to match at the parser's current position.

The parser's position can be overriden with startMatchPos. Returns whether or not the pattern successfully matched.

Implementation

@override
bool tryMatch(md.InlineParser parser, [int? startMatchPos]) {
  final match = pattern.matchAsPrefix(
    parser.source,
    startMatchPos ?? parser.pos,
  );
  if (match == null) return false;
  final source = parser.source;
  final references = parser.document.linkReferences;
  // a reference link such as [1]: https://... is a link, not a citation
  if (references.containsKey(match[1])) return false;
  // after "]" only when that closes a citation too: [1][2], not [text][2]
  if (match.start > 0 &&
      source[match.start - 1] == ']' &&
      !_citationBefore.hasMatch(source.substring(0, match.start))) {
    return false;
  }
  // before "[" only when that is a citation and not the name of a
  // reference: with "[2]: url" defined, [1][2] is a link
  if (source.startsWith('[', match.end)) {
    final next = _citationAfter.matchAsPrefix(source, match.end);
    if (next == null || references.containsKey(next[1])) return false;
  }
  return super.tryMatch(parser, startMatchPos);
}