parseLabel method

bool parseLabel()

Parses the link label, returns true if there is a valid link label.

Implementation

bool parseLabel() {
  moveThroughWhitespace(multiLine: true);

  if (length - pos < 2) {
    return false;
  }

  if (charAt() != $lbracket) {
    return false;
  }

  // Advance past the opening `[`.
  advance();
  final start = pos;

  // A link label can have at most 999 characters inside the square brackets.
  // See https://spec.commonmark.org/0.30/#link-label.
  var maxLoop = 999;
  while (true) {
    if (maxLoop-- < 0) {
      return false;
    }
    final char = charAt(pos);
    if (char == $backslash) {
      advance();
    } else if (char == $lbracket) {
      return false;
    } else if (char == $rbracket) {
      break;
    }
    advance();
    if (isDone) {
      return false;
    }
  }

  final text = substring(start, pos);
  if (text.isBlank) {
    return false;
  }

  // Advance past the closing `]`.
  advance();
  _label = text;
  return true;
}