The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
/* eslint-disable turbo/no-undeclared-env-vars */
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises'
import path from 'path'
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
import rehypePrettyCode from 'rehype-pretty-code'
import rehypeSlug from 'rehype-slug'
import { codeImport } from 'remark-code-import'
import remarkGfm from 'remark-gfm'
import { visit } from 'unist-util-visit'
import { defineConfig, s } from 'velite'
import { rehypeComponent } from './lib/rehype-component'
const CODE_OUTPUT_DIR = '.velite/codes'
const codeTheme = JSON.parse(
await readFile(path.join(process.cwd(), 'lib/themes/supabase-2.json'), 'utf8')
)
function toCodeId(slugAsParams) {
if (!slugAsParams) return 'index'
return Buffer.from(slugAsParams, 'utf8').toString('base64url')
}
const LinksProperties = s.object({
doc: s.string().optional(),
api: s.string().optional(),
})
const NestedProperties = s.object({
radix: s.boolean().optional(),
shadcn: s.boolean().optional(),
vaul: s.boolean().optional(),
inputOtp: s.boolean().optional(),
reactAccessibleTreeview: s.boolean().optional(),
recharts: s.boolean().optional(),
})
const docs = s
.object({
title: s.string(),
description: s.string(),
published: s.boolean().default(true),
links: LinksProperties.optional(),
featured: s.boolean().default(false),
component: s.boolean().default(false),
fragment: s.boolean().default(false),
toc: s.boolean().default(true),
source: NestedProperties.optional(),
// mirrors contentlayer2's `_raw.flattenedPath`: file path relative to the
// content dir, extension stripped, trailing `/index` dropped.
path: s.path(),
raw: s.raw(),
// internal doc cross-links (e.g. `[Button](components/button)`) aren't
// real files on disk — disable Velite's default asset-copying behavior,
// which otherwise treats every relative link as a local file to copy.
// Minification is also disabled: it's pure CPU-bound Terser work with no
// real benefit for a dev-only content cache, and dominates build time.
code: s.mdx({ copyLinkedFiles: false, minify: false }),
})
.transform(async ({ path: flattenedPath, code, ...data }) => {
const slugAsParams = flattenedPath.split('/').slice(1).join('/')
const codeId = toCodeId(slugAsParams)
const codesDir = path.join(process.cwd(), CODE_OUTPUT_DIR)
await mkdir(codesDir, { recursive: true })
const codePath = path.join(codesDir, `${codeId}.json`)
const tmpPath = `${codePath}.tmp`
await writeFile(tmpPath, JSON.stringify(code), 'utf8')
await rename(tmpPath, codePath)
return {
...data,
slug: `/${flattenedPath}`,
slugAsParams,
codeId,
}
})
export default defineConfig({
root: './content',
output: {
clean: process.env.NODE_ENV === 'production',
},
collections: {
allDocs: {
name: 'Doc',
pattern: 'docs/**/*.mdx',
schema: docs,
},
},
mdx: {
remarkPlugins: [remarkGfm, codeImport],
rehypePlugins: [
rehypeSlug,
rehypeComponent,
() => (tree) => {
visit(tree, (node) => {
if (node?.type === 'element' && node?.tagName === 'pre') {
const [codeEl] = node.children
if (codeEl.tagName !== 'code') {
return
}
if (codeEl.data?.meta) {
// Extract event from meta and pass it down the tree.
const regex = /event="([^"]*)"/
const match = codeEl.data?.meta.match(regex)
if (match) {
node.__event__ = match ? match[1] : null
codeEl.data.meta = codeEl.data.meta.replace(regex, '')
}
}
node.__rawString__ = codeEl.children?.[0].value
node.__src__ = node.properties?.__src__
node.__style__ = node.properties?.__style__
}
})
},
[
rehypePrettyCode,
{
// plugin shares one cached highlighter per theme, lazy language loading needs shiki >= 4.5 (shikijs/shiki#1318)
theme: codeTheme,
keepBackground: false,
onVisitLine(node) {
// Prevent lines from collapsing in `display: grid` mode, and allow empty
// lines to be copy/pasted
if (node.children.length === 0) {
node.children = [{ type: 'text', value: ' ' }]
}
},
onVisitHighlightedLine(node) {
// the plugin strips the `line` class first, leaving className undefined
node.properties.className = [...(node.properties.className ?? []), 'line--highlighted']
},
onVisitHighlightedChars(node) {
node.properties.className = ['word--highlighted']
},
},
],
() => (tree) => {
visit(tree, (node) => {
if (node?.type === 'element' && node?.tagName === 'figure') {
if (!('data-rehype-pretty-code-figure' in node.properties)) {
return
}
const preElement = node.children.at(-1)
if (preElement.tagName !== 'pre') {
return
}
preElement.properties['__withMeta__'] = node.children.at(0).tagName === 'figcaption'
preElement.properties['__rawString__'] = node.__rawString__
if (node.__src__) {
preElement.properties['__src__'] = node.__src__
}
if (node.__event__) {
preElement.properties['__event__'] = node.__event__
}
if (node.__style__) {
preElement.properties['__style__'] = node.__style__
}
}
})
},
[
rehypeAutolinkHeadings,
{
properties: {
className: ['subheading-anchor'],
ariaLabel: 'Link to section',
},
},
],
],
},
})