diff --git a/src/mdx-components.tsx b/src/mdx-components.tsx
index ec1d668..26429c1 100644
--- a/src/mdx-components.tsx
+++ b/src/mdx-components.tsx
@@ -1,11 +1,24 @@
import type { MDXComponents } from 'mdx/types'
import slugify from 'slugify'
import { HashIcon } from 'lucide-react'
+import React from 'react'
import { Codeblock } from './components/Codeblock'
import Link from '@/components/Link'
+/**
+ * Extracts the text from a nested children object
+ */
const getHeadlineId = (children: React.ReactNode) => {
- return children ? `${slugify(children.toString(), { strict: true, lower: true })}` : ''
+ let text = ''
+ React.Children.forEach(children, (child) => {
+ if (typeof child === 'string') {
+ text += child
+ }
+ if (typeof child === 'object' && React.isValidElement(child)) {
+ text += getHeadlineId(child.props.children)
+ }
+ })
+ return children ? `${slugify(text, { strict: true, lower: true })}` : ''
}
const hashClass =
@@ -21,7 +34,7 @@ export function useMDXComponents(components: MDXComponents): MDXComponents {
),
code: (props) => ,
h1: ({ children, ...props }) => {
- const id = getHeadlineId(children?.toString())
+ const id = getHeadlineId(children)
return (