openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
15 lines (14 loc) • 978 B
JavaScript
//#region packages/normalization-core/src/markdown-plain-text.ts
/**
* Flattens Markdown into a single line of readable plain text.
*
* For one-line surfaces that render text verbatim — session-list previews,
* sidebar narration — where unrendered syntax like `[title](url)` would leak
* to the user. Lossy by design: it drops fenced code entirely and keeps only
* link/image text, so it must not be used where the Markdown is rendered.
*/
function flattenMarkdownToPlainText(text) {
return text.replace(/```[\s\S]*?```/g, " ").replace(/```/g, " ").replace(/`([^`]*)`/g, "$1").replace(/!\[([^\]]*)\]\([^)]*\)/g, "$1").replace(/\[([^\]]+)\]\([^)]*\)/g, "$1").replace(/^\s{0,3}(?:#{1,6}|>|[-+*]|\d+[.)])\s+/gm, "").replace(/(\*{1,2})(?=\S)([\s\S]*?\S)\1/g, "$2").replace(/(^|[^\p{L}\p{N}])(_{1,2})(?=\S)([\s\S]*?\S)\2(?![\p{L}\p{N}])/gu, "$1$3").replace(/~~(?=\S)([\s\S]*?\S)~~/g, "$1").replace(/\s+/g, " ").trim();
}
//#endregion
export { flattenMarkdownToPlainText as t };