npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

markdown-toc-number

v1.0.1

Published

Auto add heading numbers and generate TOC for Markdown documents

Downloads

18

Readme

markdown-toc-number

TypeScript npm npm license

Auto add heading numbers and generate TOC for Markdown documents

1. Features

  • #️⃣ Heading Numbers: Auto add hierarchical numbers for H1~H6 (e.g. 1., 1.1., 1.1.1.)
  • 📑 TOC Generation: Auto extract headings and generate Markdown TOC
  • Async/Sync: Support both async and sync versions

2. Install

pnpm add markdown-toc-number
# 或
npm install markdown-toc-number

3. Usage

3.1 Add Heading Numbers

3.1.1 addHeadingNumbers(markdown, options?)

import { addHeadingNumbers } from 'markdown-toc-number'

const result = await addHeadingNumbers(`# Title
## Section 1
### Sub Section 1.1
`)
// # Title
// ## 1. Section 1
// ### 1.1. Sub Section 1.1

3.1.2 addHeadingNumbersSync(markdown, options?)

import { addHeadingNumbersSync } from 'markdown-toc-number'

const result = addHeadingNumbersSync(`# Title
## Section 1
`)

3.1.3 HeadingNumberOptions

| Option | Type | Default | Description | | -------------------------- | ------------------ | ------------- | ------------------------------------ | | resetPerFile | boolean | true | Reset counters per file | | separator | string | '. ' | Separator between number and heading | | startFromLevel | number | 2 | Start numbering from level (1-6) | | cleanupExistingNumbers | boolean | true | Cleanup existing numbers | | existingSeparatorPattern | string \| RegExp | '[.、\\s]+' | Pattern for existing numbers |

Example:

// Start numbering from H1
await addHeadingNumbers(markdown, {
  startFromLevel: 1,
})
// # 1. Title
// ## 1.1. Section 1

3.2 TOC Generation

3.2.1 generateToc(markdown, options?)

Insert TOC at the top of the document

import { generateToc } from 'markdown-toc-number'

const result = await generateToc(`# Title
## Section 1
### Sub Section
`)
// ## Table of Contents
// * [Title](#title)
//   * [Section 1](#section1)
//     * [Sub Section](#subsection)

3.2.2 generateTocSync(markdown, options?)

3.2.3 extractToc(markdown, options?)

Return TOC only, without inserting into the document

const toc = await extractToc(markdown)
// Returns TOC content only

3.2.4 TocOptions

| Option | Type | Default | Description | | ----------- | ------------------------------------------- | ------------- | ------------------------------- | | maxDepth | number | 6 | Maximum heading depth (1-6) | | minDepth | number | 1 | Minimum heading depth (1-6) | | title | string | '目录' | TOC title | | showTitle | boolean | true | Show TOC title | | position | 'top' \| 'before-first-heading' \| 'none' | 'top' | TOC insertion position | | withLinks | boolean | true | Include anchor links | | slugify | (text: string) => string | default fn | Custom anchor generation | | listStyle | 'unordered' \| 'ordered' | 'unordered' | List style | | tight | boolean | false | Tight mode (remove blank lines) |

Example:

// Generate ordered TOC with depth 2-4
await generateToc(markdown, {
  listStyle: 'ordered',
  minDepth: 2,
  maxDepth: 4,
  title: 'Table of Contents',
})

// Custom anchor generation
await generateToc(markdown, {
  slugify: text => text.toUpperCase().replace(/\s/g, '_'),
})