/// Inline markdown spans for assistant prose (the web subset). library; import 'chat_links.dart'; // ---- 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 spans; } final class MarkdownEmphasis extends MarkdownInline { const MarkdownEmphasis(this.spans); final List spans; } final class MarkdownLink extends MarkdownInline { const MarkdownLink(this.url, this.spans); final String url; final List spans; } final RegExp _wordChar = RegExp(r'[\p{L}\p{N}]', unicode: true); /// Parses inline markup into a flat span tree. Handles `` `code` ``, /// `**bold**` / `__bold__`, `*italic*` / `_italic_`, `[label](url)`, /// `` / `` autolinks and bare `http(s)://` / `www.` links; /// everything else stays plain text. Underscore /// emphasis requires word boundaries so `snake_case` is never italicised. List parseInline(String? text) { final source = text ?? ''; final spans = []; 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); final destination = close > i && close + 1 < source.length && source[close + 1] == '(' ? _destination(source, close + 2) : null; if (destination != null) { flush(); spans.add( MarkdownLink( destination.url, parseInline(source.substring(i + 1, close)), ), ); i = destination.end; } else { buffer.write(c); i += 1; } } else if (c == '<') { final close = source.indexOf('>', i + 1); final inner = close > i ? source.substring(i + 1, close) : ''; final target = inner.isEmpty ? null : autolinkTarget(inner); if (target != null) { flush(); spans.add(MarkdownLink(target, [MarkdownPlain(inner)])); i = close + 1; } else { buffer.write(c); i += 1; } } else { final url = bareLinkAt(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 `(destination)` starting at [start] (just after the `(`): parentheses /// inside it must balance, and an optional `"title"` after whitespace is /// dropped. Null when it is empty or never closes. ({String url, int end})? _destination(String source, int start) { var depth = 0; for (var i = start; i < source.length; i += 1) { final c = source[i]; if (c == '\n') return null; if (c == '(') depth += 1; if (c == ')') { if (depth == 0) { final url = source.substring(start, i).trim().split(' ').first; return url.isEmpty ? null : (url: url, end: i + 1); } depth -= 1; } } return null; } /// Only `http`/`https`/`mailto` targets (and `www.` hosts) may be activated; /// the transcript is untrusted model output, so `javascript:`/`data:`/`file:` /// stay plain labels. bool isSafeMarkdownLink(String url) => chatLinkUri(url) != null; /// 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; }