Keep numbered lists counting across nested items and blank lines

A list ended at the first line that was not an item, so a numbered list
with nested bullets, wrapped lines or blank lines between items became
one-item lists that each showed 1. Lists now nest (an item holds the
blocks indented under it), continue across blank lines, join wrapped
lines, and number from their first item.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 20:39:39 +00:00
parent dd742ec8e4
commit 99f7edc0ec
5 changed files with 253 additions and 27 deletions
@@ -199,7 +199,11 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
),
),
);
case MarkdownList(ordered: final ordered, items: final items):
case MarkdownList(
ordered: final ordered,
start: final start,
items: final items,
):
return Semantics(
container: true,
explicitChildNodes: true,
@@ -218,9 +222,9 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
children: [
Padding(
padding: const EdgeInsetsDirectional.only(end: 8),
child: Text(ordered ? '${index + 1}.' : '•'),
child: Text(ordered ? '${start + index}.' : '•'),
),
Expanded(child: _text(context, items[index])),
Expanded(child: _listItem(context, items[index])),
],
),
),
@@ -279,6 +283,20 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
).end.clamp(0, widget.text.length);
}
/// An item's text and, under it, whatever is nested in it.
Widget _listItem(BuildContext context, MarkdownListItem item) {
if (item.children.isEmpty) return _text(context, item.text);
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
spacing: 4,
children: [
_text(context, item.text),
for (final child in item.children) _block(context, child),
],
);
}
Widget _cell(BuildContext context, String text, {bool header = false}) {
final style = header
? _roleStyle(context, Theme.of(context).textTheme.labelLarge)
@@ -41,9 +41,24 @@ final class MarkdownQuote extends MarkdownBlock {
}
final class MarkdownList extends MarkdownBlock {
const MarkdownList({required this.ordered, required this.items});
const MarkdownList({
required this.ordered,
required this.items,
this.start = 1,
});
final bool ordered;
final List<String> items;
/// The number an ordered list starts at (`3.` starts at 3).
final int start;
final List<MarkdownListItem> items;
}
/// One list item: its first paragraph's text and whatever is nested under it
/// (a sub-list, a further paragraph, a code block).
final class MarkdownListItem {
const MarkdownListItem(this.text, [this.children = const []]);
final String text;
final List<MarkdownBlock> children;
}
final class MarkdownTable extends MarkdownBlock {
@@ -57,10 +72,14 @@ final class MarkdownRule extends MarkdownBlock {
}
/// Parses [source] into the block list a renderer walks. Never throws.
List<MarkdownBlock> parseMarkdown(String? source) {
final lines = decodeHtmlEntities(
List<MarkdownBlock> parseMarkdown(String? source) => _parseLines(
decodeHtmlEntities(
source ?? '',
).replaceAll('\r\n', '\n').replaceAll('\r', '\n').split('\n');
).replaceAll('\r\n', '\n').replaceAll('\r', '\n').split('\n'),
);
/// The blocks of already-decoded [lines].
List<MarkdownBlock> _parseLines(List<String> lines) {
final blocks = <MarkdownBlock>[];
var i = 0;
while (i < lines.length) {
@@ -124,15 +143,10 @@ List<MarkdownBlock> parseMarkdown(String? source) {
continue;
}
final marker = _listMarker(line);
if (marker != null) {
final ordered = marker.ordered;
final items = <String>[];
while (i < lines.length && _listMarker(lines[i])?.ordered == ordered) {
items.add(_listMarker(lines[i])!.text);
i += 1;
}
blocks.add(MarkdownList(ordered: ordered, items: items));
if (_listMarker(line) != null) {
final (list, next) = _parseList(lines, i);
blocks.add(list);
i = next;
continue;
}
@@ -218,7 +232,11 @@ _ListMarker? _listMarker(String line) {
final trimmed = line.replaceFirst(RegExp(r'^\s+'), '');
final orderedMatch = _orderedItem.firstMatch(trimmed);
if (orderedMatch != null) {
return _ListMarker(true, orderedMatch.group(2)!);
return _ListMarker(
true,
orderedMatch.group(2)!,
int.parse(orderedMatch.group(1)!),
);
}
final bulletMatch = _bulletItem.firstMatch(trimmed);
if (bulletMatch != null) return _ListMarker(false, bulletMatch.group(2)!);
@@ -226,9 +244,122 @@ _ListMarker? _listMarker(String line) {
}
final class _ListMarker {
const _ListMarker(this.ordered, this.text);
const _ListMarker(this.ordered, this.text, [this.number = 1]);
final bool ordered;
final String text;
final int number;
}
/// The column a line's text starts at (a tab advances to the next multiple
/// of four).
int _indent(String line) {
var column = 0;
for (var index = 0; index < line.length; index += 1) {
final char = line[index];
if (char == ' ') {
column += 1;
} else if (char == '\t') {
column += 4 - column % 4;
} else {
break;
}
}
return column;
}
/// [line] without its first [columns] columns of indentation.
String _dedent(String line, int columns) {
var column = 0;
var index = 0;
while (index < line.length && column < columns) {
final char = line[index];
if (char == ' ') {
column += 1;
} else if (char == '\t') {
column += 4 - column % 4;
} else {
break;
}
index += 1;
}
return line.substring(index);
}
/// The list starting at [start], and the index after it.
///
/// Items of one kind (ordered or bullet) at the list's indentation belong to
/// it, also across blank lines (a loose list). Lines indented past the
/// marker belong to the item above: a wrapped line joins its text, anything
/// else (a nested list, a later paragraph, code) is parsed as its children.
/// A list numbers from its first item, so `3.` starts it at 3.
(MarkdownList, int) _parseList(List<String> lines, int start) {
final first = _listMarker(lines[start])!;
final indent = _indent(lines[start]);
final items = <MarkdownListItem>[];
var i = start;
while (i < lines.length) {
final marker = _listMarker(lines[i]);
final column = _indent(lines[i]);
if (marker == null ||
marker.ordered != first.ordered ||
column < indent ||
column > indent + 1) {
break;
}
final text = <String>[marker.text];
final body = <String>[];
i += 1;
while (i < lines.length) {
final line = lines[i];
if (line.trim().isEmpty) {
var next = i;
while (next < lines.length && lines[next].trim().isEmpty) {
next += 1;
}
if (next < lines.length && _indent(lines[next]) > indent + 1) {
// A blank line inside the item: what follows is nested under it.
body.addAll(lines.sublist(i, next));
}
i = next;
if (next >= lines.length || _indent(lines[next]) <= indent + 1) break;
continue;
}
if (_indent(line) > indent + 1) {
if (body.isEmpty && !_startsBlock(lines, i)) {
text.add(line.trim());
} else {
body.add(line);
}
i += 1;
continue;
}
// A wrapped line at the margin still continues the item's text.
if (body.isEmpty && !_startsBlock(lines, i)) {
text.add(line.trim());
i += 1;
continue;
}
break;
}
items.add(MarkdownListItem(text.join(' '), _nested(body)));
}
return (
MarkdownList(
ordered: first.ordered,
start: first.ordered ? first.number : 1,
items: items,
),
i,
);
}
/// The blocks nested under a list item, parsed from its lines with their
/// common indentation removed.
List<MarkdownBlock> _nested(List<String> body) {
final content = body.where((line) => line.trim().isNotEmpty);
if (content.isEmpty) return const [];
final margin = content.map(_indent).reduce((a, b) => a < b ? a : b);
return _parseLines([for (final line in body) _dedent(line, margin)]);
}
/// True when [lines] at [index] begins a block that interrupts a paragraph.