This commit is contained in:
Pascal Fischer
2023-05-10 00:46:38 +02:00
parent c15d7607e2
commit 6131138e65
13 changed files with 476 additions and 25 deletions

View File

@@ -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
}