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

155 lines
4.5 KiB
Dart

/// Inline markdown spans for assistant prose (the web subset).
library;
// ---- Inline -----------------------------------------------------------------
/// One inline markdown span.
sealed class MarkdownInline {
const MarkdownInline();
}
final class MarkdownPlain extends MarkdownInline {
const MarkdownPlain(this.text);
final String text;
}
final class MarkdownInlineCode extends MarkdownInline {
const MarkdownInlineCode(this.text);
final String text;
}
final class MarkdownStrong extends MarkdownInline {
const MarkdownStrong(this.spans);
final List<MarkdownInline> spans;
}
final class MarkdownEmphasis extends MarkdownInline {
const MarkdownEmphasis(this.spans);
final List<MarkdownInline> spans;
}
final class MarkdownLink extends MarkdownInline {
const MarkdownLink(this.url, this.spans);
final String url;
final List<MarkdownInline> spans;
}
final RegExp _wordChar = RegExp(r'[\p{L}\p{N}]', unicode: true);
final RegExp _whitespace = RegExp(r'\s');
/// Parses inline markup into a flat span tree. Handles `` `code` ``,
/// `**bold**` / `__bold__`, `*italic*` / `_italic_`, `[label](url)` and bare
/// `http(s)://` autolinks; everything else stays plain text. Underscore
/// emphasis requires word boundaries so `snake_case` is never italicised.
List<MarkdownInline> parseInline(String? text) {
final source = text ?? '';
final spans = <MarkdownInline>[];
final buffer = StringBuffer();
var i = 0;
void flush() {
if (buffer.isNotEmpty) {
spans.add(MarkdownPlain(buffer.toString()));
buffer.clear();
}
}
while (i < source.length) {
final c = source[i];
if (c == '`') {
final end = source.indexOf('`', i + 1);
if (end > i) {
flush();
spans.add(MarkdownInlineCode(source.substring(i + 1, end)));
i = end + 1;
} else {
buffer.write(c);
i += 1;
}
} else if (c == '*' || c == '_') {
final doubled = i + 1 < source.length && source[i + 1] == c;
final marker = doubled ? c + c : c;
final boundOk = c != '_' || i == 0 || !_wordChar.hasMatch(source[i - 1]);
final end = boundOk ? source.indexOf(marker, i + marker.length) : -1;
final closes =
end > i + marker.length &&
(c != '_' ||
end + 1 >= source.length ||
!_wordChar.hasMatch(source[end + 1]));
if (closes) {
flush();
final inner = parseInline(source.substring(i + marker.length, end));
spans.add(doubled ? MarkdownStrong(inner) : MarkdownEmphasis(inner));
i = end + marker.length;
} else {
buffer.write(c);
i += 1;
}
} else if (c == '[') {
final close = source.indexOf(']', i + 1);
if (close > i && close + 1 < source.length && source[close + 1] == '(') {
final urlEnd = source.indexOf(')', close + 2);
final url = urlEnd > close
? source.substring(close + 2, urlEnd).trim()
: '';
if (url.isNotEmpty) {
flush();
spans.add(
MarkdownLink(url, parseInline(source.substring(i + 1, close))),
);
i = urlEnd + 1;
} else {
buffer.write(c);
i += 1;
}
} else {
buffer.write(c);
i += 1;
}
} else {
final url = _autolinkAt(source, i);
if (url != null) {
flush();
spans.add(MarkdownLink(url, [MarkdownPlain(url)]));
i += url.length;
} else {
buffer.write(c);
i += 1;
}
}
}
flush();
return spans;
}
/// A bare `http(s)://` URL starting exactly at [index], with trailing
/// punctuation trimmed.
String? _autolinkAt(String text, int index) {
if (!text.startsWith('http://', index) &&
!text.startsWith('https://', index)) {
return null;
}
var end = index;
while (end < text.length &&
!_whitespace.hasMatch(text[end]) &&
!'<>"\''.contains(text[end])) {
end += 1;
}
var url = text.substring(index, end);
while (url.isNotEmpty && '.,;:!?)]}'.contains(url[url.length - 1])) {
url = url.substring(0, url.length - 1);
}
return url.length > 'https://'.length ? url : null;
}
/// Only `http`/`https` link targets may be activated; the transcript is
/// untrusted model output, so `javascript:`/`data:` stay plain labels.
bool isSafeMarkdownLink(String url) =>
RegExp(r'^https?://', caseSensitive: false).hasMatch(url);
/// The label a fenced code block shows: its language, or `code` when unnamed.
String markdownCodeLabel(String language) {
final value = language.trim();
return value.isEmpty ? 'code' : value;
}