Files
supabase/apps/ui-library/lib/library-documents.test.ts
3e79df3ece feat(library): serve the block catalog as Markdown and harden the exporter (#50370)
## 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>
2026-09-22 14:14:04 +02:00

89 lines
3.3 KiB
TypeScript

import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import { buildLlmsTxt, getDocFiles } from '../scripts/build-markdown-index'
import { collectMdxFiles, getDocSlug, parseLibraryDocument } from './library-documents'
describe('library document exports', () => {
it('decodes YAML folded and quoted metadata for the LLM index', () => {
const directory = mkdtempSync(path.join(tmpdir(), 'library-documents-'))
try {
mkdirSync(path.join(directory, 'folded'))
writeFileSync(
path.join(directory, 'folded', 'index.mdx'),
`---
title: "A title: with punctuation"
description: >-
A folded description
across two lines
---
Body
`
)
writeFileSync(
path.join(directory, 'quoted.mdx'),
`---
title: 'Quoted title'
description: 'A quoted description'
---
`
)
const docs = getDocFiles(directory)
expect(docs).toEqual([
{
title: 'A title: with punctuation',
description: 'A folded description across two lines',
path: 'folded',
},
{ title: 'Quoted title', description: 'A quoted description', path: 'quoted' },
])
const output = buildLlmsTxt(docs, new Date('2026-09-11T00:00:00Z'))
expect(output).toMatch(/folded.md\)/)
expect(output).toMatch(/ - A folded description across two lines/)
expect(output).toMatch(/ - A quoted description/)
expect(output).not.toMatch(/>-|description:|'A quoted description'/)
} finally {
rmSync(directory, { recursive: true, force: true })
}
})
it('uses the same slug coverage for the LLM index and Markdown pages', () => {
const directory = fileURLToPath(new URL('../content/docs/', import.meta.url))
const sources = collectMdxFiles(directory)
const docs = getDocFiles(directory)
expect(docs.map((doc) => doc.path)).toEqual(
sources.map((source) => getDocSlug(path.relative(directory, source)))
)
expect(new Set(docs.map((doc) => doc.path)).size).toBe(docs.length)
expect(getDocSlug('framework\\index.mdx')).toBe('framework')
const aiChat = docs.find((doc) => doc.path === 'starters/ai-chat-app')!
expect(aiChat.description).toBe(
'A Next.js chat app with streaming responses, authentication, and saved conversations'
)
const output = buildLlmsTxt(docs)
expect(output.includes(' - Local-first, reactive collections backed by Supabase')).toBe(true)
expect(output).not.toMatch(/ - >-/)
})
it('rejects invalid metadata types rather than stringifying them into generated content', () => {
expect(() => parseLibraryDocument('---\ntitle: [one, two]\n---')).toThrow(
/title must be a string/
)
expect(() => parseLibraryDocument('---\ndescription: 42\n---')).toThrow(
/description must be a string/
)
expect(() => parseLibraryDocument('---\npreview: true\n---')).toThrow(
/preview must be a string/
)
const source = readFileSync(
new URL('../content/docs/starters/ai-chat-app.mdx', import.meta.url),
'utf8'
)
expect(parseLibraryDocument(source).content).toMatch(/npx create-next-app/)
})
})