The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat(www): serve blog, customers, events as .md for AI agents (#45403)
Pamela Chiabaabcb189c5f
diff --git a/apps/www/app/blog/[slug]/page.tsx b/apps/www/app/blog/[slug]/page.tsxindex e2f68cd513b..6e6fc8fb88c 100644--- a/apps/www/app/blog/[slug]/page.tsx+++ b/apps/www/app/blog/[slug]/page.tsx@@ -2,8 +2,8 @@ import type { Metadata } from 'next'import { draftMode } from 'next/headers'import BlogPostClient from './BlogPostClient'-import { SITE_ORIGIN } from '@/lib/constants'import { getAbsoluteBlogSocialImage } from '@/lib/blog-images'+import { SITE_ORIGIN } from '@/lib/constants'import { getAllPostSlugs, getPostdata, getSortedPosts } from '@/lib/posts'import type { Blog, BlogData, PostReturnType } from '@/types/post'@@ -53,6 +53,11 @@ export async function generateMetadata({ params }: { params: Promise<Params> }):return {title: blogPost.title,description: blogPost.description,+ alternates: {+ types: {+ 'text/markdown': `/blog/${slug}.md`,+ },+ },openGraph: {title: blogPost.title,description: blogPost.description,diff --git a/apps/www/package.json b/apps/www/package.jsonindex 219f472836a..30503d95897 100644--- a/apps/www/package.json+++ b/apps/www/package.json@@ -111,8 +111,10 @@"api-types": "workspace:*","postcss": "catalog:","react-hook-form": "^7.71.2",+ "remark-mdx": "^3.0.1","tailwindcss": "catalog:","tsconfig": "workspace:*",+ "unist-util-visit": "^5.1.0","unist-util-visit-parents": "5.1.3","uuid": "^9.0.1","vite-tsconfig-paths": "catalog:",diff --git a/apps/www/scripts/generateMdContent.mjs b/apps/www/scripts/generateMdContent.mjsindex 053408a6d26..378b18cefbf 100644--- a/apps/www/scripts/generateMdContent.mjs+++ b/apps/www/scripts/generateMdContent.mjs@@ -1,31 +1,91 @@// @ts-check/**- * Scans content/md/ and emits a TypeScript module exporting the markdown- * content as a Map plus the slug allowlist as a Set. Static imports of the- * generated file make the content traceable by @vercel/nft so it ends up in- * the serverless bundle without runtime fs.readFile.- *- * Drop a new .md file in content/md/ and the route handler picks it up on- * the next content:build — no constant to edit.+ * Scans content/md/ + _blog/ + _customers/ + _events/ and emits a TypeScript+ * module exporting MD_CONTENT (slug → markdown) and MD_PAGES (allowlist Set).+ * The static import keeps content traceable by @vercel/nft, no runtime fs reads.*/import { promises as fs } from 'fs'import path from 'path'import { fileURLToPath } from 'url'+import matter from 'gray-matter'++import { mdxBodyToMarkdown } from './lib/mdxToMarkdown.mjs'const __dirname = path.dirname(fileURLToPath(import.meta.url))-const contentDir = path.join(__dirname, '../content/md')-const outputPath = path.join(__dirname, '../app/api-v2/md/content.generated.ts')+const wwwDir = path.join(__dirname, '..')+const contentDir = path.join(wwwDir, 'content/md')+const outputPath = path.join(wwwDir, 'app/api-v2/md/content.generated.ts')++// Matches lib/posts.tsx FILENAME_SUBSTRING — strips YYYY-MM-DD- (11 chars).+const DATE_PREFIX = 11-// 'pricing' is served dynamically via generatePricingContent() instead of-// from a .md file; it still needs to be in MD_PAGES so middleware rewrites-// /pricing.md to the API route.+// Slugs handled by a dynamic generator in the route handler. Listed here so+// MD_PAGES still includes them (middleware relies on the allowlist).const DYNAMIC_SLUGS = ['pricing']+function pickFields(data, fields) {+ const picked = {}+ for (const field of fields) {+ const value = data[field]+ if (value == null || value === '' || (Array.isArray(value) && value.length === 0)) continue+ picked[field] = value+ }+ return picked+}++const MDX_SECTIONS = [+ {+ dir: '_blog',+ urlPrefix: 'blog',+ stripDatePrefix: true,+ frontmatterFields: ['title', 'description', 'author', 'date', 'tags', 'categories'],+ },+ {+ dir: '_customers',+ urlPrefix: 'customers',+ stripDatePrefix: false,+ frontmatterFields: [+ 'name',+ 'title',+ 'description',+ 'company_url',+ 'industry',+ 'region',+ 'company_size',+ 'supabase_products',+ 'date',+ ],+ },+ {+ dir: '_events',+ urlPrefix: 'events',+ stripDatePrefix: true,+ frontmatterFields: [+ 'title',+ 'subtitle',+ 'description',+ 'type',+ 'date',+ 'end_date',+ 'timezone',+ 'duration',+ 'onDemand',+ ],+ skipIf: (data) => data.disable_page_build === true,+ },+]+async function collectMdFiles(dir, prefix = '') {const results = []- const dirents = await fs.readdir(dir, { withFileTypes: true })+ let dirents+ try {+ dirents = await fs.readdir(dir, { withFileTypes: true })+ } catch (err) {+ if (err.code === 'ENOENT') return results+ throw err+ }for (const dirent of dirents) {const slug = prefix ? `${prefix}/${dirent.name}` : dirent.nameif (dirent.isDirectory()) {@@ -37,56 +97,117 @@ async function collectMdFiles(dir, prefix = '') {return results}-// homepage first (it's the site overview), then everything else alphabetical.+// _events double-underscore filenames (`2024-08-30__launch-...`) produce slugs+// with a leading underscore. The HTML page is at `/events/_launch-...`, so the+// .md slug must keep the underscore to match.+function deriveSlug(filename, stripDatePrefix) {+ const base = filename.replace(/\.mdx$/, '')+ return stripDatePrefix ? base.substring(DATE_PREFIX) : base+}++async function ingestMdxSection(section) {+ const dir = path.join(wwwDir, section.dir)+ let filenames+ try {+ filenames = await fs.readdir(dir)+ } catch (err) {+ if (err.code === 'ENOENT') {+ console.warn(` ⚠ Section directory missing: ${section.dir}`)+ return []+ }+ throw err+ }++ const mdxFilenames = filenames.filter((f) => f.endsWith('.mdx'))+ const results = await Promise.all(+ mdxFilenames.map(async (filename) => {+ const fullPath = path.join(dir, filename)+ try {+ const raw = await fs.readFile(fullPath, 'utf-8')+ const parsed = matter(raw)+ const data = parsed.data ?? {}+ if (section.skipIf?.(data)) return null++ const slug = `${section.urlPrefix}/${deriveSlug(filename, section.stripDatePrefix)}`+ const body = await mdxBodyToMarkdown(parsed.content)+ const content = matter.stringify(body, pickFields(data, section.frontmatterFields))+ return { slug, content }+ } catch (err) {+ throw new Error(`${section.dir}/${filename}: ${err.message}`, { cause: err })+ }+ })+ )+ return results.filter((entry) => entry !== null)+}+function sortSlugs(a, b) {if (a === 'homepage') return -1if (b === 'homepage') return 1return a.localeCompare(b)}-const slugs = (await collectMdFiles(contentDir)).sort(sortSlugs)+const staticSlugs = (await collectMdFiles(contentDir)).sort(sortSlugs)-if (slugs.length === 0) {+if (staticSlugs.length === 0) {console.error('❌ No .md files found in content/md/')process.exit(1)}-// A static file with the same slug as a dynamic generator would land in both-// MD_CONTENT and the dynamic append path, so /llms-full.txt would emit it-// twice. Fail the build instead of shipping a duplicate.-const collisions = slugs.filter((s) => DYNAMIC_SLUGS.includes(s))-if (collisions.length > 0) {+const staticEntries = await Promise.all(+ staticSlugs.map(async (slug) => ({+ slug,+ content: await fs.readFile(path.join(contentDir, `${slug}.md`), 'utf-8'),+ }))+)++const mdxEntries = []+for (const section of MDX_SECTIONS) {+ console.log(`📚 Ingesting ${section.dir}...`)+ const sectionEntries = await ingestMdxSection(section)+ console.log(` ${sectionEntries.length} entries`)+ mdxEntries.push(...sectionEntries)+}++const allEntries = [...staticEntries, ...mdxEntries]++const dynamicCollisions = staticSlugs.filter((s) => DYNAMIC_SLUGS.includes(s))+if (dynamicCollisions.length > 0) {console.error(- `❌ Slug collision: [${collisions.join(', ')}] is reserved for a dynamic generator. ` +- `Remove the corresponding file from content/md/ or update DYNAMIC_SLUGS.`+ `❌ Slug collision: [${dynamicCollisions.join(', ')}] reserved for a dynamic generator.`)process.exit(1)}-const entries = []-const errors = []-for (const slug of slugs) {- const filePath = path.join(contentDir, `${slug}.md`)- try {- const content = await fs.readFile(filePath, 'utf-8')- entries.push(` [${JSON.stringify(slug)}, ${JSON.stringify(content)}]`)- } catch (err) {- errors.push(`${slug}.md: ${err.message}`)- }+const sectionRoots = MDX_SECTIONS.map((s) => s.urlPrefix)+const sectionRootCollisions = [...staticSlugs, ...DYNAMIC_SLUGS].filter((s) =>+ sectionRoots.includes(s)+)+if (sectionRootCollisions.length > 0) {+ console.error(+ `❌ Section-root collision: [${sectionRootCollisions.join(', ')}] shadows an MDX section.`+ )+ process.exit(1)}-if (errors.length > 0) {- console.error('❌ Failed to read .md files:')- errors.forEach((e) => console.error(` ${e}`))- process.exit(1)+const seen = new Set()+for (const entry of allEntries) {+ if (seen.has(entry.slug)) {+ console.error(`❌ Duplicate slug emitted: ${entry.slug}`)+ process.exit(1)+ }+ seen.add(entry.slug)}-const allSlugs = [...slugs, ...DYNAMIC_SLUGS]-const pageEntries = allSlugs.map((s) => ` ${JSON.stringify(s)}`).join(',\n')+const contentEntries = allEntries+ .map((e) => ` [${JSON.stringify(e.slug)}, ${JSON.stringify(e.content)}]`)+ .join(',\n')++const allPageSlugs = [...allEntries.map((e) => e.slug), ...DYNAMIC_SLUGS]+const pageEntries = allPageSlugs.map((s) => ` ${JSON.stringify(s)}`).join(',\n')const output = `// AUTO-GENERATED by scripts/generateMdContent.mjs — do not editexport const MD_CONTENT = new Map<string, string>([-${entries.join(',\n')},+${contentEntries},])export const MD_PAGES = new Set<string>([@@ -95,4 +216,4 @@ ${pageEntries},`await fs.writeFile(outputPath, output, 'utf-8')-console.log(`✅ Generated ${outputPath} (${entries.length} files, ${allSlugs.length} pages)`)+console.log(`✅ Generated ${outputPath} (${allEntries.length} files, ${allPageSlugs.length} pages)`)diff --git a/apps/www/scripts/lib/__tests__/mdxToMarkdown.test.mjs b/apps/www/scripts/lib/__tests__/mdxToMarkdown.test.mjsnew file mode 100644index 00000000000..76ff2c551e5--- /dev/null+++ b/apps/www/scripts/lib/__tests__/mdxToMarkdown.test.mjs@@ -0,0 +1,89 @@+// @ts-check+import { describe, expect, it } from 'vitest'++import { mdxBodyToMarkdown } from '../mdxToMarkdown.mjs'++describe('mdxBodyToMarkdown', () => {+ it('preserves plain markdown unchanged', async () => {+ const out = await mdxBodyToMarkdown('# Title\n\nA **bold** word.')+ expect(out).toContain('# Title')+ expect(out).toContain('**bold**')+ })++ it('preserves code fences containing JSX literally', async () => {+ const src = [+ '```tsx',+ 'function App() {',+ ' return <Quote caption="x">hi</Quote>',+ '}',+ '```',+ ].join('\n')+ const out = await mdxBodyToMarkdown(src)+ expect(out).toContain('<Quote caption="x">hi</Quote>')+ expect(out).toContain('```tsx')+ })++ it('rewrites <Img /> to a markdown image', async () => {+ const out = await mdxBodyToMarkdown('<Img src="/x.png" alt="X" />')+ expect(out.trim()).toBe('')+ })++ it('rewrites <Img /> without alt', async () => {+ const out = await mdxBodyToMarkdown('<Img src="/x.png" />')+ expect(out.trim()).toBe('')+ })++ it('rewrites <Admonition> to a labeled blockquote', async () => {+ const out = await mdxBodyToMarkdown('<Admonition type="note">\n\nHeads up.\n\n</Admonition>')+ expect(out).toMatch(/^>\s+\*\*NOTE\*\*/m)+ expect(out).toContain('Heads up.')+ })++ it('rewrites <Quote> with caption to a blockquote with em-dash attribution', async () => {+ const out = await mdxBodyToMarkdown(+ '<Quote caption="Jane Doe, CEO">\n\nIt was great.\n\n</Quote>'+ )+ expect(out).toContain('> It was great.')+ expect(out).toContain('*— Jane Doe, CEO*')+ })++ it('rewrites <Link href> to a markdown link', async () => {+ const out = await mdxBodyToMarkdown('<Link href="/x">click</Link>')+ expect(out.trim()).toBe('[click](/x)')+ })++ it('rewrites <Subtitle> and <Badge> to bold', async () => {+ const out = await mdxBodyToMarkdown('<Subtitle>foo</Subtitle> <Badge>bar</Badge>')+ expect(out).toContain('**foo**')+ expect(out).toContain('**bar**')+ })++ it('drops <Avatar> entirely', async () => {+ const out = await mdxBodyToMarkdown('<Avatar src="/x" /> rest')+ expect(out).not.toContain('Avatar')+ expect(out).toContain('rest')+ })++ it('flattens unknown inline JSX to text, preserving inner content', async () => {+ const out = await mdxBodyToMarkdown('Some text with <Mystery>kept</Mystery> inline.')+ expect(out).toContain('kept')+ expect(out).toContain('Some text with')+ expect(out).toContain('inline.')+ expect(out).not.toContain('<Mystery')+ expect(out).not.toContain('</Mystery')+ })++ it('strips MDX comments {/* ... */}', async () => {+ const out = await mdxBodyToMarkdown('hello {/* internal note */} world')+ expect(out).toContain('hello')+ expect(out).toContain('world')+ expect(out).not.toContain('internal')+ })++ it('strips orphan mdxjsEsm imports at the top level', async () => {+ const src = "import X from 'y'\n\n# Title"+ const out = await mdxBodyToMarkdown(src)+ expect(out).toContain('# Title')+ expect(out).not.toContain('import')+ })+})diff --git a/apps/www/scripts/lib/mdxToMarkdown.mjs b/apps/www/scripts/lib/mdxToMarkdown.mjsnew file mode 100644index 00000000000..c17e844be7c--- /dev/null+++ b/apps/www/scripts/lib/mdxToMarkdown.mjs@@ -0,0 +1,114 @@+// @ts-check++import { remark } from 'remark'+import remarkGfm from 'remark-gfm'+import remarkMdx from 'remark-mdx'+import { SKIP, visit } from 'unist-util-visit'++function getJsxAttr(node, name) {+ const attr = (node.attributes ?? []).find((a) => a.type === 'mdxJsxAttribute' && a.name === name)+ return typeof attr?.value === 'string' ? attr.value : ''+}++function flattenChildText(nodes) {+ const parts = []+ for (const node of nodes) {+ if (node.type === 'text' || node.type === 'inlineCode') parts.push(node.value)+ else if (node.children) parts.push(flattenChildText(node.children))+ }+ return parts.join('')+}++const text = (value) => ({ type: 'text', value })+const para = (children) => ({ type: 'paragraph', children })+const strong = (children) => ({ type: 'strong', children })+const emph = (children) => ({ type: 'emphasis', children })+const blockquote = (children) => ({ type: 'blockquote', children })++// Unknown JSX must not become a `paragraph` (block) inside an inline parent.+function unwrap(node) {+ const children = node.children ?? []+ if (node.type === 'mdxJsxTextElement') return text(flattenChildText(children))+ return children.length > 0 ? para(children) : text('')+}++const REWRITES = {+ Img: (node) => {+ const src = getJsxAttr(node, 'src')+ return src ? { type: 'image', url: src, alt: getJsxAttr(node, 'alt'), title: null } : text('')+ },+ img: (node) => REWRITES.Img(node),++ Admonition: (node) => {+ const label = (getJsxAttr(node, 'type') || 'note').toUpperCase()+ return blockquote([para([strong([text(label)])]), ...(node.children ?? [])])+ },++ Quote: (node) => {+ const caption = getJsxAttr(node, 'caption')+ const attribution = caption ? [para([emph([text(`— ${caption}`)])])] : []+ return blockquote([...(node.children ?? []), ...attribution])+ },++ BlogCollapsible: (node) => {+ const title = getJsxAttr(node, 'title')+ const heading = title ? [para([strong([text(title)])])] : []+ return blockquote([...heading, ...(node.children ?? [])])+ },++ Link: (node) => {+ const href = getJsxAttr(node, 'href')+ if (!href) return unwrap(node)+ return { type: 'link', url: href, title: null, children: node.children ?? [] }+ },+ a: (node) => REWRITES.Link(node),++ Subtitle: (node) => strong(node.children ?? []),+ Badge: (node) => strong(node.children ?? []),++ Avatar: (node) => (node.type === 'mdxJsxTextElement' ? text('') : para([])),+}++function rewriteJsxNode(node) {+ return REWRITES[node.name]?.(node) ?? unwrap(node)+}++function transformMdxNodes() {+ return (tree, file) => {+ const unknownNames = new Set()+ visit(tree, (node, index, parent) => {+ if (parent == null || index == null) return++ if (+ node.type === 'mdxFlowExpression' ||+ node.type === 'mdxTextExpression' ||+ node.type === 'mdxjsEsm'+ ) {+ parent.children.splice(index, 1)+ return [SKIP, index]+ }++ if (node.type !== 'mdxJsxFlowElement' && node.type !== 'mdxJsxTextElement') return++ if (node.name && !(node.name in REWRITES)) unknownNames.add(node.name)+ parent.children[index] = rewriteJsxNode(node)+ return [SKIP, index]+ })+ if (unknownNames.size > 0) {+ file.message(`Unknown JSX components stripped: ${[...unknownNames].sort().join(', ')}`)+ }+ }+}++const processor = remark().use(remarkMdx).use(remarkGfm).use(transformMdxNodes).data('settings', {+ bullet: '-',+ fences: true,+ incrementListMarker: false,+ rule: '-',+})++export async function mdxBodyToMarkdown(rawBody) {+ const file = await processor.process(rawBody)+ for (const msg of file.messages) console.warn(` ⚠ ${msg.message}`)+ return String(file)+}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 10af73a9147..184a2b27ed6 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1855,12 +1855,18 @@ importers:react-hook-form:specifier: ^7.71.2version: 7.72.1([email protected])+ remark-mdx:+ specifier: ^3.0.1+ version: 3.0.1([email protected])tailwindcss:specifier: 'catalog:'version: 4.2.4tsconfig:specifier: workspace:*version: link:../../packages/tsconfig+ unist-util-visit:+ specifier: ^5.1.0+ version: 5.1.0unist-util-visit-parents:specifier: 5.1.3version: 5.1.3@@ -34099,7 +34105,7 @@ snapshots:hast-util-heading-rank: 3.0.0hast-util-is-element: 3.0.0unified: 11.0.5- unist-util-visit: 5.0.0+ unist-util-visit: 5.1.0@@ -34142,7 +34148,7 @@ snapshots:github-slugger: 2.0.0hast-util-heading-rank: 3.0.0hast-util-to-string: 3.0.0- unist-util-visit: 5.0.0+ unist-util-visit: 5.1.0dependencies: