Chat: highlighted, clickable links in messages

Markdown links, autolinks and bare http(s)/www. URLs open externally through
url_launcher (http, https and mailto only) in assistant markdown and operator
messages; code is never linkified. Links use the host primary colour with an
underline that thickens on hover, and offer Copy link on long press and
right-click without breaking text selection.
This commit is contained in:
2026-11-03 16:17:26 +00:00
parent 5f51bbf53b
commit 9bfd16c6e5
16 changed files with 1081 additions and 113 deletions
@@ -1,6 +1,8 @@
/// Inline markdown spans for assistant prose (the web subset).
library;
import 'chat_links.dart';
// ---- Inline -----------------------------------------------------------------
/// One inline markdown span.
@@ -35,11 +37,11 @@ final class MarkdownLink extends MarkdownInline {
}
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
/// `**bold**` / `__bold__`, `*italic*` / `_italic_`, `[label](url)`,
/// `<https://…>` / `<mailto:…>` autolinks and bare `http(s)://` / `www.` links;
/// everything else stays plain text. Underscore
/// emphasis requires word boundaries so `snake_case` is never italicised.
List<MarkdownInline> parseInline(String? text) {
final source = text ?? '';
@@ -87,27 +89,37 @@ List<MarkdownInline> parseInline(String? text) {
}
} 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;
}
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 = _autolinkAt(source, i);
final url = bareLinkAt(source, i);
if (url != null) {
flush();
spans.add(MarkdownLink(url, [MarkdownPlain(url)]));
@@ -122,30 +134,30 @@ List<MarkdownInline> parseInline(String? text) {
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;
/// 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;
}
}
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;
return 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);
/// 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) {