99f7edc0ec
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>
382 lines
11 KiB
Dart
382 lines
11 KiB
Dart
/// Block-level markdown parsing for assistant prose (the web subset).
|
|
|
|
/// A pure, widget-free port of `web/src/markdown.js` for assistant prose.
|
|
///
|
|
/// The subset is deliberately the same one the browser renders: paragraphs,
|
|
/// `#`..`######` headings, fenced code (``` / ~~~, language label else `code`),
|
|
/// block quotes, bullet and ordered lists, `|` tables with a `---` delimiter
|
|
/// row, thematic rules, and inline code / bold / italic / links / bare http(s)
|
|
/// autolinks. Anything outside it stays plain text and is never dropped.
|
|
library;
|
|
|
|
import 'markdown_entities.dart';
|
|
|
|
// ---- Blocks -----------------------------------------------------------------
|
|
|
|
/// One rendered markdown block.
|
|
sealed class MarkdownBlock {
|
|
const MarkdownBlock();
|
|
}
|
|
|
|
final class MarkdownHeading extends MarkdownBlock {
|
|
const MarkdownHeading(this.level, this.text);
|
|
final int level;
|
|
final String text;
|
|
}
|
|
|
|
final class MarkdownParagraph extends MarkdownBlock {
|
|
const MarkdownParagraph(this.text);
|
|
final String text;
|
|
}
|
|
|
|
final class MarkdownCode extends MarkdownBlock {
|
|
const MarkdownCode(this.language, this.code);
|
|
final String language;
|
|
final String code;
|
|
}
|
|
|
|
final class MarkdownQuote extends MarkdownBlock {
|
|
const MarkdownQuote(this.lines);
|
|
final List<String> lines;
|
|
}
|
|
|
|
final class MarkdownList extends MarkdownBlock {
|
|
const MarkdownList({
|
|
required this.ordered,
|
|
required this.items,
|
|
this.start = 1,
|
|
});
|
|
final bool ordered;
|
|
|
|
/// 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 {
|
|
const MarkdownTable(this.header, this.rows);
|
|
final List<String> header;
|
|
final List<List<String>> rows;
|
|
}
|
|
|
|
final class MarkdownRule extends MarkdownBlock {
|
|
const MarkdownRule();
|
|
}
|
|
|
|
/// Parses [source] into the block list a renderer walks. Never throws.
|
|
List<MarkdownBlock> parseMarkdown(String? source) => _parseLines(
|
|
decodeHtmlEntities(
|
|
source ?? '',
|
|
).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) {
|
|
final line = lines[i];
|
|
final trimmed = line.trim();
|
|
if (trimmed.isEmpty) {
|
|
i += 1;
|
|
continue;
|
|
}
|
|
|
|
final fence = _fenceRun(trimmed);
|
|
if (fence != null) {
|
|
final language = trimmed.substring(fence.length).trim().split(' ').first;
|
|
final code = <String>[];
|
|
i += 1;
|
|
while (i < lines.length && _fenceRun(lines[i].trim()) == null) {
|
|
code.add(lines[i]);
|
|
i += 1;
|
|
}
|
|
if (i < lines.length) i += 1; // consume the closing fence
|
|
blocks.add(MarkdownCode(language, code.join('\n')));
|
|
continue;
|
|
}
|
|
|
|
final level = _headingLevel(trimmed);
|
|
if (level > 0) {
|
|
blocks.add(
|
|
MarkdownHeading(level, _trimEndHashes(trimmed.substring(level).trim())),
|
|
);
|
|
i += 1;
|
|
continue;
|
|
}
|
|
|
|
if (_isRule(trimmed)) {
|
|
blocks.add(const MarkdownRule());
|
|
i += 1;
|
|
continue;
|
|
}
|
|
|
|
if (trimmed.startsWith('>')) {
|
|
final quote = <String>[];
|
|
while (i < lines.length && lines[i].trim().startsWith('>')) {
|
|
quote.add(_quoteLine(lines[i]));
|
|
i += 1;
|
|
}
|
|
blocks.add(MarkdownQuote(quote));
|
|
continue;
|
|
}
|
|
|
|
if (_isTableStart(lines, i)) {
|
|
final header = _splitRow(lines[i]);
|
|
i += 2; // header + delimiter row
|
|
final rows = <List<String>>[];
|
|
while (i < lines.length &&
|
|
lines[i].contains('|') &&
|
|
lines[i].trim().isNotEmpty) {
|
|
rows.add(_fitRow(_splitRow(lines[i]), header.length));
|
|
i += 1;
|
|
}
|
|
blocks.add(MarkdownTable(header, rows));
|
|
continue;
|
|
}
|
|
|
|
if (_listMarker(line) != null) {
|
|
final (list, next) = _parseList(lines, i);
|
|
blocks.add(list);
|
|
i = next;
|
|
continue;
|
|
}
|
|
|
|
final paragraph = <String>[line.trim()];
|
|
i += 1;
|
|
while (i < lines.length &&
|
|
lines[i].trim().isNotEmpty &&
|
|
!_startsBlock(lines, i)) {
|
|
paragraph.add(lines[i].trim());
|
|
i += 1;
|
|
}
|
|
blocks.add(MarkdownParagraph(paragraph.join(' ')));
|
|
}
|
|
return blocks;
|
|
}
|
|
|
|
/// The fence run (` ``` ` / `~~~`) a line opens or closes with, or null.
|
|
String? _fenceRun(String trimmed) {
|
|
if (trimmed.isEmpty) return null;
|
|
final first = trimmed[0];
|
|
if (first != '`' && first != '~') return null;
|
|
var end = 0;
|
|
while (end < trimmed.length && trimmed[end] == first) {
|
|
end += 1;
|
|
}
|
|
return end >= 3 ? trimmed.substring(0, end) : null;
|
|
}
|
|
|
|
int _headingLevel(String trimmed) {
|
|
var hashes = 0;
|
|
while (hashes < trimmed.length && trimmed[hashes] == '#') {
|
|
hashes += 1;
|
|
}
|
|
if (hashes == 0 || hashes > 6) return 0;
|
|
return trimmed.length == hashes || trimmed[hashes] == ' ' ? hashes : 0;
|
|
}
|
|
|
|
String _trimEndHashes(String text) => text.replaceFirst(RegExp(r'#+$'), '');
|
|
|
|
bool _isRule(String trimmed) {
|
|
if (trimmed.length < 3) return false;
|
|
final first = trimmed[0];
|
|
if (!'-*_'.contains(first)) return false;
|
|
for (var i = 0; i < trimmed.length; i += 1) {
|
|
if (trimmed[i] != first) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// True when [lines] at [index] is a table header followed by a `---` row.
|
|
bool _isTableStart(List<String> lines, int index) {
|
|
if (index + 1 >= lines.length || !lines[index].contains('|')) return false;
|
|
final delimiter = _splitRow(lines[index + 1]);
|
|
if (delimiter.isEmpty || _splitRow(lines[index]).length < 2) return false;
|
|
return delimiter.every(
|
|
(cell) =>
|
|
cell.isNotEmpty &&
|
|
cell.split('').every((char) => char == '-' || char == ':') &&
|
|
cell.contains('-'),
|
|
);
|
|
}
|
|
|
|
/// Splits a table row on unescaped `|`, dropping the outer empty cells.
|
|
List<String> _splitRow(String line) {
|
|
var value = line.trim();
|
|
if (value.startsWith('|')) value = value.substring(1);
|
|
if (value.endsWith('|')) value = value.substring(0, value.length - 1);
|
|
return value
|
|
.split('|')
|
|
.map((cell) => cell.replaceAll(r'\|', '|').trim())
|
|
.toList();
|
|
}
|
|
|
|
List<String> _fitRow(List<String> row, int size) => [
|
|
for (var index = 0; index < size; index += 1)
|
|
index < row.length ? row[index] : '',
|
|
];
|
|
|
|
final RegExp _orderedItem = RegExp(r'^(\d{1,9})[.)]\s+(.*)$');
|
|
final RegExp _bulletItem = RegExp(r'^([-*+])\s+(.*)$');
|
|
|
|
_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)!,
|
|
int.parse(orderedMatch.group(1)!),
|
|
);
|
|
}
|
|
final bulletMatch = _bulletItem.firstMatch(trimmed);
|
|
if (bulletMatch != null) return _ListMarker(false, bulletMatch.group(2)!);
|
|
return null;
|
|
}
|
|
|
|
final class _ListMarker {
|
|
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.
|
|
bool _startsBlock(List<String> lines, int index) {
|
|
final trimmed = lines[index].trim();
|
|
return _fenceRun(trimmed) != null ||
|
|
_headingLevel(trimmed) > 0 ||
|
|
_isRule(trimmed) ||
|
|
trimmed.startsWith('>') ||
|
|
_listMarker(lines[index]) != null ||
|
|
_isTableStart(lines, index);
|
|
}
|
|
|
|
String _quoteLine(String line) {
|
|
var value = line.trim();
|
|
if (value.startsWith('>')) value = value.substring(1);
|
|
if (value.startsWith(' ')) value = value.substring(1);
|
|
return value.replaceFirst(RegExp(r'\s+$'), '');
|
|
}
|