Files
supabase/apps/docs/internals/generate-reference-markdown.ts
Alexis Aguilar 6ac0f3f231 fix(docs): include request bodies in the Management API markdown reference (#54343)
## Problem

Agents can't find request body fields for Management API endpoints. In
an eval for the access control guide
([DOCS-1489](https://linear.app/supabase/issue/DOCS-1489)), an agent
read the [Create organization
invitations](https://supabase.com/docs/reference/api/v2-create-organization-invitations)
page and concluded the body accepts only an email. It refused to invite
someone scoped to one project, even though the API accepts `role` and
`projects`.

Reference pages have no `.md` version. Their markdown 404 points agents
to the markdown reference (`llms/api.txt`), which lists path and query
parameters but no request body for any endpoint.

Resolves part of
[DOCS-1823](https://linear.app/supabase/issue/DOCS-1823).

## Solution

Each endpoint with a request body gets a **Request body** section in the
markdown reference. It lists every field as a nested list with its type,
whether it's required, its description, accepted values, defaults, and
constraints. Unions list each option by name, and map-like objects show
the shape of their values.

This changes only the generated markdown. Reference pages render the
same as before.

Reference pages don't have their own `.md` versions yet. That's tracked
in [DOCS-1332](https://linear.app/supabase/issue/DOCS-1332). The new
renderer takes one endpoint's request body and returns markdown, so a
per-page generator can reuse it.

## Follow-ups

- **Reference page HTML (rest of DOCS-1823):** nested schema fields
still reach the HTML only after someone opens their control. Moving
these controls to native `<details>` also needs three fixes found in
review:
- A union option puts an `<li>` inside another `<li>`, which breaks the
list once the content is in the HTML.
- The **Items** control for an array of union items, such as the
notebook `cells` array, opens an empty panel.
- The new summary styles repeat the current trigger's styles. They
should share one base.
- **Parameters:** path and query parameters still use the older one-line
format, so their accepted values don't appear in the markdown reference.
For example, `continent` on the available regions endpoint lists no
values.
- **Shared schema helpers:** the renderer copies `asSchemaArray` from
`Reference.ui.tsx` and adds its own schema type. Sharing both through
`Reference.api.utils.ts` keeps the HTML and the markdown in step.

## Review category (docs PRs only)

- [ ] Critical: needs a review as soon as possible
- [ ] Urgent: the feature ships soon, or it's blocking something else
- [x] Moderate: good to have soon, not urgent
- [ ] Low: get to it when you can

## Review instructions

1. Open the [Management API markdown reference on the
preview](https://docs-git-alexisa-docs-1823-supabase.vercel.app/docs/markdown/reference/api.md).
On production, the same file is served at `/llms/api.txt`.
2. Under **Creates organization invitations**, see the **Request body**
section with `data`, `type`, `attributes`, `email`, `role`, `projects`,
and `require_sso`.
3. Under **Assign or change an organization member role**, see
`data.attributes.role` and `projects`.

## Checklist

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-12-01 11:15:33 -07:00

487 lines
15 KiB
TypeScript

import fs from 'node:fs/promises'
import path from 'node:path'
import { isFeatureEnabled, type Feature } from 'common/enabled-features'
import matter from 'gray-matter'
import yaml from 'js-yaml'
import { fromMarkdown } from 'mdast-util-from-markdown'
import { gfmFromMarkdown, gfmToMarkdown } from 'mdast-util-gfm'
import { toMarkdown } from 'mdast-util-to-markdown'
import { gfm } from 'micromark-extension-gfm'
import { renderRequestBodyMarkdown, type RequestBody } from './api-request-body-markdown'
import { addBaseUrlPrefix } from './internal-links'
const GENERATED = path.join(process.cwd(), 'features/docs/generated')
const OUT_DIR = path.join(process.cwd(), 'public/markdown/reference')
const MDX_ROOT = path.join(process.cwd(), 'docs/ref')
type Section = {
id?: string
title?: string
slug?: string
type?: string
items?: Section[]
}
type Example = { name?: string; code?: string }
type LegacyFn = {
id: string
$ref?: string
title?: string
description?: string
notes?: string
examples?: Example[]
}
type TypeComment = { shortText?: string; text?: string; examples?: Example[] }
type TypeSpec = {
methods: Record<string, { comment?: TypeComment }>
variables: Record<string, { comment?: TypeComment }>
}
type RefBase = {
title: string
outFile: string
/** Folder under docs/ref/ that holds .mdx sections for `type: "markdown"` entries. */
mdxDir: string
/**
* Enabled-features flag gating this reference. When set and the feature is
* disabled in enabled-features.json, the file is not generated.
*/
feature?: Feature
}
type SdkLegacyRef = RefBase & {
kind: 'sdk-legacy'
sectionsPath: string
functionsPath: string
}
type SdkNewRef = RefBase & {
kind: 'sdk-new'
contentDir: string
}
type ApiRef = RefBase & {
kind: 'api'
sectionsPath: string
endpointsByIdPath: string
}
type CliRef = RefBase & {
kind: 'cli'
sectionsPath: string
cliSpecPath: string
}
type Ref = SdkLegacyRef | SdkNewRef | ApiRef | CliRef
const REFERENCES: Ref[] = [
{
kind: 'api',
title: 'Management API Reference',
outFile: 'api.md',
mdxDir: path.join(MDX_ROOT, 'api'),
sectionsPath: path.join(GENERATED, 'api.latest.sections.json'),
endpointsByIdPath: path.join(GENERATED, 'api.latest.endpointsById.json'),
},
{
kind: 'cli',
title: 'Supabase CLI Reference',
outFile: 'cli.md',
mdxDir: path.join(MDX_ROOT, 'cli'),
sectionsPath: path.join(GENERATED, 'cli.latest.sections.json'),
cliSpecPath: path.join(process.cwd(), 'spec/cli_v1_commands.yaml'),
},
{
kind: 'sdk-new',
title: 'JavaScript Client Library Reference',
outFile: 'js.md',
mdxDir: path.join(MDX_ROOT, 'javascript'),
contentDir: path.join(process.cwd(), 'content/reference/javascript/v2'),
},
{
kind: 'sdk-new',
title: 'Dart Client Library Reference',
outFile: 'dart.md',
mdxDir: path.join(MDX_ROOT, 'dart'),
contentDir: path.join(process.cwd(), 'content/reference/dart/v2'),
feature: 'sdk:dart',
},
{
kind: 'sdk-new',
title: 'Supabase Server SDK Reference',
outFile: 'server.md',
mdxDir: path.join(MDX_ROOT, 'server'),
contentDir: path.join(process.cwd(), 'content/reference/server/v1'),
},
{
kind: 'sdk-new',
title: 'Supabase Middleware SDK Reference',
outFile: 'middleware.md',
mdxDir: path.join(MDX_ROOT, 'middleware'),
contentDir: path.join(process.cwd(), 'content/reference/middleware/v1'),
},
{
kind: 'sdk-new',
title: 'TanStack DB Reference',
outFile: 'tanstack_db.md',
mdxDir: path.join(MDX_ROOT, 'tanstack_db'),
contentDir: path.join(process.cwd(), 'content/reference/tanstack_db/v1'),
},
{
kind: 'sdk-legacy',
title: 'Kotlin Client Library Reference',
outFile: 'kotlin.md',
mdxDir: path.join(MDX_ROOT, 'kotlin'),
sectionsPath: path.join(GENERATED, 'kotlin.v1.sections.json'),
functionsPath: path.join(GENERATED, 'kotlin.v1.functions.json'),
feature: 'sdk:kotlin',
},
{
kind: 'sdk-legacy',
title: 'Python Client Library Reference',
outFile: 'python.md',
mdxDir: path.join(MDX_ROOT, 'python'),
sectionsPath: path.join(GENERATED, 'python.v2.sections.json'),
functionsPath: path.join(GENERATED, 'python.v2.functions.json'),
feature: 'sdk:python',
},
{
kind: 'sdk-legacy',
title: 'Swift Client Library Reference',
outFile: 'swift.md',
mdxDir: path.join(MDX_ROOT, 'swift'),
sectionsPath: path.join(GENERATED, 'swift.v2.sections.json'),
functionsPath: path.join(GENERATED, 'swift.v2.functions.json'),
feature: 'sdk:swift',
},
{
kind: 'sdk-legacy',
title: 'C# Client Library Reference',
outFile: 'csharp.md',
mdxDir: path.join(MDX_ROOT, 'csharp'),
sectionsPath: path.join(GENERATED, 'csharp.v0.sections.json'),
functionsPath: path.join(GENERATED, 'csharp.v0.functions.json'),
feature: 'sdk:csharp',
},
]
function flatten(sections: Section[]): Section[] {
return sections.flatMap((s) => (s.items ? [s, ...flatten(s.items)] : [s]))
}
async function readJson<T>(filePath: string): Promise<T> {
return JSON.parse(await fs.readFile(filePath, 'utf8')) as T
}
function renderExamples(examples: Example[] | undefined): string {
if (!examples?.length) return ''
const blocks = examples
.map((ex) => {
const header = ex.name ? `#### ${ex.name}\n\n` : ''
const code = (ex.code ?? '').trim()
return code ? `${header}${code}` : header.trim()
})
.filter(Boolean)
return blocks.length ? `### Examples\n\n${blocks.join('\n\n')}` : ''
}
function renderFunctionSection(args: {
title: string
description?: string
notes?: string
examples?: Example[]
}): string {
const parts = [`## ${args.title}`]
if (args.description?.trim()) parts.push(args.description.trim())
if (args.notes?.trim()) parts.push(args.notes.trim())
const examples = renderExamples(args.examples)
if (examples) parts.push(examples)
return parts.join('\n\n')
}
/** Remove the minimum common leading whitespace from all non-empty lines. */
function dedentBlock(text: string): string {
const lines = text.split('\n')
const nonEmpty = lines.filter((l) => /\S/.test(l))
if (!nonEmpty.length) return text
const minIndent = Math.min(...nonEmpty.map((l) => (l.match(/^([ \t]*)/) ?? ['', ''])[1].length))
if (!minIndent) return text
return lines.map((l) => l.slice(minIndent)).join('\n')
}
/**
* Strip MDX comments and JSX component tags while keeping inner prose/code.
* Outside code fences, leading whitespace from JSX nesting is removed. Inside
* code fences, the body is dedented so the closing fence sits flush left.
*/
function stripMdxJsx(content: string): string {
const segments = content.split(/(```[\s\S]*?```)/g)
return segments
.map((seg, i) => {
if (i % 2 === 0) {
let prose = seg
prose = prose.replace(/\{\/\*[\s\S]*?\*\/\}/g, '')
prose = prose.replace(/<[$A-Z][\w.]*(?:\s[^>]*)?\s*\/>/gs, '')
prose = prose.replace(/<[$A-Z][\w.]*(?:\s[^>]*)?\s*>/gs, '')
prose = prose.replace(/<\/[$A-Z][\w.]*>/g, '')
prose = prose.replace(/<\/?(?:div|a|span|p|h[1-6])(?:\s[^>]*)?\s*>/g, '')
return prose.replace(/^[ \t]+/gm, '')
}
return seg.replace(
/^(```[^\n]*\n)([\s\S]*?)(\n[ \t]*```)$/,
(_, open, body) => open + dedentBlock(body) + '\n```'
)
})
.join('')
.replace(/^[^\S\n]+$/gm, '')
.replace(/\n{3,}/g, '\n\n')
.trim()
}
/**
* Read a `type: "markdown"` section's .mdx file and convert it to plain
* markdown. Returns null if the file doesn't exist or has no usable content.
*/
async function renderMarkdownSection(mdxDir: string, section: Section): Promise<string | null> {
if (!section.slug) return null
let raw: string
try {
raw = await fs.readFile(path.join(mdxDir, `${section.slug}.mdx`), 'utf8')
} catch {
return null
}
const { content, data } = matter(raw)
const links: Array<{ title: string; href: string }> = Array.isArray(data.links) ? data.links : []
const body = [
stripMdxJsx(content),
links.length
? `## Additional links\n\n${links.map((l) => `- [${l.title}](${l.href})`).join('\n')}`
: '',
]
.filter(Boolean)
.join('\n\n')
if (!body) return null
const heading = data.hideTitle
? null
: ((typeof data.title === 'string' ? data.title : section.title) ?? null)
return heading ? `## ${heading}\n\n${body}` : body
}
async function renderSdkLegacy(ref: SdkLegacyRef, sharedTypeSpec: TypeSpec): Promise<string> {
const [sections, functions] = await Promise.all([
readJson<Section[]>(ref.sectionsPath),
readJson<LegacyFn[]>(ref.functionsPath),
])
const fnsById = new Map(functions.map((fn) => [fn.id, fn]))
const methodsByRef = new Map(Object.entries(sharedTypeSpec.methods ?? {}))
const parts: string[] = [`# ${ref.title}`]
for (const section of flatten(sections)) {
if (section.type === 'category' && section.title) {
parts.push(`## ${section.title}`)
continue
}
if (section.type === 'markdown') {
const md = await renderMarkdownSection(ref.mdxDir, section)
if (md) parts.push(md)
continue
}
if (section.type !== 'function' || !section.id) continue
const fn = fnsById.get(section.id)
if (!fn) continue
const tsm = fn.$ref ? methodsByRef.get(fn.$ref) : undefined
parts.push(
renderFunctionSection({
title: fn.title || section.title || fn.id,
description: fn.description ?? tsm?.comment?.shortText,
notes: fn.notes,
examples: fn.examples ?? tsm?.comment?.examples,
})
)
}
return parts.join('\n\n') + '\n'
}
async function renderSdkNew(ref: SdkNewRef): Promise<string> {
const [sections, functions, typeSpec] = await Promise.all([
readJson<Section[]>(path.join(ref.contentDir, 'sections.json')),
readJson<LegacyFn[]>(path.join(ref.contentDir, 'functions.json')),
readJson<TypeSpec>(path.join(ref.contentDir, 'typeSpec.json')),
])
const fnsById = new Map(functions.map((fn) => [fn.id, fn]))
const methods = typeSpec.methods ?? {}
const variables = typeSpec.variables ?? {}
const parts: string[] = [`# ${ref.title}`]
for (const section of flatten(sections)) {
if (section.type === 'category' && section.title) {
parts.push(`## ${section.title}`)
continue
}
if (section.type === 'markdown') {
const md = await renderMarkdownSection(ref.mdxDir, section)
if (md) parts.push(md)
continue
}
if (section.type !== 'function' || !section.id) continue
const fn = fnsById.get(section.id)
if (!fn) continue
// Mirror the site's getTypeSpec fallback: TypeDoc consts and type aliases
// (e.g. server's error/type exports) live under `variables`, not `methods`.
const tsm = fn.$ref ? (methods[fn.$ref] ?? variables[fn.$ref]) : undefined
// The site joins shortText + text for the full description; some entries
// (e.g. deprecated type aliases) carry content only in `text`.
const commentBody = [tsm?.comment?.shortText, tsm?.comment?.text]
.filter((s): s is string => !!s?.trim())
.join('\n\n')
parts.push(
renderFunctionSection({
title: fn.title || section.title || fn.id,
description: fn.description ?? (commentBody || undefined),
notes: fn.notes,
examples: fn.examples ?? tsm?.comment?.examples,
})
)
}
return parts.join('\n\n') + '\n'
}
async function renderApi(ref: ApiRef): Promise<string> {
type Endpoint = {
summary?: string
description?: string
path?: string
method?: string
parameters?: Array<{
name: string
required?: boolean
description?: string
schema?: { type?: string }
}>
requestBody?: RequestBody
}
const [sections, entries] = await Promise.all([
readJson<Section[]>(ref.sectionsPath),
readJson<Array<[string, Endpoint]>>(ref.endpointsByIdPath),
])
const endpointsById = new Map(entries)
const parts: string[] = [`# ${ref.title}`]
for (const section of flatten(sections)) {
if (section.type === 'category' && section.title) {
parts.push(`## ${section.title}`)
continue
}
if (section.type === 'markdown') {
const md = await renderMarkdownSection(ref.mdxDir, section)
if (md) parts.push(md)
continue
}
if (section.type !== 'operation' || !section.id) continue
const ep = endpointsById.get(section.id)
if (!ep) continue
const heading = ep.summary || section.title || section.id
const block = [`## ${heading}`]
if (ep.path) block.push(`\`${(ep.method ?? 'GET').toUpperCase()} ${ep.path}\``)
if (ep.description?.trim()) block.push(ep.description.trim())
if (ep.parameters?.length) {
const list = ep.parameters
.map((p) => {
const required = p.required ? 'required' : 'optional'
const type = p.schema?.type ?? 'string'
return `- \`${p.name}\` (${type}, ${required})${p.description ? `: ${p.description}` : ''}`
})
.join('\n')
block.push(`### Parameters\n\n${list}`)
}
const requestBody = renderRequestBodyMarkdown(ep.requestBody)
if (requestBody) block.push(requestBody)
parts.push(block.join('\n\n'))
}
return parts.join('\n\n') + '\n'
}
async function renderCli(ref: CliRef): Promise<string> {
type Command = {
id: string
title?: string
summary?: string
description?: string
usage?: string
}
const [sections, spec] = await Promise.all([
readJson<Section[]>(ref.sectionsPath),
fs.readFile(ref.cliSpecPath, 'utf8').then((raw) => yaml.load(raw) as { commands: Command[] }),
])
const cmdsById = new Map((spec.commands ?? []).map((c) => [c.id, c]))
const parts: string[] = [`# ${ref.title}`]
for (const section of flatten(sections)) {
if (section.type === 'category' && section.title) {
parts.push(`## ${section.title}`)
continue
}
if (section.type === 'markdown') {
const md = await renderMarkdownSection(ref.mdxDir, section)
if (md) parts.push(md)
continue
}
if (section.type !== 'cli-command' || !section.id) continue
const cmd = cmdsById.get(section.id)
if (!cmd) continue
const block = [`## ${cmd.summary || cmd.title || section.title || section.id}`]
if (cmd.description?.trim()) block.push(cmd.description.trim())
if (cmd.usage?.trim()) block.push('```sh\n' + cmd.usage.trim() + '\n```')
parts.push(block.join('\n\n'))
}
return parts.join('\n\n') + '\n'
}
async function generate() {
await fs.mkdir(OUT_DIR, { recursive: true })
const sharedTypeSpec = await readJson<TypeSpec>(
path.join(process.cwd(), 'content/reference/javascript/v2/typeSpec.json')
)
const references = REFERENCES.filter((ref) => !ref.feature || isFeatureEnabled(ref.feature))
await Promise.all(
references.map(async (ref) => {
let output: string
switch (ref.kind) {
case 'sdk-legacy':
output = await renderSdkLegacy(ref, sharedTypeSpec)
break
case 'sdk-new':
output = await renderSdkNew(ref)
break
case 'api':
output = await renderApi(ref)
break
case 'cli':
output = await renderCli(ref)
break
}
const tree = fromMarkdown(output, {
extensions: [gfm()],
mdastExtensions: [gfmFromMarkdown()],
})
addBaseUrlPrefix(tree)
const prefixed = toMarkdown(tree, {
extensions: [gfmToMarkdown()],
bullet: '-',
listItemIndent: 'one',
})
await fs.writeFile(path.join(OUT_DIR, ref.outFile), prefixed)
})
)
console.log(`Generated ${references.length} markdown files under public/markdown/reference/`)
}
generate()