Markdown
/ API Reference
Markdown
/ API Reference

getHeadings

Extracts a flat list of headings from a markdown string.
This is useful for creating a table of contents or for other purposes.

The Gist

import { getHeadings } from '@weser/markdown'

const markdown = `
# Heading 1
## Heading 2
### Heading 3
#### Heading 4
`

const headings = getHeadings(markdown)

console.log(headings)
// [
//   { id: 'heading-1', children: 'Heading 1', depth: 1 },
//   { id: 'heading-2', children: 'Heading 2', depth: 2 },
//   { id: 'heading-3', children: 'Heading 3', depth: 3 },
//   { id: 'heading-4', children: 'Heading 4', depth: 4 },
// ]

Parameters

ParameterTypeDescription
markdownstringThe markdown string to extract headings from.
configConfig?The configuration for the headings.

Config

ParameterTypeDefaultDescription
minDepthnumber1The minimum depth of headings to extract.
maxDepthnumber4The maximum depth of headings to extract.

Returns

(Array<T_Heading>) A flat list of headings.

On this page