tryMatch method

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

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(InlineParser parser, [int? startMatchPos]) {
  startMatchPos ??= parser.pos;
  final startMatch = pattern.matchAsPrefix(parser.source, startMatchPos);
  if (startMatch == null) {
    return false;
  }

  // When it is a link and it is not preceded by `*`, `_`, `~`, `(`, or `>`,
  // it is invalid. See
  // https://github.github.com/gfm/#extended-autolink-path-validation.
  if (startMatch[1] != null && parser.pos > 0) {
    final precededBy = String.fromCharCode(parser.charAt(parser.pos - 1));
    const validPrecedingChars = {' ', '*', '_', '~', '(', '>'};
    if (validPrecedingChars.contains(precededBy) == false) {
      return false;
    }
  }

  // When it is an email link and followed by `_` or `-`, it is invalid. See
  // https://github.github.com/gfm/#example-633
  if (startMatch[2] != null && parser.source.length > startMatch.end) {
    final followedBy = String.fromCharCode(parser.charAt(startMatch.end));
    const invalidFollowingChars = {'_', '-'};
    if (invalidFollowingChars.contains(followedBy)) {
      return false;
    }
  }

  parser.writeText();
  return onMatch(parser, startMatch);
}