import type { MDXComponents } from "mdx/types"
import * as React from "react"
import { CodeBlock } from "@workspace/ui/components/code-block"
import { slugifyHeading } from "@/lib/headings"
function HeadingAnchor({ id }: { id: string }) {
return (
#
)
}
function extractFigcaptionText(node: React.ReactNode): string | undefined {
let title: string | undefined
React.Children.forEach(node, (child) => {
if (!React.isValidElement(child)) return
const props = child.props as Record & {
children?: React.ReactNode
}
if (props["data-rehype-pretty-code-title"] !== undefined) {
if (typeof props.children === "string") title = props.children
}
})
return title
}
function extractPre(node: React.ReactNode): React.ReactNode {
let pre: React.ReactNode = null
React.Children.forEach(node, (child) => {
if (!React.isValidElement(child)) return
const props = child.props as Record
if (props["data-rehype-pretty-code-title"] === undefined) {
pre = child
}
})
return pre
}
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
h1: ({ children }) => (
{children}
),
h2: ({ children }) => {
const id = slugifyHeading(children)
return (
{children}
)
},
h3: ({ children }) => {
const id = slugifyHeading(children)
return (
{children}
)
},
p: ({ children }) => (
{children}
),
a: ({ children, href }) => (
{children}
),
ul: ({ children }) => (
),
ol: ({ children }) => (
{children}
),
code: ({ children, ...props }) => (
{children}
),
table: ({ children }) => (
),
thead: ({ children }) => (
{children}
),
tbody: ({ children }) => (
{children}
),
tr: ({ children }) => {children}
,
th: ({ children }) => (
{children}
|
),
td: ({ children }) => (
{children}
|
),
figure: (props) => {
const dataAttr = (props as Record)[
"data-rehype-pretty-code-figure"
]
if (dataAttr === undefined) {
return
}
const filename = extractFigcaptionText(props.children)
const pre = extractPre(props.children)
return {pre}
},
...components,
}
}