Files
pipelines/app/lib/features/chat/ui/transcript/markdown_blocks.dart
T

251 lines
7.3 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});
final bool ordered;
final List<String> items;
}
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) {
final lines = decodeHtmlEntities(
source ?? '',
).replaceAll('\r\n', '\n').replaceAll('\r', '\n').split('\n');
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;
}
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));
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)!);
}
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);
final bool ordered;
final String text;
}
/// 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+$'), '');
}