Skip to content

VaultMD / extractHeadings

Function: extractHeadings()

extractHeadings(content): Heading[]

Defined in: src/headings/extract.ts:33

Extract every ATX heading (# through ######) from a markdown string, in document order, with the span of the section each one opens.

Headings inside fenced code blocks are skipped, using the same CommonMark rule the rest of the package applies. Setext headings (Title over ===) are not recognised.

The string is scanned VERBATIM: a leading YAML frontmatter block is not removed, so a # comment line inside one reads as a level-1 heading. Pass parseFrontmatter(content).body when that matters — the same caveat extractLinks carries.

Parameters

content

string

Raw UTF-8 content of a markdown file, or any substring of one.

Returns

Heading[]

The headings found, each carrying its Heading span offsets into content.

Example

ts
const { body } = parseFrontmatter(content);
const notes = extractHeadings(body).find((h) => h.text === 'Notes');
const section = notes ? body.slice(notes.bodyStart, notes.end) : '';

Released under the MIT License.