|
| 1 | +# mdast-util-toc [![Build Status][build-badge]][build-status] [![Coverage Status][coverage-badge]][coverage-status] |
1 | 2 |
|
| 3 | +Generate a Table of Contents from a [**mdast**][mdast] tree. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +[npm][]: |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install remark-toc |
| 11 | +``` |
| 12 | + |
| 13 | +**mdast-util-toc** is also available as an AMD, CommonJS, and globals |
| 14 | +module, [uncompressed and compressed][releases]. |
| 15 | + |
| 16 | +Dependencies: |
| 17 | + |
| 18 | +```javascript |
| 19 | +var remark = require('remark'); |
| 20 | +var toc = require('mdast-util-toc'); |
| 21 | +``` |
| 22 | + |
| 23 | +Transform: |
| 24 | + |
| 25 | +```javascript |
| 26 | +var file = remark().parse(([ |
| 27 | + '# Alpha', |
| 28 | + '', |
| 29 | + '## Bravo', |
| 30 | + '', |
| 31 | + '### Charlie', |
| 32 | + '', |
| 33 | + '## Delta', |
| 34 | + '' |
| 35 | +].join('\n')); |
| 36 | +``` |
| 37 | +
|
| 38 | +Yields: |
| 39 | +
|
| 40 | +```markdown |
| 41 | +- [Alpha] |
| 42 | + |
| 43 | + - [Bravo](#bravo) |
| 44 | + |
| 45 | + - [Charlie](#charlie) |
| 46 | + |
| 47 | + - [Delta](#delta) |
| 48 | +``` |
| 49 | +
|
| 50 | +## API |
| 51 | +
|
| 52 | +### `toc(node[, options])` |
| 53 | +
|
| 54 | +Generate a Table of Contents from a Markdown document. |
| 55 | +
|
| 56 | +* Looks for the first heading containing `"Table of Contents"`, `"toc"`, |
| 57 | + or `table-of-contents` (case insensitive, supports alt/title attributes |
| 58 | + for links and images too); |
| 59 | +
|
| 60 | +* Removes all following contents until an equal or higher heading is found; |
| 61 | +
|
| 62 | +* Inserts a list representation of the hierarchy of following headings; |
| 63 | +
|
| 64 | +* Adds links to following headings, using the same slugs as GitHub. |
| 65 | +
|
| 66 | +#### `options` |
| 67 | +
|
| 68 | +* `heading` (`string?`) |
| 69 | + — Heading to look for, wrapped in `new RegExp('^(' + value + ')$', 'i');` |
| 70 | +
|
| 71 | +* `maxDepth` (`number?`, default: `6`) |
| 72 | + — Maximum heading depth to include in the table of contents, |
| 73 | + This is inclusive, thus, when set to `3`, level three headings, |
| 74 | + are included (those with three hashes, `###`); |
| 75 | +
|
| 76 | +* `tight` (`boolean?`, default: `false`) |
| 77 | + — Whether to compile list-items tightly. |
| 78 | +
|
| 79 | +## License |
| 80 | +
|
| 81 | +[MIT][license] © [Jonathan Haines][author] |
| 82 | +
|
| 83 | +<!-- Definitions --> |
| 84 | +
|
| 85 | +[build-badge]: https://img.shields.io/travis/BarryThePenguin/mdast-util-toc.svg |
| 86 | +
|
| 87 | +[build-status]: https://travis-ci.org/BarryThePenguin/mdast-util-toc |
| 88 | +
|
| 89 | +[coverage-badge]: https://img.shields.io/codecov/c/github/BarryThePenguin/mdast-util-toc.svg |
| 90 | +
|
| 91 | +[coverage-status]: https://codecov.io/github/BarryThePenguin/mdast-util-toc |
| 92 | +
|
| 93 | +[releases]: https://github.com/BarryThePenguin/mdast-util-toc/releases |
| 94 | +
|
| 95 | +[license]: LICENSE |
| 96 | +
|
| 97 | +[author]: http://barrythepenguin.github.io |
| 98 | +
|
| 99 | +[npm]: https://docs.npmjs.com/cli/install |
| 100 | +
|
| 101 | +[mdast]: https://github.com/wooorm/mdast |
0 commit comments