Files
supabase/apps/docs/features/docs/MdxBase.tsx
T
Chris ChinchillaandCharis Lam 6821c07811 docs: feature toggle tests (#38282)
* Docs feature toggle tests

* Prettier

* Types fix

* Hide unsupported languages

* Fix import

* Add unsupported code warning

* Further SDK toggling

* Temp intro text

* Auth toggles

* clean up conditional visibility code

* fix: typecheck, tests, tiny bug

---------

Co-authored-by: Charis Lam <26616127+charislam@users.noreply.github.com>
2025-09-04 16:46:08 +02:00

65 lines
1.8 KiB
TypeScript

import { MDXRemote } from 'next-mdx-remote/rsc'
import { type ComponentProps } from 'react'
import rehypeKatex from 'rehype-katex'
import remarkGfm from 'remark-gfm'
import remarkMath from 'remark-math'
import { isFeatureEnabled } from 'common'
import { preprocessMdxWithDefaults } from '~/features/directives/utils'
import { components } from '~/features/docs/MdxBase.shared'
import { SerializeOptions } from '~/types/next-mdx-remote-serialize'
const mdxOptions: SerializeOptions = {
mdxOptions: {
useDynamicImport: true,
remarkPlugins: [[remarkMath, { singleDollarTextMath: false }], remarkGfm],
rehypePlugins: [rehypeKatex as any],
},
}
const MDXRemoteBase = async ({
source,
options = {},
customPreprocess,
...props
}: ComponentProps<typeof MDXRemote> & {
source: string
customPreprocess?: (mdx: string) => string | Promise<string>
}) => {
const preprocess = customPreprocess ?? preprocessMdxWithDefaults
const preprocessedSource = await preprocess(source)
const { mdxOptions: { remarkPlugins, rehypePlugins, ...otherMdxOptions } = {}, ...otherOptions } =
options
const {
mdxOptions: {
remarkPlugins: originalRemarkPlugins,
rehypePlugins: originalRehypePlugins,
...originalMdxOptions
} = {},
} = mdxOptions
const finalOptions = {
scope: { isFeatureEnabled },
...mdxOptions,
...otherOptions,
mdxOptions: {
...originalMdxOptions,
...otherMdxOptions,
remarkPlugins: [...(originalRemarkPlugins ?? []), ...(remarkPlugins ?? [])],
rehypePlugins: [...(originalRehypePlugins ?? []), ...(rehypePlugins ?? [])],
},
} as SerializeOptions
return (
<MDXRemote
source={preprocessedSource}
components={components}
options={finalOptions}
{...props}
/>
)
}
export { MDXRemoteBase }