Files
supabase/apps/ui-library/scripts/markdown-schema.ts
Saxon Fletcher bf60e6cdce feat(library): serve agent-readable markdown for each docs page (#49567)
## I have read the
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
file.

YES

## What kind of change does this PR introduce?

Feature: agent-readable markdown pages for the UI library docs.

## What is the current behavior?

Library docs are HTML-only. `llms.txt` lists page titles, but there is
no `.md` body an agent can fetch.

## What is the new behavior?

Each docs page is also served as markdown:

- Build-time MDX → markdown (`pnpm --filter library build:markdown`)
- `GET /library/docs/{slug}.md` (and `Accept: text/markdown`)
- HTML pages advertise `rel=alternate` `text/markdown`
- `llms.txt` links to the `.md` URLs

This is the base of a stack. The prompt-tab PR sits on top:
https://github.com/supabase/supabase/pull/49566

## Additional context

Interactive previews are omitted from the markdown. `BlockItem` emits
the production `npx shadcn add` command so agents still get an install
path.

## To test

1. `pnpm --filter library dev` (generates markdown in `predev`).
2. Open http://localhost:3004/library/docs/nextjs/password-based-auth.md
— markdown with the install command, file tree, and setup steps; no
interactive previews.
3. Open the same path without `.md` — HTML docs unchanged (no prompt tab
in this PR).
4. `curl -H 'Accept: text/markdown'
http://localhost:3004/library/docs/nextjs/password-based-auth` should
also return markdown.
5. http://localhost:3004/library/llms.txt — links should end in `.md`.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Documentation pages are available as Markdown through `.md` URLs and a
dedicated endpoint.
* Markdown is generated automatically during development and production
builds.
* Generated content preserves front matter, links, callouts,
installation instructions, and supported documentation elements.
* Installation commands support npm, pnpm, yarn, and bun for React and
Vue projects.

* **Bug Fixes**
* Improved Markdown file handling, link rewriting, and content
negotiation.

* **Tests**
* Added coverage for Markdown conversion, content negotiation, and
installation commands.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Saxon Fletcher <SaxonF@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-01 11:35:55 +10:00

130 lines
3.7 KiB
TypeScript

import path from 'node:path'
import { getInstallCommands } from '../lib/install-command'
import { generateRegistryTree, type RegistryNode } from '../lib/process-registry'
type HandlerContext = {
props: Record<string, unknown>
children: string
}
type ComponentHandler = (ctx: HandlerContext) => string
const omit: ComponentHandler = () => ''
const unwrap: ComponentHandler = ({ children }) => children
function toAgentHref(href: string): string {
if (!href) return href
if (href.startsWith('/library/docs/')) {
const [pathname, hash] = href.split('#')
const withMd = pathname.endsWith('.md') ? pathname : `${pathname}.md`
return `https://supabase.com${withMd}${hash ? `#${hash}` : ''}`
}
if (href.startsWith('/') && !href.startsWith('//')) {
return `https://supabase.com${href}`
}
return href
}
function BlockItem({ props }: HandlerContext): string {
const name = String(props.name ?? '')
if (!name) return ''
const command = getInstallCommands(name, { production: true }).npm
return ['Install this block:', '', '```bash', command, '```'].join('\n')
}
function RegistryBlock({ props }: HandlerContext): string {
const itemName = String(props.itemName ?? '')
if (!itemName) return ''
const registryPath = path.join(process.cwd(), 'public', 'r', `${itemName}.json`)
let listing = ''
try {
const tree = generateRegistryTree(registryPath)
listing = formatTree(tree)
} catch {
listing = ''
}
const registryUrl = `https://supabase.com/library/r/${itemName}.json`
const parts = [listing, listing ? '' : null, `Full source: ${registryUrl}`].filter(
(part) => part !== null
)
return parts.join('\n').trim()
}
function formatTree(nodes: RegistryNode[], indent = 0): string {
return nodes
.map((node) => {
const prefix = `${' '.repeat(indent)}- \`${node.name}${node.type === 'directory' ? '/' : ''}\``
const children = node.children?.length ? `\n${formatTree(node.children, indent + 1)}` : ''
return `${prefix}${children}`
})
.join('\n')
}
function Callout({ props, children }: HandlerContext): string {
const type = String(props.type ?? 'note')
const label = type.charAt(0).toUpperCase() + type.slice(1)
return `${label}: ${children}`.trim()
}
function AccordionTrigger({ children }: HandlerContext): string {
const title = children.trim()
return title ? `**${title}**` : ''
}
function LinkedCard({ props, children }: HandlerContext): string {
const href = toAgentHref(String(props.href ?? ''))
const label = children.replace(/\s+/g, ' ').trim()
return href ? `- [${label || href}](${href})` : label
}
function ComponentPreview({ props }: HandlerContext): string {
const description = String(props.description ?? '').trim()
return description
}
function TanStackBeta(): string {
return 'Note: TanStack Start support is in beta. APIs may change.'
}
function TanstackDBGenerator(): string {
return [
'This block is generated from your project schema.',
'Open the HTML page to log in and generate an install command:',
'https://supabase.com/library/docs/nextjs/tanstack-db',
].join('\n')
}
function Anchor({ props, children }: HandlerContext): string {
const href = toAgentHref(String(props.href ?? ''))
return href ? `[${children}](${href})` : children
}
export const markdownSchema: Record<string, ComponentHandler> = {
BlockItem,
RegistryBlock,
Callout,
Accordion: unwrap,
AccordionItem: unwrap,
AccordionTrigger,
AccordionContent: unwrap,
Card: unwrap,
LinkedCard,
ComponentPreview,
BlockPreview: omit,
DualRealtimeChat: omit,
DualRealtimeFlow: omit,
DualRealtimeMonaco: omit,
RealtimeMonaco: omit,
TanStackBeta,
TanstackDBGenerator,
CopyButton: omit,
svg: omit,
path: omit,
title: omit,
a: Anchor,
}