trajectoryPreviewText function
Builds a bounded one-line preview without parsing the full document.
Returns the Markdown-stripped, whitespace-collapsed head of text,
capped at 512 characters, with a trailing … whenever the source or the
collapsed text was cut short.
Implementation
String trajectoryPreviewText(String text) {
final source = text.length > _previewSourceCharacters
? text.substring(0, _previewSourceCharacters)
: text;
final compact = _collapseWhitespace(markdownToPlainText(source));
var preview = compact.length > _previewOutputCharacters
? compact.substring(0, _previewOutputCharacters)
: compact;
while (preview.isNotEmpty && _trailingWhitespace.hasMatch(preview)) {
preview = preview.substring(0, preview.length - 1);
}
final truncated =
source.length < text.length || preview.length < compact.length;
return truncated ? '$preview…' : preview;
}