Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork720
API and CLI for generating a markdown TOC (table of contents) for a README or any markdown files. Uses Remarkable to parse markdown. Used by NASA/openmct, Prisma, Joi, Mocha, Sass, Prettier, Orbit DB, FormatJS, Raneto, hapijs/code, webpack-flow, docusaurus, release-it, ts-loader, json-server, reactfire, bunyan, husky, react-easy-state, react-sna…
License
jonschlinkert/markdown-toc
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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 ❤️ and support.
(TOC generated byverb usingmarkdown-toc)
Install withnpm:
$ npm install --save markdown-toc
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 ofmy other projects
![]() |
|---|
| https://jaake.tech/ |
Assuming you want to add a TOC to README.md:
$ npm install -g markdown-toc- Edit README.md and insert the following line where you want the TOC inserted:
<!-- toc --> $ markdown-toc -i README.md
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')Features
- Can optionally be used as aremarkable plugin
- Returns an object with the rendered TOC (on
content), as well as ajsonproperty with the raw TOC object, so you can generate your own TOC using templates or however you want - Works withrepeated 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 customslugify 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 inexamples, 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
vartoc=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
Use as aremarkable plugin.
varRemarkable=require('remarkable');vartoc=require('markdown-toc');functionrender(str,options){returnnewRemarkable().use(toc.plugin(options))// <= register the plugin.render(str);}
Usage example
varresults=render('# AAA\n# BBB\n# CCC\nfoo\nbar\nbaz');
Results in:
- [AAA](#aaa)- [BBB](#bbb)- [CCC](#ccc)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}]
Insert a table of contents immediately after anopening<!-- toc --> code comment, or replace an existing TOC if both anopening comment and aclosing 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 -->## abcThis is a b c.## xyzThis is x y z.Would result in something like:
<!-- toc -->- [abc](#abc)- [xyz](#xyz)<!-- tocstop -->## abcThis is a b c.## xyzThis is x y z.As a convenience to folks who wants to create a custom TOC, markdown-toc's internal utility methods are exposed:
vartoc=require('markdown-toc');
toc.bullets(): render a bullet list from an array of tokenstoc.linkify(): linking a headingcontentstringtoc.slugify(): slugify a headingcontentstringtoc.strip(): strip words or characters from a headingcontentstring
Example
varresult=toc('# AAA\n## BBB\n### CCC\nfoo');varstr='';result.json.forEach(function(heading){str+=toc.linkify(heading.content);});
Append a string to the end of the TOC.
toc(str,{append:'\n_(TOC generated by Verb)_'});
Type:Function
Default:undefined
Params:
str{String} the actual heading stringele{Objecct} object of heading tokensarr{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.
functionremoveJunk(str,ele,arr){returnstr.indexOf('...')===-1;}varresult=toc(str,{filter:removeJunk});//=> beautiful TOC
Type:Function
Default: Basic non-word character replacement.
Example
varstr=toc('# Some Article',{slugify:require('uslug')});
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.
Type:Number
Default:6
Use headings whose depth is at most maxdepth.
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.
Type:Boolean
Default:true
Strip extraneous HTML tags from heading text before slugifying. This is similar to GitHub markdown behavior.
Contributing
Pull requests and stars are always welcome. For bugs and feature requests,please create an issue.
Running Tests
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&& npmtest
Building docs
(This project's readme.md is generated byverb, 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&& verbYou might also be interested in these projects:
- gfm-code-blocks: Extract gfm (GitHub Flavored Markdown) fenced code blocks from a string. |homepage
- markdown-link: Micro util for generating a single markdown link. |homepage
- markdown-utils: Tiny helpers for creating consistenly-formatted markdown snippets. |homepage
- pretty-remarkable: Plugin for prettifying markdown with Remarkable using custom renderer rules. |homepage
- remarkable: Markdown parser, done right. 100% Commonmark support, extensions, syntax plugins, high speed - all in…more |homepage
Jon Schlinkert
Copyright © 2023,Jon Schlinkert.Released under theMIT License.
This file was generated byverb-generate-readme, v0.8.0, on July 12, 2023.
About
API and CLI for generating a markdown TOC (table of contents) for a README or any markdown files. Uses Remarkable to parse markdown. Used by NASA/openmct, Prisma, Joi, Mocha, Sass, Prettier, Orbit DB, FormatJS, Raneto, hapijs/code, webpack-flow, docusaurus, release-it, ts-loader, json-server, reactfire, bunyan, husky, react-easy-state, react-sna…
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
