The best way to control Claude Code, Codex and more from any computer or phone
| 1 | export function plainHastText(node: unknown): string | null { |
| 2 | if (!node || typeof node !== "object" || !("children" in node) || !Array.isArray(node.children)) { |
| 3 | return null; |
| 4 | } |
| 5 | const parts = node.children.map((child) => { |
| 6 | if ( |
| 7 | child && |
| 8 | typeof child === "object" && |
| 9 | "type" in child && |
| 10 | child.type === "text" && |
| 11 | "value" in child && |
| 12 | typeof child.value === "string" |
| 13 | ) { |
| 14 | return child.value; |
| 15 | } |
| 16 | return null; |
| 17 | }); |
| 18 | return parts.every((part) => part !== null) ? parts.join("") : null; |
| 19 | } |
| 20 | |
| 21 | export function hastPlainTextDeep(node: unknown): string { |
| 22 | if (!node || typeof node !== "object") return ""; |
| 23 | if ("type" in node && node.type === "text" && "value" in node && typeof node.value === "string") { |
| 24 | return node.value; |
| 25 | } |
| 26 | if (!("children" in node) || !Array.isArray(node.children)) return ""; |
| 27 | return node.children.map(hastPlainTextDeep).join(""); |
| 28 | } |
| 29 | |
| 30 | export function hastHasText(node: unknown): boolean { |
| 31 | if (!node || typeof node !== "object") return false; |
| 32 | if ( |
| 33 | "type" in node && |
| 34 | node.type === "text" && |
| 35 | "value" in node && |
| 36 | typeof node.value === "string" && |
| 37 | node.value.trim().length > 0 |
| 38 | ) { |
| 39 | return true; |
| 40 | } |
| 41 | return "children" in node && Array.isArray(node.children) && node.children.some(hastHasText); |
| 42 | } |