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

@lincy/markdown-it-toc-and-anchor

v5.0.0

Published

markdown-it plugin to add toc and anchor links in headings

Readme

@lincy/markdown-it-toc-and-anchor

English | 简体中文

npm version License: MIT

markdown-it plugin that adds table of contents and anchor links to headings.

Fork of markdown-it-toc-and-anchor, rewritten in TypeScript with built-in type definitions.

Features

  • Insert a table of contents at @[toc] in your Markdown
  • Add id attributes and optional anchor links (#) to headings
  • Customize TOC level range, CSS classes, slug generation, and anchor placement
  • Receive TOC data via callback (Markdown, structured array, or HTML)

Installation

pnpm add @lincy/markdown-it-toc-and-anchor markdown-it
npm install @lincy/markdown-it-toc-and-anchor markdown-it
yarn add @lincy/markdown-it-toc-and-anchor markdown-it

markdown-it is a peer dependency at runtime — install it in your project.

Usage

Basic

Place @[toc] where you want the table of contents to appear:

@[toc]

# Introduction

## Getting started
import markdownItTocAndAnchor from '@lincy/markdown-it-toc-and-anchor'
import MarkdownIt from 'markdown-it'

const md = new MarkdownIt({
    html: true,
    linkify: true,
})

md.use(markdownItTocAndAnchor)

const html = md.render(markdown)

Rendered output includes a TOC list at the @[toc] position and id attributes on each heading. Anchor links are enabled by default.

With options

import markdownItTocAndAnchor from '@lincy/markdown-it-toc-and-anchor'
import MarkdownIt from 'markdown-it'

const md = new MarkdownIt({ html: true, linkify: true })

md.use(markdownItTocAndAnchor, {
    tocClassName: 'table-of-contents',
    anchorLinkSymbol: '¶',
    anchorLinkBefore: false,
    tocFirstLevel: 2,
    tocLastLevel: 4,
})

const html = md.render(markdown)

TypeScript

The plugin exports types you can import directly:

import type {
    PluginOptions,
    TocCallback,
    TocHeading,
} from '@lincy/markdown-it-toc-and-anchor'

Options

All options are optional.

| Option | Type | Default | Description | | --- | --- | --- | --- | | toc | boolean | true | Enable or disable replacing @[toc] with the generated TOC HTML | | tocClassName | string \| null | "markdownIt-TOC" | CSS class for the TOC <ul>. Set to null to omit the class | | tocFirstLevel | number | 1 | Lowest heading level included in the TOC (e.g. 2 skips <h1>) | | tocLastLevel | number | 6 | Highest heading level included in the TOC (e.g. 5 skips <h6>) | | tocCallback | TocCallback \| null | null | Callback invoked with TOC data after parsing. See TOC callback | | anchorLink | boolean | true | Add clickable anchor links inside headings | | anchorLinkSymbol | string | "#" | Text shown for the anchor link | | anchorLinkBefore | boolean | true | Place the anchor link before (true) or after (false) the heading text | | anchorLinkSpace | boolean | true | Insert a space between the anchor link and heading text | | anchorLinkSymbolClassName | string \| null | null | When set, wrap the symbol in <span class="..."> | | anchorClassName | string \| null | "markdownIt-Anchor" | CSS class for anchor <a> elements. Set to null to omit | | anchorLinkPrefix | string | undefined | Prefix added to generated heading IDs (e.g. "section-") | | wrapHeadingTextInAnchor | boolean | false | Wrap the entire heading text in the anchor link (overrides symbol and position options) | | resetIds | boolean | true | Reset duplicate-ID counters on each render. Set to false when rendering multiple documents on one page | | slugify | (value: string) => string | uslug | Custom function to generate heading IDs from text |

Custom slugify

md.use(markdownItTocAndAnchor, {
    slugify: value => `/some/prefix/${value.replace(/([/ '])/g, '_')}`,
})

TOC callback

Use tocCallback to receive TOC data without inserting it into the document, or in addition to @[toc].

interface TocHeading {
    content: string
    anchor: string
    level: number
}

type TocCallback = (
    tocMarkdown: string,
    tocArray: TocHeading[],
    tocHtml: string,
) => void

Plugin options

md.use(markdownItTocAndAnchor, {
    toc: false,
    tocCallback(tocMarkdown, tocArray, tocHtml) {
        console.log(tocHtml)
    },
})

Global markdown-it options

const md = new MarkdownIt().use(markdownItTocAndAnchor)

md.set({
    tocCallback(tocMarkdown, tocArray, tocHtml) {
        console.log(tocHtml)
    },
})

md.render(markdown)

Render environment

md.render(markdown, {
    tocCallback(tocMarkdown, tocArray, tocHtml) {
        console.log(tocHtml)
    },
})

When multiple sources define a callback, priority is: render environment → plugin options → global markdown-it options.

Development

pnpm install
pnpm test          # run tests
pnpm test:watch    # watch mode
pnpm build         # compile to dist/
pnpm lint          # eslint
pnpm lint:ts       # type check

Contributing

  • Pull requests and stars are welcome.
  • For bugs and feature requests, please open an issue.
  • Pull requests should include passing tests (pnpm test).

Changelog

License

MIT — see LICENSE. Based on the original work by Maxime Thirouin.