scanFootnoteReferences function

void scanFootnoteReferences(
  1. String line,
  2. Set<String> referenced
)

Adds the label of every [^label] reference in line to referenced, in order — outside code spans, and not escaped, and not [^x](url), which is a link.

Implementation

void scanFootnoteReferences(String line, Set<String> referenced) {
  if (!line.contains('[^')) {
    return;
  }
  var i = 0;
  while (i < line.length) {
    final c = line.codeUnitAt(i);
    if (c == 0x5C /* \ */ ) {
      i += 2;
      continue;
    }
    if (c == 0x60 /* ` */ ) {
      final span = codeSpanAt(line, i);
      i = span != null ? span.close + span.run : i + backtickRunAt(line, i);
      continue;
    }
    if (c == _openBracket &&
        i + 1 < line.length &&
        line.codeUnitAt(i + 1) == 0x5E /* ^ */ ) {
      final close = line.indexOf(']', i + 2);
      if (close > i + 2) {
        final label = line.substring(i + 2, close);
        final isLink =
            close + 1 < line.length && line.codeUnitAt(close + 1) == 0x28;
        if (!isLink && !label.contains(RegExp(r'[\s\[]'))) {
          referenced.add(normalizeLabel(label));
          i = close + 1;
          continue;
        }
      }
    }
    i += 1;
  }
}