mirror of
https://github.com/supabase/supabase.git
synced 2026-10-18 07:39:07 +03:00
Add the ability to embed code samples from GitHub into tutorials, so we can have a single source of truth for the source code. Introduces the <$CodeSample /> syntax, which is a special syntax and not a real React component (see the directives/README.md for why on earth I did it this way -- in this specific case, CodeHike adjusts the MDX syntax tree before it gets compiled, and we need to adjust it ourselves before CodeHike sees it, so we need to get down to the level of manipulating the AST in order to make this work with CodeHike). Adjusted one of the example tutorials to use this new feature as a test.
77 lines
2.1 KiB
TypeScript
77 lines
2.1 KiB
TypeScript
import codeHikeTheme from 'config/code-hike.theme.json' assert { type: 'json' }
|
|
import { remarkCodeHike, type CodeHikeConfig } from '@code-hike/mdx'
|
|
import { type SerializeOptions } from 'next-mdx-remote/dist/types'
|
|
import { MDXRemote } from 'next-mdx-remote/rsc'
|
|
import { type ComponentProps } from 'react'
|
|
import remarkGfm from 'remark-gfm'
|
|
import rehypeKatex from 'rehype-katex'
|
|
import remarkMath from 'remark-math'
|
|
|
|
import { preprocessMdxWithDefaults } from '~/features/directives/utils'
|
|
import { components } from '~/features/docs/MdxBase.shared'
|
|
|
|
const codeHikeOptions: CodeHikeConfig = {
|
|
theme: codeHikeTheme,
|
|
lineNumbers: true,
|
|
showCopyButton: true,
|
|
skipLanguages: [],
|
|
autoImport: false,
|
|
}
|
|
|
|
const mdxOptions: SerializeOptions = {
|
|
mdxOptions: {
|
|
useDynamicImport: true,
|
|
remarkPlugins: [
|
|
[remarkMath, { singleDollarTextMath: false }],
|
|
remarkGfm,
|
|
[remarkCodeHike, codeHikeOptions],
|
|
],
|
|
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 = {
|
|
...mdxOptions,
|
|
...otherOptions,
|
|
mdxOptions: {
|
|
...originalMdxOptions,
|
|
...otherMdxOptions,
|
|
remarkPlugins: [...(originalRemarkPlugins ?? []), ...(remarkPlugins ?? [])],
|
|
rehypePlugins: [...(originalRehypePlugins ?? []), ...(rehypePlugins ?? [])],
|
|
},
|
|
} as SerializeOptions
|
|
|
|
return (
|
|
<MDXRemote
|
|
source={preprocessedSource}
|
|
components={components}
|
|
options={finalOptions}
|
|
{...props}
|
|
/>
|
|
)
|
|
}
|
|
|
|
export { MDXRemoteBase }
|