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

Parse markdown content and return an array of TOC items.

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

Portable Text (Sanity)

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

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

Output

An array of TOCItemType is returned.

Last updated on