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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@takanorip/markdown-it-table-of-contents

v0.5.2

Published

A Markdown-it plugin for adding a table of contents to markdown documents

Downloads

2

Readme

markdown-it-table-of-contents

A table of contents plugin for Markdown-it. Simple, customizable and with a default slugifier that matches that of https://www.npmjs.com/package/markdown-it-anchor (>5.0.0).

Looking for maintainer

I'm looking for someone to take over this package to maintain and improve on it. Interested? Open an issue and just quickly explain your thoughts...

Usage

var MarkdownIt = require("markdown-it");
var md = new MarkdownIt();

md.use(require("markdown-it-anchor").default); // Optional, but makes sense as you really want to link to something
md.use(require("markdown-it-table-of-contents"));

Then add [[toc]] where you want the table of contents to be added in your markdown.

Example markdown

This markdown:

# Heading

[[toc]]

## Sub heading 1
Some nice text

## Sub heading 2
Some even nicer text

... would render this HTML using the default options specified in "usage" above:

<h1 id="heading">Heading</h1>

<div class="table-of-contents">
  <ul>
    <li><a href="#heading">Heading</a>
      <ul>
        <li><a href="#sub-heading-1">Sub heading 1</a></li>
        <li><a href="#sub-heading-2">Sub heading 2</a></li>
      </ul>
    </li>
  </ul>
</div>

<h2 id="sub-heading-1">Sub heading 1</h2>
<p>Some nice text</p>

<h2 id="sub-heading-2">Sub heading 2</h2>
<p>Some even nicer text</p>

Options

You may specify options when useing the plugin. like so:

md.use(require("markdown-it-table-of-contents"), options);

These options are available:

Name | Description | Default -----------------------|-------------------------------------------------------------------------------------|------------------------------------ "includeLevel" | Headings levels to use (2 for h2:s etc) | [1, 2] "containerClass" | The class for the container DIV | "table-of-contents" "slugify" | A custom slugification function | encodeURIComponent(String(s).trim().toLowerCase().replace(/\s+/g, '-')) "markerPattern" | Regex pattern of the marker to be replaced with TOC | /^\[\[toc\]\]/im "listType" | Type of list (ul for unordered, ol for ordered) | ul "format" | A function for formatting headings (see below) | md.renderInline(content) "containerHeaderHtml" | Optional HTML string for container header | <div class="toc-container-header">Contents</div> "containerFooterHtml" | Optional HTML string for container footer | <div class="toc-container-footer">Footer</div> "transformLink" | A function for transforming the TOC links | undefined

format is an optional function for changing how the headings are displayed in the TOC.

By default, TOC headings will be formatted using markdown-it's internal MD formatting rules (i.e. it will be formatted using the same rules / extensions as other markdown in your document). You can override this behavior by specifying a custom format function. The function should accept two arguments:

  1. content - The heading test, as a markdown string.
  2. md – markdown-it's internal markdown parser object. This should only be need for advanced use cases.
function format(content, md) {
  // manipulate the headings as you like here.
  return manipulatedHeadingString;
}

transformLink is an optional function for transform the link as you like.

function transformLink(link) {
  // transform the link as you like here.
  return transformedLink;
}