Skip to content

Commit b30d86e

Browse files
committed
fix: split anchor tags from doc headlines
1 parent 11b24ac commit b30d86e

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

src/components/RichText/Heading.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ export const Heading: React.FC<any> = ({ node, nodesToJSX }) => {
1212

1313
const childrenText = node.children?.[0]?.text as string
1414

15-
const anchor = slugify(formatAnchor(childrenText))
15+
const { label, tag } = formatAnchor(childrenText)
1616
const { addHeading } = useRichText()
1717

18+
const anchor = slugify(tag ?? label)
19+
1820
useEffect(() => {
1921
addHeading(anchor, childrenText, 'secondary')
2022
}, [addHeading, anchor, childrenText])
2123

2224
return (
2325
<JumplistNode id={anchor} type={node.tag.toLowerCase()}>
24-
{children}
26+
{children.length > 1 ? children : label}
2527
</JumplistNode>
2628
)
2729
}

src/components/RichText/formatAnchor.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,21 @@ const flattenChildren: (value: { props: { children: any } } | string) => string
2424
return value.props.children
2525
}
2626

27-
export const formatAnchor: (children: string | string[]) => string = (children) => {
27+
export const formatAnchor: (children: string | string[]) => {
28+
label: string
29+
tag?: string | undefined
30+
} = (children) => {
2831
if (Array.isArray(children)) {
29-
return children.map(flattenChildren).join('')
32+
return {
33+
label: children.map(flattenChildren).join(''),
34+
}
3035
}
3136

3237
if (typeof children === 'string' && children.includes('#')) {
33-
return children.split('#')[1]
38+
return {
39+
label: children.split('#')[0],
40+
tag: children.split('#')[1],
41+
}
3442
}
35-
return flattenChildren(children)
43+
return { label: flattenChildren(children) }
3644
}

0 commit comments

Comments
 (0)