Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

byteplus-doc.ts

46 lines1.5 KB

1/**
2 * A docs.byteplus.com page as the doc center renders it: `_ROUTER_DATA`
3 * embedding a Lark doc. Each block is a text line (`## ` makes it a
4 * heading) or a table of cell strings, header row first.
5 */
6export function docFixtureHtml(
7 blocks: Array<string | Array<Array<string>>>,
8): string {
9 const ops: Array<unknown> = []
10 const data: Record<string, unknown> = { '0': { ops } }
11 blocks.forEach((block, t) => {
12 if (typeof block === 'string') {
13 const heading = block.startsWith('## ')
14 ops.push(
15 { insert: '*', attributes: heading ? { heading: 'h2' } : {} },
16 { insert: `${block.replace(/^## /, '')}\n` },
17 )
18 return
19 }
20 ops.push({ insert: '*', attributes: { aceTable: `rows${t} cols${t}` } })
21 data[`rows${t}`] = {
22 ops: block.map((_, index) => ({ insert: { id: `t${t}r${index}` } })),
23 zoneType: 'R',
24 }
25 data[`cols${t}`] = {
26 ops: (block[0] ?? []).map((_, index) => ({
27 insert: { id: `t${t}c${index}` },
28 })),
29 zoneType: 'C',
30 }
31 block.forEach((cells, row) => {
32 cells.forEach((text, column) => {
33 data[`xt${t}r${row}xt${t}c${column}`] = {
34 ops: [
35 { insert: '*', attributes: { lmkr: '1' } },
36 { insert: `${text}\n` },
37 ],
38 }
39 })
40 })
41 })
42 const router = {
43 loaderData: { page: { curDoc: { Content: JSON.stringify({ data }) } } },
44 }
45 return `<script>window._ROUTER_DATA = ${JSON.stringify(router)}</script>`