mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
## 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, bug fix. Part 3 of 6 in a stack that splits the library redesign into reviewable pieces. ## What is the current behavior? An agent can already fetch any guide as Markdown, but has no way to find out what guides exist: the entry point is a rendered React page. The exporter also fails quietly in ways that ship wrong output rather than failing the build: - An unknown component silently unwraps to its children, so a component rename drops its rendered content. - A registry item that cannot be read produces a page with no file listing. - A link to a missing page produces a 404 URL. - An unrecognized install framework produces a plausible command for the wrong CLI. - Only absolute `/library/docs` links are rewritten, so in-page anchors and sibling links break in the export. ## What is the new behavior? `/library` negotiates Markdown the same way the guides do — `Accept: text/markdown`, or an explicit `/library/index.md` — and returns a categorized catalog with every block, its description, its framework variants, and a link to each guide's Markdown. `config/library.ts` is the single catalog description the generator reads, and a test ties it to the content directory in both directions: a guide cannot be added without a catalog entry, or listed without a guide. Each quiet failure above now throws, and links resolve against the page they appear on and are checked against the set of published documents. ```bash curl -H 'Accept: text/markdown' https://supabase.com/library ``` ## Additional context `config/library.ts` also carries the category and preview metadata the redesigned homepage consumes in the last PR of the stack; here it is exercised by the Markdown index and its test. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added a browsable library catalog covering categories, blocks, starter apps, and supported frameworks. - Added Markdown versions of the library homepage and documentation for compatible tools and workflows. - Added framework-aware links and expanded registry information, including dependencies and source details. - Markdown requests now work for the homepage and documentation, while browser requests continue receiving HTML. - **Bug Fixes** - Improved document link handling, metadata validation, slug consistency, and detection of duplicate or missing documentation entries. - **Tests** - Added coverage for catalog routes, Markdown generation, homepage negotiation, document parsing, and framework-specific links. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
108 lines
3.6 KiB
TypeScript
108 lines
3.6 KiB
TypeScript
import type { Content, Parent, Root } from 'mdast'
|
|
import { fromMarkdown } from 'mdast-util-from-markdown'
|
|
import { gfmFromMarkdown, gfmToMarkdown } from 'mdast-util-gfm'
|
|
import { mdxFromMarkdown, mdxToMarkdown } from 'mdast-util-mdx'
|
|
import type { MdxJsxFlowElement, MdxJsxTextElement } from 'mdast-util-mdx-jsx'
|
|
import { toMarkdown } from 'mdast-util-to-markdown'
|
|
import { gfm } from 'micromark-extension-gfm'
|
|
import { mdxjs } from 'micromark-extension-mdxjs'
|
|
import { visit } from 'unist-util-visit'
|
|
|
|
import { parseLibraryDocument, toAgentHref } from './library-documents'
|
|
import { markdownSchema, type MarkdownOptions } from './markdown-schema'
|
|
|
|
type JsxNode = MdxJsxFlowElement | MdxJsxTextElement
|
|
type ComponentHandler = (ctx: {
|
|
props: Record<string, unknown>
|
|
children: string
|
|
node: JsxNode
|
|
options: MarkdownOptions
|
|
}) => string
|
|
|
|
const PARSE_OPTIONS = {
|
|
extensions: [mdxjs(), gfm()],
|
|
mdastExtensions: [mdxFromMarkdown(), gfmFromMarkdown()],
|
|
}
|
|
const SERIALIZE_OPTIONS = {
|
|
extensions: [mdxToMarkdown(), gfmToMarkdown()],
|
|
bullet: '-' as const,
|
|
listItemIndent: 'one' as const,
|
|
}
|
|
|
|
const parseMdx = (source: string): Root => fromMarkdown(source, PARSE_OPTIONS)
|
|
const serializeMdx = (tree: Parent): string => toMarkdown(tree as Root, SERIALIZE_OPTIONS)
|
|
|
|
const isJsx = (n: Content): n is JsxNode =>
|
|
n.type === 'mdxJsxFlowElement' || n.type === 'mdxJsxTextElement'
|
|
|
|
function propsFrom(node: JsxNode): Record<string, unknown> {
|
|
const props: Record<string, unknown> = {}
|
|
for (const attr of node.attributes) {
|
|
if (attr.type !== 'mdxJsxAttribute') continue
|
|
if (attr.value == null) props[attr.name] = true
|
|
else if (typeof attr.value === 'string') props[attr.name] = attr.value
|
|
else props[attr.name] = attr.value.value
|
|
}
|
|
return props
|
|
}
|
|
|
|
function applySchema(
|
|
parent: Parent,
|
|
schema: Record<string, ComponentHandler>,
|
|
options: MarkdownOptions
|
|
): void {
|
|
for (const child of parent.children as Content[]) {
|
|
if ('children' in child) applySchema(child as Parent, schema, options)
|
|
}
|
|
const next: Content[] = []
|
|
for (const child of parent.children as Content[]) {
|
|
if (
|
|
child.type === 'mdxFlowExpression' ||
|
|
child.type === 'mdxTextExpression' ||
|
|
child.type === 'mdxjsEsm'
|
|
) {
|
|
continue
|
|
}
|
|
if (isJsx(child)) {
|
|
const handler = schema[child.name ?? '']
|
|
if (!handler && child.name && /^[A-Z]/.test(child.name)) {
|
|
throw new Error(`No Markdown handler for component: ${child.name}`)
|
|
}
|
|
const children = serializeMdx({
|
|
type: 'root',
|
|
children: child.children as Root['children'],
|
|
}).trim()
|
|
const value = handler
|
|
? handler({ props: propsFrom(child), children, node: child, options })
|
|
: children
|
|
if (value) {
|
|
next.push({ type: 'html', value } as Content)
|
|
}
|
|
continue
|
|
}
|
|
next.push(child)
|
|
}
|
|
parent.children = next as Parent['children']
|
|
}
|
|
|
|
function rewriteLibraryLinks(tree: Root, options: MarkdownOptions): void {
|
|
visit(tree, 'link', (node) => {
|
|
node.url = toAgentHref(node.url, options.documentSlugs, options.documentBasePath)
|
|
})
|
|
}
|
|
|
|
export function transformLibraryMdx(raw: string, options: MarkdownOptions = {}): string {
|
|
const { content, data } = parseLibraryDocument(raw)
|
|
const tree = parseMdx([data.preview, content].filter(Boolean).join('\n\n'))
|
|
rewriteLibraryLinks(tree, options)
|
|
applySchema(tree, markdownSchema, options)
|
|
const body = serializeMdx(tree).trim()
|
|
|
|
const headerParts: string[] = []
|
|
if (data.title) headerParts.push(`# ${String(data.title)}`)
|
|
if (data.description) headerParts.push(String(data.description))
|
|
|
|
const header = headerParts.join('\n\n')
|
|
return `${header ? `${header}\n\n${body}` : body}\n`
|
|
}
|