Files
supabase/apps/docs/features/docs/Reference.mdx.tsx
T
Charis ee87cc20e8 ci(docs): fix build timeouts (#35336)
Fix build timeouts by skipping type generation for reference pages
2025-04-28 16:54:07 -04:00

59 lines
2.0 KiB
TypeScript

import matter from 'gray-matter'
import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
import { type ComponentProps } from 'react'
import CliGlobalFlagsHandler from '~/components/reference/enrichments/cli/CliGlobalFlagsHandler'
import { MDXRemoteBase } from '~/features/docs/MdxBase'
import { components } from '~/features/docs/MdxBase.shared'
import { RefSubLayout } from '~/features/docs/Reference.ui'
import { cache_fullProcess_withDevCacheBust } from '~/features/helpers.fs'
import { REF_DOCS_DIRECTORY } from '~/lib/docs'
import { CodeBlock } from '../ui/CodeBlock/CodeBlock'
async function getRefMarkdownInternal(relPath: string) {
const fullPath = join(REF_DOCS_DIRECTORY, relPath + '.mdx')
try {
if (!fullPath.startsWith(REF_DOCS_DIRECTORY)) {
throw Error(`Accessing forbidden path outside of REF_DOCS_DIRECTORY: ${fullPath}`)
}
const mdx = await readFile(fullPath, 'utf-8')
const { content } = matter(mdx)
return content
} catch (err) {
console.error(`Error fetching reference markdown from file: ${fullPath}:\n\n${err}`)
return ''
}
}
/**
* Caching this for the entire process is fine because the Markdown content is
* baked into each deployment and cannot change. There's also nothing sensitive
* here: this is just reading the public MDX files from the codebase.
*/
const getRefMarkdown = cache_fullProcess_withDevCacheBust(
getRefMarkdownInternal,
REF_DOCS_DIRECTORY,
(filename: string) => JSON.stringify([filename.replace(/\.mdx$/, '')])
)
interface MDXRemoteRefsProps {
source: string
}
function MDXRemoteRefs({ source }: MDXRemoteRefsProps) {
const refComponents = {
...components,
// Override the CodeBlock used for normal guides to skip type generation
// because it is too resource-intensive
pre: (props: ComponentProps<typeof CodeBlock>) => <CodeBlock {...props} skipTypeGeneration />,
RefSubLayout,
CliGlobalFlagsHandler,
}
return <MDXRemoteBase source={source} components={refComponents} />
}
export { getRefMarkdown, MDXRemoteRefs }