stripClosingHashes function

String stripClosingHashes(
  1. String content
)

content without an ATX heading's optional closing sequence.

# Title ## is Title. The #s only close the heading when a space comes before them, so # C# keeps its #, and so does an escaped \#.

Implementation

String stripClosingHashes(String content) {
  var end = content.length;
  while (end > 0 && content.codeUnitAt(end - 1) == _hash) {
    end -= 1;
  }
  if (end == content.length) {
    return content;
  }
  if (end == 0) {
    return '';
  }
  final before = content.codeUnitAt(end - 1);
  if (before != _space && before != _tab) {
    return content;
  }
  return content.substring(0, end).trimRight();
}