onMatchEnd method

  1. @override
bool onMatchEnd(
  1. InlineParser parser,
  2. Match match,
  3. TagState state
)
override

Implementation

@override
bool onMatchEnd(InlineParser parser, Match match, TagState state) {
  if (!_pendingStatesAreActive) {
    return false;
  }

  final text = parser.source.substring(state.endPos, parser.pos);
  // The current character is the `]` that closed the link text. Examine the
  // next character, to determine what type of link we might have (a '('
  // means a possible inline link; otherwise a possible reference link).
  if (parser.pos + 1 >= parser.source.length) {
    // In this case, the Markdown document may have ended with a shortcut
    // reference link.

    return _tryAddReferenceLink(parser, state, text);
  }
  // Peek at the next character; don't advance, so as to avoid later stepping
  // backward.
  final char = parser.charAt(parser.pos + 1);

  if (char == $lparen) {
    // Maybe an inline link, like `[text](destination)`.
    parser.advanceBy(1);
    final leftParenIndex = parser.pos;
    final inlineLink = _parseInlineLink(parser);
    if (inlineLink != null) {
      return _tryAddInlineLink(parser, state, inlineLink);
    }

    // Reset the parser position.
    parser
      ..pos = leftParenIndex

      // At this point, we've matched `[...](`, but that `(` did not pan out
      // to be an inline link. We must now check if `[...]` is simply a
      // shortcut reference link.
      ..advanceBy(-1);
    return _tryAddReferenceLink(parser, state, text);
  }

  if (char == $lbracket) {
    parser.advanceBy(1);
    // At this point, we've matched `[...][`. Maybe a *full* reference link,
    // like `[foo][bar]` or a *collapsed* reference link, like `[foo][]`.
    if (parser.pos + 1 < parser.source.length && parser.charAt(parser.pos + 1) == $rbracket) {
      // That opening `[` is not actually part of the link. Maybe a
      // *shortcut* reference link (followed by a `[`).
      parser.advanceBy(1);
      return _tryAddReferenceLink(parser, state, text);
    }
    final label = _parseReferenceLinkLabel(parser);
    if (label != null) {
      return _tryAddReferenceLink(parser, state, label);
    }
    return false;
  }

  // The link text (inside `[...]`) was not followed with a opening `(` nor
  // an opening `[`. Perhaps just a simple shortcut reference link (`[...]`).

  return _tryAddReferenceLink(parser, state, text);
}