Get TOC

Parse Table of contents from markdown/mdx content

Parse Table of contents from markdown/mdx content.

Usage

Note: If you're using a CMS, you should use the API provided by the CMS instead.

Markdown/MDX

import { getTableOfContents } from 'next-docs-zeta/server'
 
const toc = await getTableOfContents(page.body.raw)

Portable Text (Sanity)

import { getTableOfContentsFromPortableText } from 'next-docs-zeta/server'
 
const posts = await client.fetch(query)
 
const toc = await getTableOfContentsFromPortableText(posts[0].body)

getTableOfContents

Parse markdown content and return an array of TOC items.

getTableOfContentsFromPortableText

Takes a portable text and slug function, return an array of TOC items with their slug generated using the slug function.

TOCItemType

PropTypeDescription
titlestringTitle of heading
urlstringHref url
depthnumberFrom 1 to 6

Last updated on