isHeading function

({String content, int level})? isHeading(
  1. String trimmed
)

ATX heading: 1–6 # followed by a space or tab, or ending the line — # alone is an empty heading. Returns (level, content), with the optional closing sequence (## Title ##) removed.

Implementation

({int level, String content})? isHeading(String trimmed) {
  var level = 0;
  while (level < trimmed.length && trimmed.codeUnitAt(level) == _hash) {
    level += 1;
  }
  if (level < 1 || level > 6) {
    return null;
  }
  if (level == trimmed.length) {
    return (level: level, content: '');
  }
  final after = trimmed.codeUnitAt(level);
  if (after != _space && after != _tab) {
    return null;
  }
  return (
    level: level,
    content: stripClosingHashes(trimmed.substring(level + 1).trim()),
  );
}