Files
supabase/apps/ui-library/scripts/build-llms-txt.ts
Ivan Vasilov 0990155ae7 feat: more fixes for UI library (#34355)
* Regenerate the registry files.

* Regenerate the llms.txt file.

* Set the supabase project for testing.

* Fix the registryBlock component.

* Various fixes.

* Add missing deps to satisfy TS build.

* Fix the tanstack client.

* Add a tanstack block for password-based auth.

* Fix the command URL.

* Fix the env vars in the React client.

* Add missing pages to the nextjs auth.

* Simplify the tanstack auth block.

* Add success state to the forgot-password form.

* Fix the v0 button. Add some more docs.

* Revert some changes.
2025-03-24 14:24:59 +01:00

107 lines
2.7 KiB
TypeScript

import fs from 'fs'
import path from 'path'
const BASE_URL = 'https://supabase.com/ui/docs'
interface DocMeta {
title: string
description?: string
path: string
}
console.log('🤖 Building llms.txt')
// Function to extract frontmatter from MDX files
function extractFrontmatter(content: string): { title?: string; description?: string } {
const frontmatterRegex = /---\n([\s\S]*?)\n---/
const match = content.match(frontmatterRegex)
if (!match) return {}
const frontmatter = match[1]
const titleMatch = frontmatter.match(/title:\s*(.*)/)
const descriptionMatch = frontmatter.match(/description:\s*(.*)/)
return {
title: titleMatch?.[1],
description: descriptionMatch?.[1],
}
}
// Function to recursively get all MDX files
function getMdxFiles(dir: string): string[] {
const files: string[] = []
const entries = fs.readdirSync(dir, { withFileTypes: true })
for (const entry of entries) {
const fullPath = path.join(dir, entry.name)
if (entry.isDirectory()) {
files.push(...getMdxFiles(fullPath))
} else if (entry.name.endsWith('.mdx')) {
files.push(fullPath)
}
}
return files
}
// Function to get all MDX files and their metadata
function getDocFiles(): DocMeta[] {
const docsDir = path.join(process.cwd(), 'content/docs')
const mdxFiles = getMdxFiles(docsDir).sort((a, b) => a.localeCompare(b))
const docs: DocMeta[] = []
for (const fullPath of mdxFiles) {
console.log(fullPath)
const content = fs.readFileSync(fullPath, 'utf-8')
const { title, description } = extractFrontmatter(content)
if (title) {
// Get relative path and convert to URL path
const relativePath = path.relative(docsDir, fullPath)
const urlPath = relativePath
.replace(/\.mdx$/, '')
.replace(/\/index$/, '')
.replace(/\\/g, '/')
docs.push({
title,
description,
path: urlPath,
})
}
}
return docs
}
// Generate the llms.txt content
const docs = getDocFiles()
let content = `# Supabase UI Library
Last updated: ${new Date().toISOString()}
## Overview
Library of components for your project. The components integrate with Supabase and are shadcn compatible.
## Docs
`
// Add documentation links
for (const doc of docs) {
const url = `${BASE_URL}/${doc.path}`
content += `- [${doc.title}](${url})`
if (doc.description) {
content += `\n - ${doc.description}`
}
content += '\n'
}
// Write the file
const publicDir = path.join(process.cwd(), 'public')
if (!fs.existsSync(publicDir)) {
fs.mkdirSync(publicDir, { recursive: true })
}
fs.writeFileSync(path.join(publicDir, 'llms.txt'), content)
console.log('✅ Generated llms.txt in public directory')