Generate a table of contents index
Creates a list representing the structure of a document as determined by the headings in the document.
To inject the created list into a location in the document place the marker <!-- @toc -->
in the source document.
By default it creates anchor links (#
) to named anchors on the same page but the link behaviour may be disabled or customized using a destination
function, see the api docs.
npm i mktoc --save
For the command line interface install mkdoc globally (npm i -g mkdoc
).
Create the stream and write a commonmark document:
var toc = require('mktoc')
, ast = require('mkast');
ast.src('# Heading\n\n## Sub Heading\n\n')
.pipe(toc())
.pipe(ast.stringify({indent: 2}))
.pipe(process.stdout);
Create a standalone table of contents:
mkcat README.md | mktoc -s | mkout > TOC.md
Inject the table of contents into a document containing the <!-- @toc -->
marker:
mkcat readme.md | mktoc | mkout > README.md
Set an initial heading with the specified level:
mkcat readme.md | mktoc --title 'Table of Contents' --level 2 | mkout > README.md
Only include headings for levels 2-4:
mkcat readme.md | mktoc -d 2 -m 4 | mkout > README.md
Create an ordered list and set the delimiter:
mkcat readme.md | mktoc -o -E '.' | mkout > README.md
Usage: mktoc [options]
Generates a table of contents index.
Options
-t, --title=[TITLE] Set initial heading
-l, --level=[NUM] Set level for initial heading
-d, --depth=[LEVEL] Ignore headings below LEVEL
-m, --max=[LEVEL] Ignore headings above LEVEL
-p, --prefix=[VAL] Set link destination prefix to VAL
-b, --base=[URL] Base URL for absolute links
-B, --bullet=[CHAR] Character for bullet lists
-E, --delimiter=[CHAR] Delimiter for ordered lists
-D, --disable Disable automatic links
-o, --ordered Create an ordered list
-s, --standalone Standalone index, discards input
-h, --help Display help and exit
--version Print the version and exit
[email protected]
toc([opts][, cb])
Generate a document containing a table of contents list.
See Toc for more available options.
Returns an output stream.
opts
Object processing options.cb
Function callback function.
input
Readable input stream.output
Writable output stream.
new Toc([opts])
Create a table of contents index stream.
Note that in order to build a complete index all data must be read so this implementation buffers incoming nodes and flushes them when the stream is ended writing the index nodes where necessary.
When the first child of a heading is a link it is preserved and no automatic link is created, otherwise when creating links inline markup in the heading is discarded.
If the standalone
option is given then the incoming data is discarded
and the document representing the index is flushed.
When a destination
function is specified it is passed a string
literal of the heading text and should return a URL, the function is
invoked in the scope of this stream.
Typically prefix
will be either a /
, #
or the empty string
depending upon whether you want absolute, anchor or relative links. The
default is to use #
for anchor links on the same page.
If the bullet
option is given it must be one of -
, +
or *
.
If the delimiter
option is given it must a period .
or right
parenthesis )
.
opts
Object processing options.
standalone
Boolean discard incoming data.type
String=bullet list output type,bullet
orordered
.link
Boolean=true whether to create links in the output lists.depth
Number=1 ignore headings below this level.max
Number=6 ignore headings above this level.destination
Function builds the link URLs.prefix
String=# default link prefix.base
String a base path for absolute links.bullet
String=- character for bullet lists.delimiter
String=) delimiter for ordered lists.
MIT
Created by mkdoc on April 18, 2016