Files
supabase__supabase/apps/ui-library/lib/markdown-schema.ts
Saxon Fletcher abbac3b852 refactor(library): resolve registry dependencies from one source of truth (#50367)
## 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?

Refactor, bug fix.

Part 1 of 6 in a stack that splits the library redesign into reviewable
pieces. This one is the foundation the rest build on and has no visual
change.

## What is the current behavior?

Three build steps each reimplement "where does this registry file land
in the user's project": `process-registry`'s `getDefaultPath`,
`registry/utils`' `uniqBy` on `file.path`, and the Markdown exporter.
They disagree, which produces real bugs:

- A Vue block whose files come from `node_modules/@supabase/vue-blocks/`
keeps its package path, so the installer writes the package folder into
the user's project.
- `registryItemAppend` builds its `docs` string from `(item.docs,
items.flatMap(...))` — a comma expression, so the item's own docs are
discarded.
- A name collision between a block file and its client's file silently
keeps one of the two.
- Install commands guess the CLI family from substrings in the item
name, so `infinite-query-composable` — a Vue block with neither "vue"
nor "nuxtjs" in its name — gets the React CLI.
- Production Vue installs use `@supabase/<name>`, but the `@supabase`
namespace is registered with shadcn, not shadcn-vue.
- `build:registry`, `build:content`, `build:markdown` and `build:llms`
run in parallel, but the last three read `public/r`.

## What is the new behavior?

`lib/registry-resolution.ts` owns installed-path derivation, first-party
dependency naming, deduplication, and cycle detection, and every
consumer calls it. `build-registry` validates the whole registry against
shadcn's schema and resolves every item, so a broken reference fails the
build instead of shipping. `clean-registry` throws rather than logging
past a failure.

Pages declare their install `framework` explicitly instead of it being
inferred, and production Vue installs use the absolute registry URL.

The build steps are serialized behind `build:prepare`, and a new
`library-tests.yml` workflow runs the library's tests, checks the
generated registry is committed, and builds the app.

## Additional context

Regenerated registry artifacts are the mechanical result of the
resolution fix — the Vue client items and the OAuth consent items that
gained their client's docs.

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

## Summary by CodeRabbit

* **New Features**
* Added explicit React and Vue framework selection for library blocks
and installation commands.
* Improved registry resolution, dependency handling, path validation,
and Vue file normalization.
* Added support for reliable local, preview, and production registry
URLs.

* **Documentation**
* Updated Vue and Nuxt installation documentation to identify the Vue
framework explicitly.

* **Bug Fixes**
* Preserved combined documentation and validated generated registry
content more consistently.

* **Tests**
* Added coverage for installation commands, registry resolution,
dependency handling, and generated artifacts.

* **Chores**
  * Added automated pull-request checks for library tests and builds.

<!-- 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>
2026-09-18 10:38:48 +10:00

134 lines
3.9 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 framework = props.framework ?? 'react'
if (framework !== 'react' && framework !== 'vue') {
throw new Error(`Unsupported install framework for ${name}: ${String(framework)}`)
}
const command = getInstallCommands(name, { framework, 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,
}