The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { RootContent } from "mdast"; |
| 2 | import { unified } from "unified"; |
| 3 | import remarkParse from "remark-parse"; |
| 4 | import { msg } from "gt-react-native"; |
| 5 | import { m, type MessageResolver } from "../i18n/messages"; |
| 6 | |
| 7 | import { decodeHtmlEntities, markdownHtmlTags } from "./markdownDocument"; |
| 8 | |
| 9 | export type MarkdownDetailsSegment = |
| 10 | | { readonly kind: "markdown"; readonly markdown: string; readonly sourceOffset: number } |
| 11 | | { |
| 12 | readonly kind: "details"; |
| 13 | readonly summary: string; |
| 14 | readonly body: string; |
| 15 | readonly open: boolean; |
| 16 | readonly sourceOffset: number; |
| 17 | }; |
| 18 | |
| 19 | type OpenFold = { start: number; openTagEnd: number; open: boolean }; |
| 20 | |
| 21 | const parser = unified().use(remarkParse); |
| 22 | const DETAILS_TAG = /^<(\/?)details\b/i; |
| 23 | const SUMMARY = /^\s*<summary\b[^>]*>([\s\S]*?)<\/summary\s*>/i; |
| 24 | |
| 25 | /** |
| 26 | * Splits top-level `<details>` blocks out of chat Markdown so they can render as folds. |
| 27 | * Tags inside code, or a fold opened inline in a paragraph, stay Markdown. A fold whose |
| 28 | * closing tag has not streamed yet runs to the end. |
| 29 | */ |
| 30 | export function splitMarkdownDetails( |
| 31 | markdown: string, |
| 32 | resolve: MessageResolver = m, |
| 33 | ): ReadonlyArray<MarkdownDetailsSegment> { |
| 34 | if (!/<details\b/i.test(markdown)) return [{ kind: "markdown", markdown, sourceOffset: 0 }]; |
| 35 | |
| 36 | const folds: Array<OpenFold & { end: number; closeTagLength: number }> = []; |
| 37 | let depth = 0; |
| 38 | // Asserted so the scan closure's writes are not narrowed away after the loop. |
| 39 | let current = null as OpenFold | null; |
| 40 | // A fold opens only at block level; once open, a tag inside a paragraph can close it. |
| 41 | const scan = (node: RootContent, inline: boolean) => { |
| 42 | if (node.type !== "html") { |
| 43 | if (depth > 0 && "children" in node) { |
| 44 | for (const child of node.children) scan(child, true); |
| 45 | } |
| 46 | return; |
| 47 | } |
| 48 | if (node.position?.start.offset === undefined) return; |
| 49 | const nodeStart = node.position.start.offset; |
| 50 | for (const tag of markdownHtmlTags(node.value)) { |
| 51 | const details = DETAILS_TAG.exec(tag[0]); |
| 52 | if (!details || (inline && depth === 0)) continue; |
| 53 | const start = nodeStart + tag.index; |
| 54 | if (details[1] !== "/") { |
| 55 | if (depth === 0) { |
| 56 | // Quoted values are blanked so `title="is open"` does not count as the attribute. |
| 57 | const attributes = tag[0].replace(/"[^"]*"|'[^']*'/g, '""'); |
| 58 | current = { |
| 59 | start, |
| 60 | openTagEnd: start + tag[0].length, |
| 61 | open: /\sopen\b/i.test(attributes), |
| 62 | }; |
| 63 | } |
| 64 | depth += 1; |
| 65 | } else if (depth > 0) { |
| 66 | depth -= 1; |
| 67 | if (depth === 0 && current) { |
| 68 | folds.push({ ...current, end: start + tag[0].length, closeTagLength: tag[0].length }); |
| 69 | current = null; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | }; |
| 74 | for (const node of parser.parse(markdown).children) scan(node, false); |
| 75 | if (current) folds.push({ ...current, end: markdown.length, closeTagLength: 0 }); |
| 76 | if (folds.length === 0) return [{ kind: "markdown", markdown, sourceOffset: 0 }]; |
| 77 | |
| 78 | const segments: MarkdownDetailsSegment[] = []; |
| 79 | let cursor = 0; |
| 80 | for (const fold of folds) { |
| 81 | if (fold.start > cursor) { |
| 82 | segments.push({ |
| 83 | kind: "markdown", |
| 84 | markdown: markdown.slice(cursor, fold.start), |
| 85 | sourceOffset: cursor, |
| 86 | }); |
| 87 | } |
| 88 | let body = markdown.slice(fold.openTagEnd, fold.end - fold.closeTagLength); |
| 89 | const summary = SUMMARY.exec(body); |
| 90 | if (summary) body = body.slice(summary[0].length); |
| 91 | const label = summary ? decodeHtmlEntities(summary[1]!.replace(/<[^>]*>/g, "")).trim() : ""; |
| 92 | segments.push({ |
| 93 | kind: "details", |
| 94 | summary: |
| 95 | label || resolve(msg("Details", { $context: "fallback summary of a collapsed section" })), |
| 96 | body, |
| 97 | open: fold.open, |
| 98 | sourceOffset: fold.start, |
| 99 | }); |
| 100 | cursor = fold.end; |
| 101 | } |
| 102 | if (cursor < markdown.length) { |
| 103 | segments.push({ kind: "markdown", markdown: markdown.slice(cursor), sourceOffset: cursor }); |
| 104 | } |
| 105 | return segments; |
| 106 | } |