Open History to see the last commit
1/**
2 * Bracket scanning over a docs page's compiled MDX, where every string is a
3 * template literal with no `${…}` (`_jsx(Component,{key:`value`,…})`).
4 * Shared by readers of pages whose `.md` export renders a component
5 * without its values (Kimi `<DocTable>`, Cerebras `<ModelInfo>`, MiniMax
6 * `<Tab>`) or without its nesting (Novita `<ParamField>`).
7 */
8
9/** Index of the backtick closing the template literal opened at `open`. */
10export function literalEnd(
11 source: string,
12 open: number,
13 label: string,
14): number {
15 for (let i = open + 1; i < source.length; i++) {
16 if (source[i] === '\\') i++
17 else if (source[i] === '`') return i
18 }
19 throw new Error(`${label}: unterminated template literal`)
20}
21
22/** Index of the bracket closing the one at `open`, skipping literals. */
23export function closing(source: string, open: number, label: string): number {
24 let depth = 0
25 for (let i = open; i < source.length; i++) {
26 const char = source[i]
27 if (char === '`') {
28 i = literalEnd(source, i, label)
29 } else if (char === '[' || char === '{' || char === '(') {
30 depth++
31 } else if (char === ']' || char === '}' || char === ')') {
32 depth--
33 if (depth === 0) return i
34 }
35 }
36 throw new Error(`${label}: unbalanced brackets`)
37}