findBlockBoundaries function
Implementation
List<int> findBlockBoundaries(
String text,
int start, {
required bool complete,
bool dollarMath = true,
}) {
final mathOpener = mathOpenerFor(dollar: dollarMath);
final boundaries = <int>[];
var pos = start;
var hasContent = false;
// list / footnote / indented code: an indented line after a blank line
// still belongs to it.
var continuable = false;
var afterBlank = false;
var inHtmlBlock = false;
OpenFence? fence;
RegExp? mathEnd;
RegExp? htmlEnd;
var details = 0;
// set after <!-- ... --> and friends end: a directly following HTML line
// joins the same block in the markdown package
var htmlChain = false;
void startBlockAt(int offset) {
if (offset > start && (boundaries.isEmpty || offset > boundaries.last)) {
boundaries.add(offset);
}
hasContent = false;
continuable = false;
afterBlank = false;
inHtmlBlock = false;
}
while (pos < text.length) {
final newline = text.indexOf('\n', pos);
final hasNewline = newline != -1;
final next = hasNewline ? newline + 1 : text.length;
final line = withoutCarriageReturn(
text.substring(pos, hasNewline ? newline : text.length),
);
final lineComplete = hasNewline || complete;
if (fence != null) {
if (lineComplete && fence.isClosedBy(line)) {
fence = null;
startBlockAt(next);
}
pos = next;
continue;
}
if (mathEnd != null) {
if (lineComplete && mathEnd.hasMatch(line)) {
mathEnd = null;
startBlockAt(next);
}
pos = next;
continue;
}
// <details> can hold blank lines; it ends at its closing tag
if (htmlChain) {
htmlChain = false;
final raw = _htmlRawBlock.firstMatch(line);
if (raw != null) {
final end = _htmlEndFor(raw);
if (end.hasMatch(line.substring(raw.end))) {
htmlChain = true;
} else {
htmlEnd = end;
}
pos = next;
continue;
}
if (_htmlBlock.hasMatch(line)) {
inHtmlBlock = true;
pos = next;
continue;
}
}
final startsDetails =
!inHtmlBlock &&
!(continuable && _indentWidth(line) > 0) &&
_shallow(line) &&
detailsOpener.hasMatch(line);
if (details > 0 || startsDetails) {
if (detailsOpener.hasMatch(line)) {
if (details == 0 && hasContent) startBlockAt(pos);
details++;
}
if (detailsCloser.hasMatch(line)) details--;
hasContent = true;
if (details == 0 && lineComplete && hasNewline) startBlockAt(next);
pos = next;
continue;
}
if (htmlEnd != null) {
if (htmlEnd.hasMatch(line)) {
htmlEnd = null;
htmlChain = true;
}
pos = next;
continue;
}
if (isBlank(line)) {
if (hasContent) afterBlank = true;
inHtmlBlock = false;
pos = next;
continue;
}
final indent = _indentWidth(line);
if (afterBlank) {
if (!continuable) {
startBlockAt(pos);
} else if (indent > 0 ||
_listMarker.hasMatch(line) ||
_footnoteDefinition.hasMatch(line)) {
afterBlank = false;
} else if (!lineComplete && _partialListMarker.hasMatch(line)) {
break;
} else {
startBlockAt(pos);
}
}
// indented fence/heading inside a list belongs to the item
final nested = continuable && indent > 0;
if (indent <= 3 && !nested && !inHtmlBlock) {
final math = mathOpener.firstMatch(line);
if (math != null) {
// "$$" could still become "$$x$$" on one line
if (!lineComplete) break;
if (hasContent) startBlockAt(pos);
mathEnd = mathBlockCloserFor(math[1]!);
hasContent = true;
pos = next;
continue;
}
final opener = _fenceOpener.firstMatch(line);
if (opener != null) {
// ```foo``` is inline code, wait for the whole line
if (opener[2]![0] == '`' && !lineComplete) break;
final opened = fenceOpenedBy(line);
if (opened != null) {
if (hasContent) startBlockAt(pos);
fence = opened;
hasContent = true;
pos = next;
continue;
}
}
final heading = _atxHeading.firstMatch(line);
// "#" could still become "#tag"
if (heading != null && (heading[1]!.isNotEmpty || lineComplete)) {
if (hasContent) startBlockAt(pos);
if (hasNewline) {
startBlockAt(next);
} else {
hasContent = true;
}
pos = next;
continue;
}
final html = _htmlRawBlock.firstMatch(line);
if (html != null) {
final end = _htmlEndFor(html);
if (end.hasMatch(line.substring(html.end))) {
htmlChain = true;
} else {
htmlEnd = end;
}
hasContent = true;
pos = next;
continue;
}
if (_htmlBlock.hasMatch(line)) inHtmlBlock = true;
}
if (indent >= 4 ||
_listMarker.hasMatch(line) ||
_footnoteDefinition.hasMatch(line)) {
continuable = true;
}
hasContent = true;
pos = next;
}
return boundaries;
}