
Product
Introducing Custom Tabs for Org Alerts
Create and share saved alert views with custom tabs on the org alerts page, making it easier for teams to return to consistent, named filter sets.
marked-toc
Advanced tools

Generate a TOC (table of contents) for markdown files
(example)
Install the module with npm:
npm i -g marked-toc --save
In any markdown file, add <!-- toc --> where you want to add the TOC. Then in the command line, run:
toc [filename]
If you add the toc to a README.md, no need to add [filename], just run toc.
var toc = require('marked-toc');
var file = fs.readFileSync('README.md', 'utf8');
// Generate a TOC
toc(file);
All methods accept an object of options as the last argument.
Type: String
Default: <%= depth %><%= bullet %>[<%= heading %>](#<%= url %>)\n
The Lo-Dash template used to generate the Table of Contents.
Example (this is the default):
var tmpl = '<%= depth %><%= bullet %>[<%= heading %>](#<%= url %>)\n';
toc(file, {template: tmpl});
Type: String|Array
Default: *
The bullet to use for each item in the generated TOC. This is passed as a variable to the <%= bullet %> template.
If an array, like ['* ', '- '], the bullet point strings will be used based on the header depth.
Type: Number
Default: 3
Use headings whose depth is at most maxDepth.
Type: Boolean
Default: False
Include the first h1-level heading in a file. For example, this prevent the first heading in a README from showing up in the TOC.
Type: Array
Default: ['Table of Contents', 'TOC', 'TABLE OF CONTENTS']
Omit entire headings from the TOC if they have these strings.
Type: Array
Default: ['mixin', 'helper', 'filter']
Strip "blacklisted" keywords from the headings.
Example:
toc(file, {clean: ['docs', 'methods']});
converts this:
## docs-foo
Foo
## methods-bar
Bar
to:
* [foo](#docs-foo)
* [bar](#methods-bar)
Type: Boolean
Default: true
An array of strings used the omit option:
['grunt', 'helper', 'handlebars-helper', 'mixin', 'filter', 'assemble-contrib', 'assemble']
(These strings are used a lot in documentation headings, but (usually) shouldn't show up in the gererated TOC.)
Type: String
Default: -
String of chars that you want to be whitelisted when headings are "slugified" for links, e.g. -_~.
Example:
// This heading
# Getting Started
// Converts to this link
* [Getting Started](#getting-started)
Most methods expect a string as the first paramter, so unless otherwise noted, assume that each example gets the str variable from:
var str = fs.readFileSync('README.md', 'utf8')
Generates a Table of Contents from a string.
// Generate a TOC
var table = toc(str);
fs.writeFileSync('toc.md', table);
Inject a TOC at the insertion point in a string, <!-- toc -->.
Params:
str: the contentoptions: object of optionstoc.insert(str, options);
<!-- toc -->,dest, (or re-write back to the source file if no dest is passed)toc.add(src, dest, options)
Example:
toc.add('path/to/source.md', 'path/to/dest.md');
Source only:
toc.add('README.md');
Output a "raw" (JSON) Table of Contents object, for customization and usage in templates
toc.raw(str, options);
Returns an object (JSON) with two properties, data and toc:
data: array of headings and associated properties used to construct a TOC. TIP: this can be extended with properties, such as src path etc.toc: the actual Table of Contents result, as a stringExample:
{
// Array of
"data": [
{
"depth": "",
"bullet": "* ",
"heading": "Getting Started",
"url": "getting-started"
},
{
"depth": "",
"bullet": "* ",
"heading": "Usage",
"url": "usage"
}
],
// String. the actual TOC
"toc": "* [Getting Started](#getting-started)\n* [Options](#options)\n* [Contributing](#contributing)\n"
}
See an example.
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint your code using jshint and run tests with mocha -R spec before making a pull request.
Copyright (c) 2014 Jon Schlinkert, contributors Licensed under the MIT license.
FAQs
Generate a markdown TOC (table of contents).
The npm package marked-toc receives a total of 330 weekly downloads. As such, marked-toc popularity was classified as not popular.
We found that marked-toc demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Create and share saved alert views with custom tabs on the org alerts page, making it easier for teams to return to consistent, named filter sets.

Product
Socket’s Rust and Cargo support is now generally available, providing dependency analysis and supply chain visibility for Rust projects.

Security News
Chrome 144 introduces the Temporal API, a modern approach to date and time handling designed to fix long-standing issues with JavaScript’s Date object.