Home

Awesome

markdown-toc NPM version NPM monthly downloads NPM total downloads

Generate a markdown TOC (table of contents) with Remarkable.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.

(TOC generated by verb using markdown-toc)

Install

Install with npm:

$ npm install --save markdown-toc

Sponsors

Thanks to the following companies, organizations, and individuals for supporting the ongoing maintenance and development of markdown-toc! Become a Sponsor to add your logo to this README, or any of my other projects

Gold Sponsors

<img src="https://github.com/jonschlinkert/clone-deep/assets/383994/98036489-2cae-48a2-8d29-7dec58ea05c4" alt="https://jaake.tech/" width="100"/>
https://jaake.tech/
<br />

Quick Start

Assuming you want to add a TOC to README.md:

  1. $ npm install -g markdown-toc
  2. Edit README.md and insert the following line where you want the TOC inserted:<br /><!-- toc -->
  3. $ markdown-toc -i README.md

CLI

Usage: markdown-toc [options] <input>

  input:        The Markdown file to parse for table of contents,
                or "-" to read from stdin.

  -i:           Edit the <input> file directly, injecting the TOC at - [Highlights](#highlights)
- [Usage](#usage)
- [API](#api)
  * [toc.plugin](#tocplugin)
  * [toc.json](#tocjson)
  * [toc.insert](#tocinsert)
  * [Utility functions](#utility-functions)
- [Options](#options)
  * [options.append](#optionsappend)
  * [options.filter](#optionsfilter)
  * [options.slugify](#optionsslugify)
  * [options.bullets](#optionsbullets)
  * [options.maxdepth](#optionsmaxdepth)
  * [options.firsth1](#optionsfirsth1)
  * [options.stripHeadingTags](#optionsstripheadingtags)
- [About](#about)

_(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_;
                (Without this flag, the default is to print the TOC to stdout.)

  --json:       Print the TOC in JSON format

  --append:     Append a string to the end of the TOC

  --bullets:    Bullets to use for items in the generated TOC
                (Supports multiple bullets: --bullets "*" --bullets "-" --bullets "+")
                (Default is "*".)

  --maxdepth:   Use headings whose depth is at most maxdepth
                (Default is 6.)

  --no-firsth1: Include the first h1-level heading in a file

  --no-stripHeadingTags: Do not strip extraneous HTML tags from heading
                         text before slugifying

  --indent:     Provide the indentation to use - defaults to '  '
                (to specify a tab, use the bash-escaped $'\t')

Highlights

Features

Safe!

Usage

var toc = require('markdown-toc');

toc('# One\n\n# Two').content;
// Results in:
// - [One](#one)
// - [Two](#two)

To allow customization of the output, an object is returned with the following properties:

API

toc.plugin

Use as a remarkable plugin.

var Remarkable = require('remarkable');
var toc = require('markdown-toc');

function render(str, options) {
  return new Remarkable()
    .use(toc.plugin(options)) // <= register the plugin
    .render(str);
}

Usage example

var results = render('# AAA\n# BBB\n# CCC\nfoo\nbar\nbaz');

Results in:

- [AAA](#aaa)
- [BBB](#bbb)
- [CCC](#ccc)

toc.json

Object for creating a custom TOC.

toc('# AAA\n## BBB\n### CCC\nfoo').json;

// results in
[ { content: 'AAA', slug: 'aaa', lvl: 1 },
  { content: 'BBB', slug: 'bbb', lvl: 2 },
  { content: 'CCC', slug: 'ccc', lvl: 3 } ]

toc.insert

Insert a table of contents immediately after an opening <!-- toc --> code comment, or replace an existing TOC if both an opening comment and a closing comment (<!-- tocstop -->) are found.

(This strategy works well since code comments in markdown are hidden when viewed as HTML, like when viewing a README on GitHub README for example).

Example

<!-- toc -->
- old toc 1
- old toc 2
- old toc 3
<!-- tocstop -->

## abc
This is a b c.

## xyz
This is x y z.

Would result in something like:

<!-- toc -->
- [abc](#abc)
- [xyz](#xyz)
<!-- tocstop -->

## abc
This is a b c.

## xyz
This is x y z.

Utility functions

As a convenience to folks who wants to create a custom TOC, markdown-toc's internal utility methods are exposed:

var toc = require('markdown-toc');

Example

var result = toc('# AAA\n## BBB\n### CCC\nfoo');
var str = '';

result.json.forEach(function(heading) {
  str += toc.linkify(heading.content);
});

Options

options.append

Append a string to the end of the TOC.

toc(str, {append: '\n_(TOC generated by Verb)_'});

options.filter

Type: Function

Default: undefined

Params:

Example

From time to time, we might get junk like this in our TOC.

[.aaa([foo], ...) another bad heading](#-aaa--foo--------another-bad-heading)

Unless you like that kind of thing, you might want to filter these bad headings out.

function removeJunk(str, ele, arr) {
  return str.indexOf('...') === -1;
}

var result = toc(str, {filter: removeJunk});
//=> beautiful TOC

options.slugify

Type: Function

Default: Basic non-word character replacement.

Example

var str = toc('# Some Article', {slugify: require('uslug')});

options.bullets

Type: String|Array

Default: *

The bullet to use for each item in the generated TOC. If passed as an array (['*', '-', '+']), the bullet point strings will be used based on the header depth.

options.maxdepth

Type: Number

Default: 6

Use headings whose depth is at most maxdepth.

options.firsth1

Type: Boolean

Default: true

Exclude the first h1-level heading in a file. For example, this prevents the first heading in a README from showing up in the TOC.

options.stripHeadingTags

Type: Boolean

Default: true

Strip extraneous HTML tags from heading text before slugifying. This is similar to GitHub markdown behavior.

About

<details> <summary><strong>Contributing</strong></summary>

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

</details> <details> <summary><strong>Running Tests</strong></summary>

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
</details> <details> <summary><strong>Building docs</strong></summary>

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb
</details>

Related projects

You might also be interested in these projects:

Contributors

CommitsContributor
199jonschlinkert
9doowb
4dbooth-boston
3sapegin
3Marsup
2dvcrn
2maxogden
2twang2218
2zeke
1Vortex375
1chendaniely
1Daniel-Mietchen
1Feder1co5oave
1garygreen
1TehShrike
1citizenmatt
1mgroenhoff
1rafaelsteil
1RichardBradley
1sethvincent
1shanehughes3
1bcho
1lu22do

Author

Jon Schlinkert

License

Copyright © 2023, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on July 12, 2023.