Generates a markdown TOC (table of contents).
The tool can be used for English and Russian languages.
$ npm install toc-md
Add an HTML comment <!-- TOC -->
to a markdown file.
A TOC will be generated exactly on this place for the following headers.
var toc = require('toc-md');
@param {String} - a source where to insert a TOC (must contain the HTML comment <!-- TOC -->
)
@param {Object} - options:
- maxDepth: Number - makes
toc-md
use headings whose depth is at most the specified value (default:6
)
@param {Function} - callback
@param {String} - a source whereof to clean a TOC
@param {Function} - callback
var fs = require('fs'),
toc = require('toc-md');
var source = fs.readFileSync('markdown-without-toc.md', 'utf-8');
var options = {
maxDepth: 6
};
toc.insert(source, options, function (err, res) {
if (err) {
console.log(err);
} else {
fs.writeFileSync('markdown-with-toc.md', res);
}
});
source = fs.readFileSync('markdown-with-toc.md', 'utf-8');
toc.clean(source, function (err, res) {
if (err) {
console.log(err);
} else {
fs.writeFileSync('markdown-without-toc.md', res);
}
});
$ toc-md --help
Generates a markdown TOC (table of contents)
Usage:
toc-md [OPTIONS] [ARGS]
Options:
-h, --help : Help
-v, --version : Shows the version number
-m MAXDEPTH, --max-depth=MAXDEPTH : Uses headings whose depth is at most the specified value (default: 6)
-c, --clean : Cleans a TOC
Arguments:
SOURCE : Path to an input markdown file (it must contain the HTML comment <!-- TOC -->) (required)
TARGET : Path to an output markdown file
If argument TARGET
is not specified, a result will be written to SOURCE
.
$ toc-md path/to/input/markdown path/to/output/markdown --max-depth=4
$ toc-md path/to/markdown -m 4
$ toc-md path/to/input/markdown path/to/output/markdown --clean
$ toc-md path/to/markdown -c