Skip to content

Commit 24ed8b3

Browse files
committed
fix: index root content + toc fixes
1 parent 89b234d commit 24ed8b3

File tree

7 files changed

+48
-27
lines changed

7 files changed

+48
-27
lines changed

src/app/[...slug]/layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export default async function Layout({ params, children }: Props) {
8585
</>
8686
}
8787
nav={<Nav docs={docs} asPath={asPath} />}
88-
aside={<Toc toc={doc.tableOfContents} />}
88+
aside={<Toc toc={doc.tableOfContents.filter(({ level }) => level > 0)} />}
8989
footer={
9090
<>
9191
{!!currentPage && (

src/components/Search/SearchItem.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function sanitizeAllHtmlButMark(str: string) {
2020
allowedTags: ['mark'],
2121
allowedAttributes: false,
2222
disallowedTagsMode: 'escape',
23+
parseStyleAttributes: false,
2324
})
2425
}
2526

src/components/Search/SearchModalContainer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const SearchModalContainer = ({ onClose }: SearchModalContainerProps) =>
1515
// const router = useRouter()
1616
// const boxes = useCSB()
1717
const { docs } = useDocs()
18-
console.log('docs', docs)
18+
// console.log('docs', docs)
1919
// const [lib] = router.query.slug as string[]
2020
const [query, setQuery] = React.useState('')
2121
const deferredQuery = React.useDeferredValue(query)

src/components/mdx/Toc/Toc.tsx

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,67 @@
22

33
import type { DocToC } from '@/app/[...slug]/DocsContext'
44
import cn from '@/lib/cn'
5-
import * as React from 'react'
5+
import { useEffect, useState } from 'react'
66

77
export function Toc({ toc }: { toc: DocToC[] }) {
88
// console.log('toc', toc)
9-
const [activeIndex, setActiveIndex] = React.useState(0)
109

11-
React.useEffect(() => {
12-
const headings = toc.map((heading) => document.getElementById(heading.id))
10+
const [activeIndex, setActiveIndex] = useState<number | undefined>()
1311

14-
const observer = new IntersectionObserver(([entry]) => {
15-
if (entry.intersectionRatio > 0) {
16-
const headingIndex = headings.indexOf(entry.target as HTMLElement)
17-
setActiveIndex(headingIndex)
18-
}
19-
})
12+
const updateActiveIndex = (hash: string) => {
13+
const index = toc.findIndex((item) => item.id === hash.slice(1))
14+
if (index !== -1) {
15+
setActiveIndex(index)
16+
}
17+
}
18+
19+
useEffect(() => {
20+
updateActiveIndex(window.location.hash)
21+
22+
const onHashChanged = (e: HashChangeEvent) => {
23+
updateActiveIndex(new URL(e.newURL).hash)
24+
}
2025

21-
for (const heading of headings) {
22-
if (heading) observer.observe(heading)
26+
window.addEventListener('hashchange', onHashChanged)
27+
return () => {
28+
window.removeEventListener('hashchange', onHashChanged)
2329
}
30+
}, [])
2431

25-
return () => observer.disconnect()
26-
}, [toc])
32+
// React.useEffect(() => {
33+
// const headings = toc.map((heading) => document.getElementById(heading.id))
34+
35+
// const observer = new IntersectionObserver(([entry]) => {
36+
// if (entry.intersectionRatio > 0) {
37+
// const headingIndex = headings.indexOf(entry.target as HTMLElement)
38+
// setActiveIndex(headingIndex)
39+
// }
40+
// })
41+
42+
// for (const heading of headings) {
43+
// if (heading) observer.observe(heading)
44+
// }
45+
46+
// return () => observer.disconnect()
47+
// }, [toc])
2748

2849
return (
2950
<div className="max-h-(screen-16) sticky top-16 flex flex-col justify-between overflow-y-auto pb-6">
30-
<label className={cn('mb-2 mt-12 text-sm font-semibold uppercase tracking-wide lg:text-xs')}>
51+
<label className="mb-2 mt-12 text-sm font-semibold uppercase tracking-wide text-on-surface-variant/50 lg:text-xs">
3152
On This Page
3253
</label>
33-
{toc.map((item, index) => (
34-
<h4 key={`${item.title}-${index}`}>
54+
{toc.map(({ title, id, level }, index) => (
55+
<h4 key={`${title}-${index}`}>
3556
<a
36-
aria-label={item.title}
37-
aria-current={index === activeIndex}
57+
aria-label={title}
3858
className={cn(
3959
'block py-1 text-sm font-normal leading-6 text-on-surface-variant/50 hover:underline',
40-
41-
item.parent && 'ml-4',
4260
index === activeIndex && 'text-on-surface',
4361
)}
44-
href={`#${item.id}`}
62+
style={{ marginLeft: `${(level - 1) * 1}rem` }}
63+
href={`#${id}`}
4564
>
46-
{item.title}
65+
{title}
4766
</a>
4867
</h4>
4968
))}

src/components/mdx/Toc/rehypeToc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const rehypeToc = (target: DocToC[] = [], url: string, page: string) => {
3636
const node = root.children[i]
3737

3838
if (isHeading(node)) {
39-
const level = parseInt(node.tagName[1])
39+
const level = parseInt(node.tagName[1]) - 1
4040

4141
const title = toString(node)
4242
const id = slugify(title)

src/components/mdx/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function Heading({ id, Tag, ...props }: { id?: string; Tag: Hn } & ComponentProp
2424
</a>
2525
)
2626
}
27+
export const h1 = (props: ComponentProps<'h1'>) => <h1 className="hidden" {...props} />
2728
export const h2 = ({ id, ...props }: Omit<ComponentProps<typeof Heading>, 'Tag'>) => (
2829
<Heading id={id} Tag="h2" {...props} />
2930
)

src/utils/docs.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ async function _getDocs(
146146
const tableOfContents: DocToC[] = []
147147

148148
const { content: jsx } = await compileMDX({
149-
source: content,
149+
source: `# ${title}\n ${content}`,
150150
options: {
151151
mdxOptions: {
152152
remarkPlugins: [remarkGFM],

0 commit comments

Comments
 (0)