Files
Emir KarabegandWaleed Latif 7860fbe8cc feat(docs): rebuild documentation with the shared Sim design system (#9459)
* feat(docs): rebuild documentation with the Paper design system

* fix(docs): verify nested tabs and fresh-checkout CI setup

* chore(docs): refresh preservation evidence against latest staging

* fix(docs): align shared navigation and controls with Paper

* fix(docs): share active heading across responsive outlines

* fix(docs): track the final section at the end of the page

* fix(docs): preserve sidebar scroll in a persistent layout

* fix(docs): restore live screenshot backgrounds and framing

* fix(docs): finish layout polish and clean implementation references

* fix(docs): align shared dark theme and overlay states

* fix(docs): remove dark borders from platform screenshots

* test(docs): benchmark production navigation and image delivery in CI

* fix(docs): preserve machine content and production reading contracts

* fix(docs): retain readable tabs without JavaScript and verify current staging

* fix(docs): expose root response schemas and generate clean-checkout types

* fix(docs): resolve no-script tabs in the preflight cascade layer

* perf(docs): import only the search schema constructors

* fix(docs): preserve retrieval excerpts and reduce reading overhead

* fix(docs): reuse in-flight intent prefetches during navigation

* docs: record production readiness evidence and release requirements

* docs: clarify optimizer measurement provenance

* docs: preserve latest staging Search assets and compatibility evidence

* fix(docs): polish responsive controls and keep search indexing current

* test(docs): select the visible API example in browser checks

* test(docs): execute native examples against streaming and binary responses

* fix(docs): stream native API responses without buffering

* test(docs): cover review regressions through public interfaces

* fix(docs): preserve accessible examples and gate index publication

* fix(docs): finish accessible controls and cached content validation

* fix(docs): validate integration parents and report partial search failures

* fix(docs): refine navigation and compact sample controls

* fix(docs): isolate synchronous corpus validation from HTTP readers

* fix(docs): restore Paper interaction styling and verify responsive controls

* fix(docs): score each search page once per retrieval source

* fix(docs): attribute benchmark metrics to their source document

---------

Co-authored-by: Waleed Latif <walif6@gmail.com>
2026-10-30 17:06:09 -07:00

288 lines
9.9 KiB
TypeScript

import { isRecordLike, toArray, toRecord } from '@sim/utils/object'
import type { Nodes, PhrasingContent, Root, RootContent, TableRow } from 'mdast'
import { gfmToMarkdown } from 'mdast-util-gfm'
import { toMarkdown } from 'mdast-util-to-markdown'
import { toString as mdastToString } from 'mdast-util-to-string'
import { SKIP, visit } from 'unist-util-visit'
import { BLOCK_DISPLAY_WORKFLOWS } from '@/components/workflow-preview/block-display-workflows'
import * as workflowExamples from '@/components/workflow-preview/examples'
import { decodeOutputValues } from '@/lib/content/output'
const examples: Record<string, unknown> = workflowExamples
/** Evaluates only literal content data; documentation expressions never execute during serialization. */
function literal(expression: unknown): unknown {
const node = toRecord(expression)
if (node.type === 'Literal') return node.value
if (node.type === 'Identifier') {
if (typeof node.name === 'string' && node.name in examples) return examples[node.name]
throw new Error(`Unknown documentation data reference: ${String(node.name)}`)
}
if (node.type === 'ArrayExpression') return toArray(node.elements).map(literal)
if (node.type === 'ObjectExpression')
return Object.fromEntries(
toArray(node.properties).map((raw) => {
const property = toRecord(raw)
if (
property.type !== 'Property' ||
property.computed ||
property.method ||
property.kind !== 'init'
)
throw new Error('Only literal object properties are supported in documentation')
const key = toRecord(property.key)
return [key.name ?? key.value, literal(property.value)]
})
)
if (node.type === 'UnaryExpression' && node.operator === '-')
return -Number(literal(node.argument))
throw new Error(`Unsupported documentation expression: ${String(node.type)}`)
}
function expressionValue(value: unknown): unknown {
const expression = toRecord(value)
const program = toRecord(toRecord(expression.data).estree)
const statement = toRecord(toArray(program.body)[0])
if (
!statement.type &&
typeof expression.value === 'string' &&
expression.value.trim().startsWith('/*')
)
return undefined
return literal(statement.expression)
}
function attributes(node: Nodes): Record<string, unknown> {
const record = toRecord(node)
return Object.fromEntries(
toArray(record.attributes)
.map(toRecord)
.filter((attribute) => attribute.type === 'mdxJsxAttribute')
.map((attribute) => [
String(attribute.name),
isRecordLike(attribute.value)
? expressionValue(attribute.value)
: (attribute.value ?? true),
])
)
}
const paragraph = (text: string): Nodes => ({
type: 'paragraph',
children: [{ type: 'text', value: text }],
})
const json = (value: unknown): Nodes => ({
type: 'code',
lang: 'json',
value: JSON.stringify(value, null, 2),
})
function isPhrasing(node: Nodes): node is PhrasingContent {
return [
'text',
'emphasis',
'strong',
'delete',
'inlineCode',
'break',
'image',
'imageReference',
'link',
'linkReference',
'footnoteReference',
'html',
].includes(node.type)
}
function inlineContent(nodes: Nodes[]): PhrasingContent[] {
return nodes.flatMap((node): PhrasingContent[] => {
if (node.type === 'paragraph') return inlineContent(node.children)
if (node.type === 'break') return [{ type: 'html', value: '<br />' }]
if (isPhrasing(node)) return [node]
throw new Error(`Unsupported block inside inline documentation content: ${node.type}`)
})
}
/** Converts every authored component to useful Markdown while retaining all inactive content. */
export function serializeMarkdown(tree: Root): string {
const convertNode = (node: Nodes, tabLabel?: string): Nodes[] => {
if (node.type === 'mdxjsEsm') return []
if (node.type === 'mdxFlowExpression' || node.type === 'mdxTextExpression') {
const value = expressionValue(node)
return value === undefined ? [] : [{ type: 'text', value: String(value) }]
}
if (node.type === 'mdxJsxFlowElement' || node.type === 'mdxJsxTextElement') {
const props = attributes(node)
const children = node.children.flatMap((child) => convert(child))
switch (node.name) {
case 'WorkflowPreview':
return [paragraph('Workflow configuration'), json(props.workflow)]
case 'BlockPreview': {
const type = props.type
if (typeof type !== 'string' || !Object.hasOwn(BLOCK_DISPLAY_WORKFLOWS, type))
throw new Error(`Unknown documentation block preview: ${String(type)}`)
const workflow = BLOCK_DISPLAY_WORKFLOWS[type]
return [paragraph(`${type} block configuration`), json(workflow)]
}
case 'OutputBundle':
return [
paragraph(`Output from ${String(props.blockName)}`),
json(decodeOutputValues(props.values)),
]
case 'Image':
case 'img':
return [
{
type: 'image',
url: String(props.src),
alt: String(props.alt ?? ''),
...(typeof props.caption === 'string' ? { title: props.caption } : {}),
},
]
case 'IntroductionHero':
return [
{
type: 'image',
url: '/static/introduction/platform.webp',
alt: String(props.alt ?? 'Sim workspace'),
},
]
case 'Card':
case 'IntroductionLink':
return [
{
type: 'paragraph',
children: [
{
type: 'link',
url: String(props.href),
children: [{ type: 'text', value: String(props.title) }],
},
],
},
...children,
]
case 'Callout':
return [
paragraph(
`${typeof props.title === 'string' ? props.title : props.type === 'warn' || props.type === 'warning' ? 'Warning' : 'Note'}:`
),
...children,
]
case 'Tabs': {
let index = 0
return node.children.flatMap((child) =>
convert(
child,
(child.type === 'mdxJsxFlowElement' || child.type === 'mdxJsxTextElement') &&
child.name === 'Tab'
? String(toArray(props.items)[index++] ?? 'Configuration')
: undefined
)
)
}
case 'Tab':
return [
paragraph(`Tab: ${String(props.title ?? props.value ?? tabLabel ?? 'Configuration')}`),
...children,
]
case 'TabsList':
case 'TabsTrigger':
return []
case 'ReferenceDetails':
return typeof props.label === 'string' ? [paragraph(props.label), ...children] : children
case 'a': {
if (typeof props.href === 'string')
return [
{
type: 'link',
url: props.href,
children: inlineContent(children),
},
]
return typeof props.id === 'string'
? [
{ type: 'html', value: `<a id="${props.id.replace(/"/g, '&quot;')}"></a>` },
...children,
]
: children
}
case 'code':
return [
{ type: 'inlineCode', value: children.map((child) => mdastToString(child)).join('') },
]
case 'br':
return [{ type: 'break' }]
case 'table': {
const rows: Nodes[] = []
visit(node, (child) => {
if (
(child.type === 'mdxJsxFlowElement' || child.type === 'mdxJsxTextElement') &&
child.name === 'tr'
) {
rows.push(child)
return SKIP
}
})
return [
{
type: 'table',
children: rows.map((row): TableRow => {
const cells: TableRow['children'] = []
visit(row, (cell) => {
if (
(cell.type === 'mdxJsxFlowElement' || cell.type === 'mdxJsxTextElement') &&
(cell.name === 'td' || cell.name === 'th')
) {
cells.push({ type: 'tableCell', children: inlineContent(convert(cell)) })
return SKIP
}
})
return { type: 'tableRow', children: cells }
}),
},
]
}
default:
return children
}
}
if (node.type === 'heading' && typeof node.data?.hProperties?.id === 'string')
return [
{
...node,
children: [
...inlineContent(node.children.flatMap((child) => convert(child))),
{ type: 'text', value: ` [#${node.data.hProperties.id}]` },
],
},
]
if ('children' in node)
return [{ ...node, children: node.children.flatMap((child) => convert(child)) } as Nodes]
return [node]
}
const convert = (node: Nodes, tabLabel?: string): Nodes[] => {
const converted = convertNode(node, tabLabel)
if (node.type !== 'mdxJsxFlowElement' && node.type !== 'mdxFlowExpression') return converted
const blocks: Nodes[] = []
for (const child of converted) {
if (isPhrasing(child) && child.type !== 'html') {
const previous = blocks.at(-1)
if (previous?.type === 'paragraph') previous.children.push(child)
else blocks.push({ type: 'paragraph', children: [child] })
} else blocks.push(child)
}
return blocks
}
const markdownTree: Root = {
type: 'root',
children: tree.children.flatMap((child) => convert(child)) as RootContent[],
}
return toMarkdown(markdownTree, {
extensions: [gfmToMarkdown({ tablePipeAlign: false })],
fences: true,
bullet: '-',
listItemIndent: 'one',
})
}