Open History to see the last commit
1/**
2 * `<DocTable>` tables from a Kimi docs page (platform.kimi.com and
3 * platform.kimi.ai, both Mintlify). As of 2026-10-20 the `.md` export of a
4 * page renders each `<DocTable>` as an empty `<table>`, so the pricing
5 * tables are read from the HTML page instead. Its RSC payload embeds the
6 * page's compiled MDX: `_jsx(Heading,{level:`2`,…,children:`…`})` (or
7 * `_components.h2`) for each heading and
8 * `_jsx(DocTable,{columns:[…],rows:[[…]]})` for each table, with every
9 * string a template literal. The payload carries the body twice: first
10 * with a rendering `DocTable`, then with `DocTable=()=>null` (on
11 * platform.kimi.ai that copy has no `rows`). Only the first copy, up to its
12 * `_provideComponents(`, is read.
13 */
14import { closing, literalEnd } from './compiled-mdx.ts'
15
16const LABEL = 'kimi docs'
17
18export interface KimiDocTable {
19 /** Text of the nearest level-1/2 heading above the table; null when not plain text. */
20 section: string | null
21 /** Text of the `###` heading under that section; null when none or not plain text. */
22 subsection: string | null
23 /** Column titles; null for a column whose title is not plain text. */
24 titles: Array<string | null>
25 /**
26 * A cell is its text: a template literal, or a fragment of them joined
27 * (`<>{"$"}0.16</>` compiles to `_jsxs(_Fragment,{children:[`$`,`0.16`]})`,
28 * read as `$0.16`). Anything else is null.
29 */
30 rows: Array<Array<string | null>>
31}
32
33/** A template literal with no escape or `${`: its text is the source text. */
34const LITERAL = '`((?:[^`\\\\$]|\\$(?!\\{))*)`'
35const TEXT = new RegExp(`^${LITERAL}$`)
36const FRAGMENT = /^_jsxs?\(_Fragment,\{children:([\s\S]*)\}\)$/
37const HEADING = /_jsxs?\((?:Heading|_components\.h(\d)),\{/g
38const TITLE = new RegExp(`^\\{title:${LITERAL}`)
39const TABLE = /_jsxs?\(DocTable,\{/g
40
41/** The comma-separated items between `open` and its closing bracket. */
42function items(source: string, open: number): Array<string> {
43 const close = closing(source, open, LABEL)
44 const out: Array<string> = []
45 let start = open + 1
46 for (let i = start; i < close; i++) {
47 const char = source[i]
48 if (char === '`') {
49 i = literalEnd(source, i, LABEL)
50 } else if (char === '[' || char === '{' || char === '(') {
51 i = closing(source, i, LABEL)
52 } else if (char === ',') {
53 out.push(source.slice(start, i))
54 start = i + 1
55 }
56 }
57 if (close > start) out.push(source.slice(start, close))
58 return out
59}
60
61function cell(item: string): string | null {
62 const text = TEXT.exec(item)
63 if (text) return text[1] ?? ''
64 const children = FRAGMENT.exec(item)?.[1]
65 if (children === undefined) return null
66 const parts = children.startsWith('[') ? items(children, 0) : [children]
67 const texts = parts.map((part) => TEXT.exec(part)?.[1])
68 return texts.every((part) => part !== undefined) ? texts.join('') : null
69}
70
71/** `key:value` props of the object literal opened at `open`. */
72function props(source: string, open: number): Map<string, string> {
73 const out = new Map<string, string>()
74 for (const prop of items(source, open)) {
75 const colon = prop.indexOf(':')
76 if (colon > 0) out.set(prop.slice(0, colon), prop.slice(colon + 1))
77 }
78 return out
79}
80
81/**
82 * Every DocTable on the page, in order, under the headings above it.
83 * Throws when the page has no compiled MDX body or no DocTable, when a
84 * heading's level is unreadable, when a DocTable's props are not exactly
85 * `columns` then `rows` or a row is not an array, or when the body is
86 * malformed.
87 */
88export function parseKimiDocTables(html: string): Array<KimiDocTable> {
89 const start = html.search(/_jsxs?\((?:Heading|DocTable|_components\.h\d),/)
90 const end = start < 0 ? -1 : html.indexOf('_provideComponents(', start)
91 if (end < 0) throw new Error('kimi docs: page has no compiled MDX body')
92 const body = html.slice(start, end)
93
94 const marks: Array<
95 | { at: number; level: number; text: string | null }
96 | { at: number; table: KimiDocTable }
97 > = []
98 for (const match of body.matchAll(HEADING)) {
99 const heading = props(body, match.index + match[0].length - 1)
100 const level = Number(
101 match[1] ?? TEXT.exec(heading.get('level') ?? '')?.[1] ?? NaN,
102 )
103 if (!Number.isInteger(level)) {
104 throw new Error('kimi docs: heading level is unreadable')
105 }
106 const children = heading.get('children')
107 const text =
108 children === undefined ? null : (TEXT.exec(children)?.[1] ?? null)
109 marks.push({ at: match.index, level, text })
110 }
111 for (const match of body.matchAll(TABLE)) {
112 const table = [...props(body, match.index + match[0].length - 1)]
113 const [columns, rows] = table.map(([, value]) => value)
114 if (
115 table.map(([key]) => key).join() !== 'columns,rows' ||
116 !columns?.startsWith('[') ||
117 !rows?.startsWith('[')
118 ) {
119 throw new Error('kimi docs: DocTable props are not columns, rows')
120 }
121 const titles = items(columns, 0).map(
122 (column) => TITLE.exec(column)?.[1] ?? null,
123 )
124 const cells = items(rows, 0).map((row) => {
125 if (!row.startsWith('[')) {
126 throw new Error('kimi docs: DocTable row is not an array')
127 }
128 return items(row, 0).map(cell)
129 })
130 marks.push({
131 at: match.index,
132 table: { section: null, subsection: null, titles, rows: cells },
133 })
134 }
135 marks.sort((a, b) => a.at - b.at)
136
137 const tables: Array<KimiDocTable> = []
138 let section: string | null = null
139 let subsection: string | null = null
140 for (const mark of marks) {
141 if ('table' in mark) {
142 tables.push({ ...mark.table, section, subsection })
143 } else if (mark.level <= 2) {
144 section = mark.text
145 subsection = null
146 } else if (mark.level === 3) {
147 subsection = mark.text
148 }
149 }
150 if (tables.length === 0) throw new Error('kimi docs: page has no DocTable')
151 return tables
152}