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

byteplus-pricing-doc.ts

85 lines2.8 KB

1import type { ByteplusDoc } from '../byteplus-pricing.ts'
2​
3/**
4 * One pricing-page table, header row first. `tab` puts it in a tab strip
5 * with that title; `folded` puts it in a folded section ("Retiring
6 * models"). A merge is `[row indexes, column indexes]`: put its text in
7 * its first cell and leave the others blank.
8 */
9export interface PricingTable {
10 rows: Array<Array<string>>
11 tab?: string
12 folded?: boolean
13 merges?: Array<[Array<number>, Array<number>]>
15​
16/**
17 * The ModelArk pricing page's Lark doc as docs.byteplus.com embeds it on
18 * 2026-10-23: tables in zone `0`, in a `tabsZoneId` strip of `tabContent`
19 * zones, or in a folded `zoneId` section; merged cells listed by
20 * `mergeCellID` on their rows and columns.
21 */
22export function pricingDoc(tables: Array<PricingTable>): ByteplusDoc {
23 const top: Array<{ insert: string; attributes: Record<string, string> }> = []
24 const data: ByteplusDoc['data'] = { '0': { ops: top } }
25 tables.forEach(({ rows, tab, folded, merges = [] }, t) => {
26 const table = { insert: '*', attributes: { aceTable: `rows${t} cols${t}` } }
27 let placed: { insert: string; attributes: Record<string, string> } = table
28 if (tab !== undefined) {
29 data[`tabs${t}`] = {
30 ops: [
31 {
32 insert: ' ',
33 attributes: { tabContent: 'true', title: tab, zoneId: `pane${t}` },
34 },
35 ],
36 }
37 data[`pane${t}`] = { ops: [table] }
38 placed = { insert: ' ', attributes: { tabsZoneId: `tabs${t}` } }
39 }
40 if (folded) {
41 data[`fold${t}`] = { ops: [placed] }
42 placed = {
43 insert: ' ',
44 attributes: { collapse: 'container', zoneId: `fold${t}` },
45 }
46 }
47 top.push(placed)
48 const line = (prefix: string, index: number, side: 0 | 1) => {
49 const ids = merges.flatMap((merge, m) =>
50 merge[side].includes(index) ? [`m${String(t)}x${String(m)}`] : [],
51 )
52 return {
53 insert: { id: `t${String(t)}${prefix}${String(index)}` },
54 ...(ids.length > 0 && { attributes: { mergeCellID: ids.join(',') } }),
55 }
56 }
57 data[`rows${t}`] = {
58 ops: rows.map((_, index) => line('r', index, 0)),
59 zoneType: 'R',
60 }
61 data[`cols${t}`] = {
62 ops: (rows[0] ?? []).map((_, index) => line('c', index, 1)),
63 zoneType: 'C',
64 }
65 rows.forEach((cells, r) => {
66 cells.forEach((text, c) => {
67 data[`xt${t}r${r}xt${t}c${c}`] = {
68 ops: [
69 { insert: '*', attributes: { lmkr: '1' } },
70 { insert: `${text}\n` },
71 ],
72 }
73 })
74 })
75 })
76 return { data }
78​
79/** `doc` inside the page's `window._ROUTER_DATA`. */
80export function routerHtml(doc: ByteplusDoc): string {
81 const router = {
82 loaderData: { page: { curDoc: { Content: JSON.stringify(doc) } } },
83 }
84 return `<script>window._ROUTER_DATA = ${JSON.stringify(router)}</script>`