1
11export function blockText(content: unknown): string {
12 if (content == null) return "";
13 if (typeof content === "string") return stripHtml(content);
14 if (typeof content === "object") {
15 const c = content as { text?: unknown; title?: unknown };
16 if (typeof c.text === "string") return c.text;
17 if (typeof c.title === "string") return c.title;
18
19
20 const t = walkTiptapText(content);
21 if (t) return t;
22 }
23 return "";
24}
25
26
28export function stripHtml(html: string): string {
29 if (typeof DOMParser !== "undefined") {
30 return new DOMParser().parseFromString(html, "text/html").body.textContent ?? "";
31 }
32 return html.replace(/<[^>]*>/g, "");
33}
34
35function walkTiptapText(node: unknown, depth = 0): string {
36 if (!node || typeof node !== "object" || depth > 8) return "";
37 const n = node as { type?: string; text?: unknown; content?: unknown };
38 if (typeof n.text === "string") return n.text;
39 if (Array.isArray(n.content)) return n.content.map((c) => walkTiptapText(c, depth + 1)).join("");
40 return "";
41}
42