UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

176 lines (175 loc) • 5.23 kB
//#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(e) { let t = stripInlineLinks(e.replace(/```[\s\S]*?```/g, " ").replace(/```/g, " ").replace(/`([^`]*)`/g, "$1")).replace(/^[\t ]{0,3}(?:(?:>[\t ]*)+|(?:#{1,6}|[-+*]|\d+[.)])\s+)/gm, ""); for (const e of [ "*", "_", "~" ]) t = stripEmphasis(t, e); return t.replace(/\s+/g, " ").trim(); } function stripEmphasis(e, t) { const n = t === "~" ? [2] : [2, 1]; const r = { 1: 0, 2: 0 }; const findCloser = (n, i) => { const a = t.repeat(n); let o = e.indexOf(a, Math.max(i, r[n])); while (o !== -1) { if (/\S/.test(e.charAt(o - 1)) && (t !== "_" || !/^[\p{L}\p{N}]/u.test(e.slice(o + n, o + n + 2)))) { r[n] = o; return o; } o = e.indexOf(a, o + 1); } r[n] = e.length; return -1; }; let i = ""; let a = 0; for (let r = e.indexOf(t); r !== -1; r = e.indexOf(t, r + 1)) { const o = (e.codePointAt(r - 2) ?? 0) > 65535 ? 2 : 1; if (t === "_" && r !== 0 && (r === a || /[\p{L}\p{N}]/u.test(e.slice(r - o, r)))) continue; for (const o of n) { const n = r + o; if (!e.startsWith(t.repeat(o), r) || !/\S/.test(e.charAt(n))) continue; const s = findCloser(o, n + 1); if (s !== -1) { i += e.slice(a, r) + e.slice(n, s); a = s + o; r = a - 1; break; } } } return i + e.slice(a); } /** CommonMark link syntax only breaks on ASCII; U+00A0 and other Unicode spaces belong to the destination. */ const ASCII_WHITESPACE = /[\t\n\v\f\r ]/; function isAsciiSpaceOrControl(e) { return e <= " " || e === ""; } /** Maps each `(` to its balanced `)` within the same whitespace-free run, skipping escapes. */ function matchParens(e) { const t = /* @__PURE__ */ new Map(); const n = []; for (let r = 0; r < e.length; r += 1) { const i = e.charAt(r); if (i === "\\") r += 1; else if (isAsciiSpaceOrControl(i)) n.length = 0; else if (i === "(") n.push(r); else if (i === ")") { const e = n.pop(); if (e !== void 0) t.set(e, r); } } return t; } const TITLE_CLOSERS = { "\"": "\"", "'": "'", "(": ")" }; function skipSpaces(e, t) { let n = t; while (n < e.length && ASCII_WHITESPACE.test(e.charAt(n))) n += 1; return n; } /** Returns the index of the `)` closing `(destination "title")` opened at `open`. */ function findDestinationEnd(e, t, n) { let r = skipSpaces(e, t + 1); if (e.charAt(r) === "<") { r += 1; while (r < e.length && !"<>\n".includes(e.charAt(r))) r += e.charAt(r) === "\\" ? 2 : 1; if (e.charAt(r) !== ">") return; r += 1; } else while (r < e.length && e.charAt(r) !== ")" && !isAsciiSpaceOrControl(e.charAt(r))) if (e.charAt(r) === "(") { const e = n.get(r); if (e === void 0) return; r = e + 1; } else r += e.charAt(r) === "\\" ? 2 : 1; const i = skipSpaces(e, r); const a = TITLE_CLOSERS[e.charAt(i)]; if (i > r && a) { r = i + 1; while (r < e.length && e.charAt(r) !== a) { if (a === ")" && e.charAt(r) === "(") return; r += e.charAt(r) === "\\" ? 2 : 1; } r = skipSpaces(e, r + 1); } else r = i; return e.charAt(r) === ")" ? r : void 0; } /** * Maps each link label's `[` to its `]` and that `]` to the end of the `(destination "title")` * that follows it, skipping backslash-escaped characters. Brackets inside a destination or title * are not labels, so they never pair with a label bracket. */ function matchLinks(e) { const t = /* @__PURE__ */ new Map(); const n = /* @__PURE__ */ new Map(); const r = matchParens(e); const i = []; for (let a = 0; a < e.length; a += 1) { const o = e.charAt(a); if (o === "\\") a += 1; else if (o === "[") i.push(a); else if (o === "]") { const o = i.pop(); if (o === void 0) continue; t.set(o, a); const s = e.charAt(a + 1) === "(" ? findDestinationEnd(e, a + 1, r) : void 0; if (s !== void 0) { n.set(a, s); a = s; } } } return { labelEnds: t, destinationEnds: n }; } /** Strips inline link destinations, retaining nested labels and optionally omitting images entirely. */ function stripInlineLinks(e, t = {}) { const { labelEnds: n, destinationEnds: r } = matchLinks(e); const i = /* @__PURE__ */ new Map(); let a = ""; let o = 0; for (let s = 0; s < e.length; s += 1) { const c = i.get(s); if (c !== void 0) { a += e.slice(o, s); o = c; s = c - 1; continue; } const l = e[s] === "[" ? n.get(s) : void 0; const u = l === void 0 ? void 0 : r.get(l); if (l === void 0 || u === void 0) continue; let d = s > o && e[s - 1] === "!"; if (d) for (let t = s - 2; t >= 0 && e[t] === "\\"; t -= 1) d = !d; if (!d && l === s + 1) continue; a += e.slice(o, d ? s - 1 : s); if (d && t.images === "omit") { o = u + 1; s = u; continue; } o = s + 1; i.set(l, u + 1); } return a + e.slice(o); } //#endregion export { stripInlineLinks as n, flattenMarkdownToPlainText as t };