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:
@@ -83,7 +83,11 @@ class _SpeechBuilder {
|
||||
_part = part;
|
||||
_markdown = markdown;
|
||||
_cursor = 0;
|
||||
for (final block in chatMarkdownCache.blocksFor(markdown)) {
|
||||
_blocks(chatMarkdownCache.blocksFor(markdown));
|
||||
}
|
||||
|
||||
void _blocks(List<MarkdownBlock> blocks) {
|
||||
for (final block in blocks) {
|
||||
switch (block) {
|
||||
case MarkdownHeading(text: final text):
|
||||
_visible(text);
|
||||
@@ -92,7 +96,10 @@ class _SpeechBuilder {
|
||||
case MarkdownQuote(lines: final lines):
|
||||
lines.where((line) => line.trim().isNotEmpty).forEach(_visible);
|
||||
case MarkdownList(items: final items):
|
||||
items.forEach(_visible);
|
||||
for (final item in items) {
|
||||
_visible(item.text);
|
||||
_blocks(item.children);
|
||||
}
|
||||
case MarkdownTable(header: final header, rows: final rows):
|
||||
_phrase('Table.', _cursor);
|
||||
[...header, ...rows.expand((row) => row)].forEach(_visible);
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -102,10 +102,10 @@ void main() {
|
||||
expect((blocks[0] as MarkdownQuote).lines, ['quoted **line**', 'second']);
|
||||
final bullets = blocks[1] as MarkdownList;
|
||||
expect(bullets.ordered, isFalse);
|
||||
expect(bullets.items, ['one', 'two']);
|
||||
expect(_texts(bullets), ['one', 'two']);
|
||||
final ordered = blocks[2] as MarkdownList;
|
||||
expect(ordered.ordered, isTrue);
|
||||
expect(ordered.items, ['first', 'second']);
|
||||
expect(_texts(ordered), ['first', 'second']);
|
||||
});
|
||||
|
||||
test('thematic rules need three identical markers', () {
|
||||
@@ -198,11 +198,66 @@ void main() {
|
||||
expect(_visible(parseInline(source)), source);
|
||||
});
|
||||
|
||||
test('an indented nested item is retained rather than dropped', () {
|
||||
test('an indented nested item is kept under its parent', () {
|
||||
final blocks = parseMarkdown('- outer\n - inner\n continuation');
|
||||
final list = blocks.first as MarkdownList;
|
||||
expect(list.items, ['outer', 'inner']);
|
||||
expect((blocks.last as MarkdownParagraph).text, 'continuation');
|
||||
final list = blocks.single as MarkdownList;
|
||||
expect(_texts(list), ['outer']);
|
||||
final inner = list.items.single.children.single as MarkdownList;
|
||||
expect(_texts(inner), ['inner continuation']);
|
||||
});
|
||||
|
||||
test('numbered items keep counting around nested bullets', () {
|
||||
final blocks = parseMarkdown(
|
||||
'1. First\n - detail a\n - detail b\n2. Second\n - detail c\n'
|
||||
'3. Third',
|
||||
);
|
||||
final list = blocks.single as MarkdownList;
|
||||
expect(list.ordered, isTrue);
|
||||
expect(_texts(list), ['First', 'Second', 'Third']);
|
||||
expect(_texts(list.items.first.children.single as MarkdownList), [
|
||||
'detail a',
|
||||
'detail b',
|
||||
]);
|
||||
});
|
||||
|
||||
test('a loose list stays one list across blank lines', () {
|
||||
final list =
|
||||
parseMarkdown('1. one\n\n2. two\n\n\n3. three').single
|
||||
as MarkdownList;
|
||||
expect(_texts(list), ['one', 'two', 'three']);
|
||||
});
|
||||
|
||||
test('an ordered list starts at its first number', () {
|
||||
final list = parseMarkdown('3. three\n4. four').single as MarkdownList;
|
||||
expect(list.start, 3);
|
||||
expect(parseMarkdown('- a').single, isA<MarkdownList>());
|
||||
});
|
||||
|
||||
test('a wrapped line and an indented paragraph stay in their item', () {
|
||||
final list =
|
||||
parseMarkdown(
|
||||
'1. starts here\nand wraps\n\n later paragraph\n2. next',
|
||||
).single
|
||||
as MarkdownList;
|
||||
expect(_texts(list), ['starts here and wraps', 'next']);
|
||||
expect(
|
||||
(list.items.first.children.single as MarkdownParagraph).text,
|
||||
'later paragraph',
|
||||
);
|
||||
});
|
||||
|
||||
test('a list ends at text back at the margin after a blank line', () {
|
||||
final blocks = parseMarkdown('1. one\n2. two\n\nAfter the list.');
|
||||
expect(blocks, hasLength(2));
|
||||
expect((blocks.last as MarkdownParagraph).text, 'After the list.');
|
||||
});
|
||||
|
||||
test('nested content is decoded once', () {
|
||||
final list =
|
||||
parseMarkdown('- a\n - &lt;b&gt;').single as MarkdownList;
|
||||
expect(_texts(list.items.single.children.single as MarkdownList), [
|
||||
'<b>',
|
||||
]);
|
||||
});
|
||||
|
||||
test('a link with an empty target stays literal', () {
|
||||
@@ -212,3 +267,7 @@ void main() {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
List<String> _texts(MarkdownList list) => [
|
||||
for (final item in list.items) item.text,
|
||||
];
|
||||
|
||||
@@ -3,6 +3,17 @@ import 'package:flutter/material.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart';
|
||||
|
||||
const _sample = '''
|
||||
Numbered steps with nested details and blank lines keep counting:
|
||||
|
||||
1. Install the server
|
||||
- needs Docker
|
||||
- takes a few minutes
|
||||
|
||||
2. Sign in
|
||||
- open the web app
|
||||
|
||||
3. Start a chat
|
||||
|
||||
A wide table wraps its cells to the transcript width instead of scrolling:
|
||||
|
||||
| Stage | What it does | Result | Owner |
|
||||
|
||||
Reference in New Issue
Block a user