dedent method

DedentedText dedent([
  1. int length = 4
])

Removes up to length characters of leading whitespace.

Implementation

// The way of handling tabs: https://spec.commonmark.org/0.30/#tabs
DedentedText dedent([int length = 4]) {
  final whitespaceMatch = RegExp('^[ \t]{0,$length}').firstMatch(this);
  const tabSize = 4;

  int? tabRemaining;
  var start = 0;
  final whitespaces = whitespaceMatch?[0];
  if (whitespaces != null) {
    var indentLength = 0;
    for (; start < whitespaces.length; start++) {
      final isTab = whitespaces[start] == '\t';
      if (isTab) {
        indentLength += tabSize;
        tabRemaining = 4;
      } else {
        indentLength += 1;
      }
      if (indentLength >= length) {
        if (tabRemaining != null) {
          tabRemaining = indentLength - length;
        }
        if (indentLength == length || isTab) {
          start += 1;
        }
        break;
      }
      if (tabRemaining != null) {
        tabRemaining = 0;
      }
    }
  }
  return DedentedText(substring(start), tabRemaining);
}