diff --git a/apps/temp-docs/components/CustomHTMLElements/CustomHTMLElements.utils.ts b/apps/temp-docs/components/CustomHTMLElements/CustomHTMLElements.utils.ts index fbf05c39a7b..6bae9755003 100644 --- a/apps/temp-docs/components/CustomHTMLElements/CustomHTMLElements.utils.ts +++ b/apps/temp-docs/components/CustomHTMLElements/CustomHTMLElements.utils.ts @@ -1,11 +1,20 @@ export const getAnchor = (text: any): string | undefined => { - if (typeof text !== 'string') { - return undefined - } else { + if (typeof text === 'object') { + const formattedText = text.map((x) => { + if (typeof x !== 'string') { + return x.props.children + } else { + return x.trim() + } + }) + return formattedText.join('-').toLowerCase() + } else if (typeof text === 'string') { return text .toLowerCase() - .replace(/[^a-z0-9 ]/g, '') + .replace(/[^a-z0-9- ]/g, '') .replace(/[ ]/g, '-') + } else { + return undefined } } diff --git a/apps/temp-docs/components/CustomHTMLElements/Heading.tsx b/apps/temp-docs/components/CustomHTMLElements/Heading.tsx index 478ad47ab32..feed568e44c 100644 --- a/apps/temp-docs/components/CustomHTMLElements/Heading.tsx +++ b/apps/temp-docs/components/CustomHTMLElements/Heading.tsx @@ -13,7 +13,9 @@ const Heading = ({ tag, children }) => { const link = `#${anchor}` // check if current heading is in view, update TOC active item accordingly - const { ref, inView, entry } = useInView({ + // [Joshen] Ideally we highlight the section in the TOC when it's at the top of the page + // much like when we click on the item in the TOC itself, the rootMargin fix is insufficient + const { ref } = useInView({ threshold: 1, onChange: (inView, entry) => { if (window.scrollY === 0) unHighlightSelectedTocItems() diff --git a/apps/temp-docs/components/TableOfContents.tsx b/apps/temp-docs/components/TableOfContents.tsx new file mode 100644 index 00000000000..3f64c1350a7 --- /dev/null +++ b/apps/temp-docs/components/TableOfContents.tsx @@ -0,0 +1,48 @@ +import { FC } from 'react' +import { removeAnchor } from './CustomHTMLElements/CustomHTMLElements.utils' + +interface TOCHeader { + id: number + lvl: number + seen: number + content: string + slug: string +} + +interface Props { + toc: any +} + +const formatSlug = (slug: string) => { + // [Joshen] We will still provide support for headers declared like this: + // ## REST API {#rest-api-overview} + // At least for now, this was a docusaurus thing. + if (slug.includes('#')) return slug.split('-#')[0] + return slug +} + +const TableOfContents: FC = ({ toc }) => { + // [Joshen] markdown-toc doesn't seem to read maxdepth from the options passed in + // Our first level headers will be H2s (H1 is ignored), and we only show up to H3 + + return ( + + ) +} + +export default TableOfContents diff --git a/apps/temp-docs/layouts/Default.tsx b/apps/temp-docs/layouts/Default.tsx index 6dbbaad90e8..0276837c775 100644 --- a/apps/temp-docs/layouts/Default.tsx +++ b/apps/temp-docs/layouts/Default.tsx @@ -3,6 +3,7 @@ import Head from 'next/head' import NavBar from '../components/Navigation/NavBar' import SideBar from '../components/Navigation/SideBar' import Footer from '../components/Footer' +import TableOfContents from '~/components/TableOfContents' interface Props { meta: { title: string; description?: string; hide_table_of_contents?: boolean } @@ -34,12 +35,6 @@ const Layout: FC = ({ meta, children, toc, menuItems, currentPage }) => { - {/* */} - {/* */}
@@ -49,7 +44,7 @@ const Layout: FC = ({ meta, children, toc, menuItems, currentPage }) => {
@@ -58,26 +53,7 @@ const Layout: FC = ({ meta, children, toc, menuItems, currentPage }) => {
{toc && !meta?.hide_table_of_contents && (
-
    - {toc.json - .filter((item) => item.lvl !== 1) - .map((item: any, i: number) => { - return ( -
  • - - {item.content} - -
  • - ) - })} -
+
)}
diff --git a/apps/temp-docs/pages/[...slug].tsx b/apps/temp-docs/pages/[...slug].tsx index 3a1c8a90c0c..0bcff406c77 100644 --- a/apps/temp-docs/pages/[...slug].tsx +++ b/apps/temp-docs/pages/[...slug].tsx @@ -1,3 +1,4 @@ +import toc from 'markdown-toc' import { MDXProvider } from '@mdx-js/react' import { useRouter } from 'next/router' import { MDXRemote } from 'next-mdx-remote' @@ -8,9 +9,6 @@ import { getPageType } from '../lib/helpers' import { getAllDocs, getDocsBySlug } from '../lib/docs' import Layout from '~/layouts/Default' -// table of contents extractor -const toc = require('markdown-toc') - interface Meta { id: string title: string @@ -54,7 +52,7 @@ export async function getStaticProps({ params }: { params: { slug: string[] } }) props: { ...doc, content, - toc: toc(doc.content, { maxdepth: 2 }), + toc: toc(doc.content, { maxdepth: 1, firsth1: false }), }, } }