Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@substrate-system/markdown-toc

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

anchor, API documentation, code documentation, commonmark, content navigation, content organization, developer tools, docs, document structure, document, documentation tool, documentation, glossary, heading, headings, index, javascript, links, markdown parser, markdown tools, Markdown-based, markdown-toc, markdown, markup language, md files, md parser, md-toc, md, node.js, npm, open-source, outline, package.json, plugin, README tool, readme, reference, remarkable, remarkableplugin, render, renderer, structure, table of contents, table-of-contents, table, toc-generator, toc, write

readme

markdown-toc

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.

fork

This is a fork of jonschlinkert/markdown-toc.

<summary>

Contents

</summary>

Install

Install with npm:

npm i -D @substrate-system/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

https://jaake.tech/
https://jaake.tech/

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:
    <!-- 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

  • Can optionally be used as a remarkable plugin
  • Returns an object with the rendered TOC (on content), as well as a json property with the raw TOC object, so you can generate your own TOC using templates or however you want
  • Works with repeated headings
  • Uses sane defaults, so no customization is necessary, but you can if you need to.
  • filter out headings you don't want
  • Improve the headings you do want
  • Use a custom slugify function to change how links are created

Safe!

  • Won't mangle markdown in code examples in gfm code blocks that other TOC generators mistake as being actual headings (this happens when markdown headings are show in examples, meaning they arent' actually headings that should be in the toc. Also happens with yaml and coffee-script comments, or any comments that use #)
  • Won't mangle front-matter, or mistake front-matter properties for headings like other TOC generators

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:

  • content {String}: The generated table of contents. Unless you want to customize rendering, this is all you need.
  • highest {Number}: The highest level heading found. This is used to adjust indentation.
  • tokens {Array}: Headings tokens that can be used for custom rendering

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');
  • toc.bullets(): render a bullet list from an array of tokens
  • toc.linkify(): linking a heading content string
  • toc.slugify(): slugify a heading content string
  • toc.strip(): strip words or characters from a heading content string

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:

  • str {String} the actual heading string
  • ele {Objecct} object of heading tokens
  • arr {Array} all of the headings objects

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

<summary>Contributing</summary>

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

<summary>Running Tests</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
<summary>Building docs</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

You might also be interested in these projects:

Contributors

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

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.

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Generated by auto-changelog.

v1.3.5

Commits

v1.3.4 - 2025-05-01

Commits

v1.3.3 - 2024-10-20

Commits

v1.3.2 - 2024-10-20

Commits

v1.3.1 - 2024-10-20

Merged

  • Add indent cli argument to docs #105
  • expose utils via toc.utils #93
  • Update gray-matter to v3. #108
  • Added Quick Start section #100

Fixed

  • expose utils via toc.utils #92

Commits

1.2.0 - 2017-09-19

Merged

  • Pass options via CLI #95
  • fixed typo #79

Fixed

  • Pass options via CLI #94

Commits

1.1.0 - 2017-01-15

Commits

1.0.3 - 2017-01-01

Commits

  • only set linkify to true if undefined e534a7d

1.0.2 - 2016-12-09

Fixed

Commits

1.0.1 - 2016-12-09

Fixed

Commits

1.0.0 - 2016-12-09

Merged

  • Correctly handles accents / diacritics in links #76
  • option linkify to disable links in inserted tocs #72 #73

Commits

0.13.0 - 2016-09-27

Merged

  • Add escape to linkify() so it can handle CJK characters correctly #68
  • strip CJK punctuations in slugs. Fix #58 #70

Fixed

  • Merge pull request #70 from twang2218/issue58 #58
  • strip CJK punctuations in slugs. Fix #58 #58

Commits

0.12.16 - 2016-07-31

Commits

  • ensure slugs are based on actual heading text cb62e3c
  • generate docs afc63df

0.12.15 - 2016-07-25

Merged

  • Made fix to avoid crashing on empty headers. Added corresponding test. #64

Commits

0.12.14 - 2017-09-19

Commits

0.12.13 - 2017-09-19

Commits

0.12.12 - 2017-09-19

Fixed

0.12.11 - 2017-09-19

Fixed

0.12.10 - 2017-09-19

Fixed

0.12.9 - 2017-09-19

0.12.7 - 2017-09-19

Merged

  • fix slug generation for underscores #52

Fixed

  • improved slugification #53 #55
  • fix slug generation for underscores #51

Commits

0.12.6 - 2016-04-23

Merged

  • Fix slug generation of headlines including colons (:) #50

Fixed

Commits

0.12.5 - 2017-09-19

Merged

  • options.maxdepth is broken #47

Commits

0.12.3 - 2017-09-19

Merged

  • Include slug in json #43

Commits

0.12.2 - 2017-09-19

Fixed

0.12.1 - 2017-09-19

Fixed

Commits

0.12.0 - 2017-09-19

0.11.10 - 2017-09-19

Fixed

Commits

0.11.9 - 2017-09-19

Fixed

0.11.8 - 2017-09-19

Merged

  • 37 add CLI support for inserting TOCs inline into files #38

Fixed

Commits

0.11.7 - 2015-09-17

Commits

  • ensure options are merged 4f97796

0.11.6 - 2015-09-17

Commits

0.11.5 - 2017-09-19

Commits

  • lazily require gray-matter aabd1d2

0.11.4 - 2015-05-21

Merged

Commits

0.11.3 - 2015-04-27

Commits

0.11.2 - 2015-04-23

Merged

  • Fix typo in README.md #28

Commits

0.11.0 - 2015-03-25

Commits

  • adds append docs b5a0b0d
  • adds append option, for appending content after toc 02c77d5

0.10.0 - 2017-09-19

Commits

0.9.0 - 2015-03-14

Commits

0.8.0 - 2017-09-19

Commits

0.7.2 - 2017-09-19

Merged

  • Fix sorting of highest #26

Commits

0.7.1 - 2017-09-19

Merged

  • Revert change from f9495f8b0f8 #25

0.7.0 - 2017-09-19

Merged

  • Allow options passing through insert #24

Commits

0.6.1 - 2017-09-19

Commits

0.6.0 - 2017-09-19

Commits

0.5.1 - 2017-09-19

Commits

0.4.0 - 2017-09-19

0.3.1 - 2017-09-19

Commits

0.3.0 - 2014-12-10

Merged

  • Specify different bullet chars at different depths #20
  • Added ability to use a custom slugify function #15
  • need global install flag to work as stated in the readme #12
  • document maxDepth option #10

Commits

v0.2.6 - 2014-07-07

Merged

  • Ensure toc comments have newlines #8

Commits

v0.2.5 - 2017-09-19

Merged

  • use _.extend on options in toc.add() method #4

v0.2.4 - 2017-09-19

v0.2.3 - 2017-09-19

v0.2.2 - 2014-02-17

Commits

v0.2.1 - 2017-09-19

Commits

v0.2.0 - 2014-02-03

Merged

  • Clean up tests #2

Commits

  • updates tests and adds a bunch of test fixtures 19cc5a0
  • first commit d415e5f
  • resolves jonschlinkert/marked-toc/#1 339e696