|
| 1 | +import { findCodeRegions, isInsideCode } from "../../shared/text/code-regions.js"; |
| 2 | + |
| 3 | +const MAX_DETAILS_RENDER_DEPTH = 32; |
| 4 | + |
| 5 | +type DetailsNode = |
| 6 | + | string |
| 7 | + | { |
| 8 | + type: "details" | "summary"; |
| 9 | + children: DetailsNode[]; |
| 10 | + }; |
| 11 | + |
| 12 | +const DETAILS_TAG_RE = /<\s*(\/?)\s*(details|summary)(?=\s|>)[^>]*>/gi; |
| 13 | + |
| 14 | +function appendNode(target: DetailsNode[], node: DetailsNode): void { |
| 15 | + const previous = target.at(-1); |
| 16 | + if (typeof previous === "string" && typeof node === "string") { |
| 17 | + target[target.length - 1] = previous + node; |
| 18 | + return; |
| 19 | + } |
| 20 | + target.push(node); |
| 21 | +} |
| 22 | + |
| 23 | +function trimMarkdownBlankLines(value: string): string { |
| 24 | + return value.replace(/^(?:[ \t]*\r?\n)+/, "").replace(/(?:\r?\n[ \t]*)+$/, ""); |
| 25 | +} |
| 26 | + |
| 27 | +type MarkdownContainerLayout = { blankPrefix: string; continuationPrefix: string }; |
| 28 | + |
| 29 | +function resolveMarkdownContainerLayout(rendered: string): MarkdownContainerLayout | null { |
| 30 | + const currentLine = rendered.slice(rendered.lastIndexOf("\n") + 1); |
| 31 | + if (/^[ \t]+$/.test(currentLine)) { |
| 32 | + return { blankPrefix: "", continuationPrefix: currentLine }; |
| 33 | + } |
| 34 | + const quotePrefix = /^[ \t]{0,3}(?:>\s?)+/.exec(currentLine)?.[0] ?? ""; |
| 35 | + const remainder = currentLine.slice(quotePrefix.length); |
| 36 | + if (quotePrefix && !remainder) { |
| 37 | + return { blankPrefix: quotePrefix.trimEnd(), continuationPrefix: quotePrefix }; |
| 38 | + } |
| 39 | + const listMarker = /^ {0,3}(?:[-+*]|\d{1,9}[.)])[ \t]+$/.exec(remainder)?.[0]; |
| 40 | + if (listMarker) { |
| 41 | + return { |
| 42 | + blankPrefix: quotePrefix.trimEnd(), |
| 43 | + continuationPrefix: quotePrefix + " ".repeat(listMarker.length), |
| 44 | + }; |
| 45 | + } |
| 46 | + return null; |
| 47 | +} |
| 48 | + |
| 49 | +function renderInMarkdownContainer(block: string, layout: MarkdownContainerLayout): string { |
| 50 | + return block |
| 51 | + .split("\n") |
| 52 | + .map((line, index) => { |
| 53 | + if (index === 0) { |
| 54 | + return line; |
| 55 | + } |
| 56 | + return line ? layout.continuationPrefix + line : layout.blankPrefix; |
| 57 | + }) |
| 58 | + .join("\n"); |
| 59 | +} |
| 60 | + |
| 61 | +function stripMarkdownContainerLayout(block: string, layout: MarkdownContainerLayout): string { |
| 62 | + return block |
| 63 | + .split("\n") |
| 64 | + .map((line) => { |
| 65 | + if (line === layout.blankPrefix) { |
| 66 | + return ""; |
| 67 | + } |
| 68 | + return line.startsWith(layout.continuationPrefix) |
| 69 | + ? line.slice(layout.continuationPrefix.length) |
| 70 | + : line; |
| 71 | + }) |
| 72 | + .join("\n"); |
| 73 | +} |
| 74 | + |
| 75 | +function collectDetailsText(nodes: readonly DetailsNode[]): string { |
| 76 | + let text = ""; |
| 77 | + const pending = [...nodes].toReversed(); |
| 78 | + while (pending.length > 0) { |
| 79 | + const node = pending.pop(); |
| 80 | + if (typeof node === "string") { |
| 81 | + text += node; |
| 82 | + } else if (node) { |
| 83 | + pending.push(...node.children.toReversed()); |
| 84 | + } |
| 85 | + } |
| 86 | + return text; |
| 87 | +} |
| 88 | + |
| 89 | +function renderDetailsNodes(nodes: readonly DetailsNode[], depth = 0): string { |
| 90 | + let rendered = ""; |
| 91 | + for (const [index, node] of nodes.entries()) { |
| 92 | + if (typeof node === "string") { |
| 93 | + rendered += node; |
| 94 | + continue; |
| 95 | + } |
| 96 | + if (node.type === "summary") { |
| 97 | + rendered += |
| 98 | + depth >= MAX_DETAILS_RENDER_DEPTH |
| 99 | + ? collectDetailsText(node.children) |
| 100 | + : renderDetailsNodes(node.children, depth + 1); |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + const summary = node.children.find( |
| 105 | + (child): child is Exclude<DetailsNode, string> => |
| 106 | + typeof child !== "string" && child.type === "summary", |
| 107 | + ); |
| 108 | + const bodyNodes = node.children.filter((child) => child !== summary); |
| 109 | + const container = resolveMarkdownContainerLayout(rendered); |
| 110 | + const renderedBody = |
| 111 | + depth >= MAX_DETAILS_RENDER_DEPTH |
| 112 | + ? collectDetailsText(bodyNodes) |
| 113 | + : renderDetailsNodes(bodyNodes, depth + 1); |
| 114 | + const body = trimMarkdownBlankLines( |
| 115 | + container ? stripMarkdownContainerLayout(renderedBody, container) : renderedBody, |
| 116 | + ); |
| 117 | + const label = summary |
| 118 | + ? (depth >= MAX_DETAILS_RENDER_DEPTH |
| 119 | + ? collectDetailsText(summary.children) |
| 120 | + : renderDetailsNodes(summary.children, depth + 1) |
| 121 | + ).trim() |
| 122 | + : "Details"; |
| 123 | + const heading = `**${label || "Details"}**`; |
| 124 | + const block = body ? `${heading}\n\n${body}` : heading; |
| 125 | + if (container) { |
| 126 | + rendered += renderInMarkdownContainer(block, container); |
| 127 | + } else { |
| 128 | + if (rendered && !rendered.endsWith("\n\n")) { |
| 129 | + rendered += rendered.endsWith("\n") ? "\n" : "\n\n"; |
| 130 | + } |
| 131 | + rendered += block; |
| 132 | + } |
| 133 | + const next = nodes[index + 1]; |
| 134 | + if ( |
| 135 | + next !== undefined && |
| 136 | + next !== "" && |
| 137 | + (typeof next !== "string" || !next.startsWith("\n\n")) |
| 138 | + ) { |
| 139 | + rendered += typeof next === "string" && next.startsWith("\n") ? "\n" : "\n\n"; |
| 140 | + } |
| 141 | + } |
| 142 | + return rendered; |
| 143 | +} |
| 144 | + |
| 145 | +/** Flattens model-authored details blocks for transports without native disclosure widgets. */ |
| 146 | +export function flattenMarkdownDetails(text: string): string { |
| 147 | + if (!/<\s*\/?\s*(?:details|summary)(?=\s|>)/i.test(text)) { |
| 148 | + return text; |
| 149 | + } |
| 150 | + |
| 151 | + const root: DetailsNode[] = []; |
| 152 | + const stack: Array<Extract<DetailsNode, { type: "details" | "summary" }>> = []; |
| 153 | + // Accepted tradeoff: four-space-indented literal tags may flatten as details. |
| 154 | + // Fenced and inline code stay protected; models normally author code fences. |
| 155 | + const codeRegions = findCodeRegions(text); |
| 156 | + let cursor = 0; |
| 157 | + for (const match of text.matchAll(DETAILS_TAG_RE)) { |
| 158 | + const start = match.index ?? 0; |
| 159 | + if (isInsideCode(start, codeRegions)) { |
| 160 | + continue; |
| 161 | + } |
| 162 | + |
| 163 | + const target = stack.at(-1)?.children ?? root; |
| 164 | + appendNode(target, text.slice(cursor, start)); |
| 165 | + cursor = start + match[0].length; |
| 166 | + |
| 167 | + const type = match[2]?.toLowerCase(); |
| 168 | + if (type !== "details" && type !== "summary") { |
| 169 | + continue; |
| 170 | + } |
| 171 | + if (match[1]) { |
| 172 | + if ( |
| 173 | + type === "details" && |
| 174 | + stack.at(-1)?.type === "summary" && |
| 175 | + stack.at(-2)?.type === "details" |
| 176 | + ) { |
| 177 | + stack.pop(); |
| 178 | + } |
| 179 | + if (stack.at(-1)?.type === type) { |
| 180 | + stack.pop(); |
| 181 | + } |
| 182 | + continue; |
| 183 | + } |
| 184 | + |
| 185 | + const node = { type, children: [] } satisfies Exclude<DetailsNode, string>; |
| 186 | + appendNode(target, node); |
| 187 | + stack.push(node); |
| 188 | + } |
| 189 | + appendNode(stack.at(-1)?.children ?? root, text.slice(cursor)); |
| 190 | + return renderDetailsNodes(root); |
| 191 | +} |
0 commit comments