mirror of
https://github.com/netbirdio/docs.git
synced 2026-04-16 07:26:35 +00:00
add TOC
This commit is contained in:
@@ -2,12 +2,14 @@ import Head from 'next/head'
|
||||
import { Router, useRouter } from 'next/router'
|
||||
import { MDXProvider } from '@mdx-js/react'
|
||||
|
||||
import { Layout } from '@/components/Layout'
|
||||
import { LayoutAPI } from '@/components/LayoutAPI'
|
||||
import * as mdxComponents from '@/components/mdx'
|
||||
import { useMobileNavigationStore } from '@/components/MobileNavigation'
|
||||
|
||||
import '@/styles/tailwind.css'
|
||||
import 'focus-visible'
|
||||
import {LayoutDocs} from "@/components/LayoutDocs";
|
||||
import {slugifyWithCounter} from "@sindresorhus/slugify";
|
||||
|
||||
function onRouteChange() {
|
||||
useMobileNavigationStore.getState().close()
|
||||
@@ -18,7 +20,7 @@ Router.events.on('hashChangeStart', onRouteChange)
|
||||
|
||||
export default function App({ Component, pageProps }) {
|
||||
let router = useRouter()
|
||||
|
||||
let tableOfContents = collectHeadings(pageProps.sections)
|
||||
return (
|
||||
<>
|
||||
<Head>
|
||||
@@ -31,10 +33,45 @@ export default function App({ Component, pageProps }) {
|
||||
<meta name="description" content={pageProps.description} />
|
||||
</Head>
|
||||
<MDXProvider components={mdxComponents}>
|
||||
<Layout {...pageProps}>
|
||||
<Component {...pageProps} />
|
||||
</Layout>
|
||||
|
||||
{router.pathname.startsWith("/docs") ? (
|
||||
<LayoutDocs title={pageProps.title.toString()} tableOfContents={tableOfContents} {...pageProps}>
|
||||
<Component {...pageProps} />
|
||||
</LayoutDocs>
|
||||
) : (
|
||||
<LayoutAPI {...pageProps}>
|
||||
<Component {...pageProps} />
|
||||
</LayoutAPI>
|
||||
)}
|
||||
</MDXProvider>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
function collectHeadings(sections, slugify = slugifyWithCounter()) {
|
||||
let output = []
|
||||
|
||||
for (let section of sections) {
|
||||
if (section.tagName === 'h2' || section.tagName === 'h3') {
|
||||
let title = section.title
|
||||
let id = section.id
|
||||
if (section.tagName === 'h3') {
|
||||
if (!output[output.length - 1]) {
|
||||
throw new Error(
|
||||
'Cannot add `h3` to table of contents without a preceding `h2`'
|
||||
)
|
||||
}
|
||||
output[output.length - 1].children.push({
|
||||
id,
|
||||
title,
|
||||
})
|
||||
} else {
|
||||
output.push({ id, title, children: [] })
|
||||
}
|
||||
}
|
||||
|
||||
output.push(...collectHeadings(output.children ?? [], slugify))
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user