# template-toc

> Middleware for generating a markdown-formatted table of contents with Template, or Template-based applications

Latest version **0.6.2** (published 2015-12-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install template-toc
pnpm add template-toc
yarn add template-toc
bun add template-toc
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.2 |
| Published | 2015-12-16 |
| First published | 2015-03-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.10.0 |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Jon Schlinkert |
| Maintainers | jonschlinkert |
| Keywords | contents, docs, documentation, generate, insert, markdown, md, readme, table, template, toc |

## Links

- npm: https://www.npmjs.com/package/template-toc
- Repository: https://github.com/jonschlinkert/template-toc
- Issues: https://github.com/jonschlinkert/template-toc/issues
- npm.io page: https://npm.io/package/template-toc

## Dependencies (3)

- [is-primitive](https://npm.io/package/is-primitive.md) ^2.0.0
- [markdown-toc](https://npm.io/package/markdown-toc.md) ^0.12.2
- [extend-shallow](https://npm.io/package/extend-shallow.md) ^2.0.1

## Alternatives

- [@mdxeditor/editor](https://npm.io/package/@mdxeditor/editor.md) — 962.4K weekly downloads
- [mmdb-lib](https://npm.io/package/mmdb-lib.md) — 680.9K weekly downloads
- [playcanvas](https://npm.io/package/playcanvas.md) — 36.2K weekly downloads
- [@glw907/cairn-cms](https://npm.io/package/@glw907/cairn-cms.md) — 967 weekly downloads
- [markdown-to-confluence](https://npm.io/package/markdown-to-confluence.md) — 103 weekly downloads

## Recent versions

- 0.6.2 (latest) — 2015-12-16
- 0.6.1 — 2015-12-15
- 0.6.0 — 2015-12-15
- 0.5.1 — 2015-12-15
- 0.5.0 — 2015-12-11
- 0.4.0 — 2015-10-21
- 0.3.3 — 2015-04-23
- 0.3.2 — 2015-03-25
- 0.3.1 — 2015-03-22
- 0.3.0 — 2015-03-21
- 0.2.1 — 2015-03-21
- 0.2.0 — 2015-03-19
- 0.1.1 — 2015-03-15
- 0.1.0 — 2015-03-15

## README

# template-toc [![NPM version](https://img.shields.io/npm/v/template-toc.svg)](https://www.npmjs.com/package/template-toc) [![Build Status](https://img.shields.io/travis/jonschlinkert/template-toc.svg)](https://travis-ci.org/jonschlinkert/template-toc)

> Middleware for generating a markdown-formatted table of contents with Template, or Template-based applications

<!-- toc -->

* [Install](#install)
* [Usage](#usage)
  - [Two Steps](#two-steps)
  - [Templates.js](#templatesjs)
  - [Verb](#verb)
  - [Assemble](#assemble)
* [Pro-tip](#pro-tip)
* [Related projects](#related-projects)
* [Running tests](#running-tests)
* [Contributing](#contributing)
* [Author](#author)
* [License](#license)

_(TOC generated by [verb](https://github.com/verbose/verb))_

<!-- tocstop -->

## Install

Install with [npm](https://www.npmjs.com/)

```sh
$ npm i template-toc --save
```

## Usage

```js
var toc = require('template-toc');
```

### Two Steps

**1. [Register the middleware](#usage-with-template)**

Register with [templates](https://github.com/jonschlinkert/templates), [verb](https://github.com/verbose/verb), [assemble](http://assemble.io) or any other application based on [templates](https://github.com/jonschlinkert/templates)

**2. Add a TOC marker**

Add the following to any markdown file that should get a Table of Contents:

```html
<!-- toc -->
```

### Templates.js

Usage with [template.js](https://github.com/jonschlinkert/templates):

```js
var Templates = require('templates');
var templates = new Templates();
```

**Pre-render middleware**

If you want the TOC to be generated before layouts are used or partials are injected into pages, use the `.preRender()` method.

```js
templates.preRender(/\.md$/, toc(template));
```

This project's [verbfile](./.verb.md) is a good example. Verb uses this middleware with `.preRender()` so that the generated TOC only includes "top-level" headings that are in the template, and none of the headings from partials are includes.

**Post-render middleware**

If you want the TOC to be generated **after** layouts are used or partials are injected into pages, use the `.postRender()` method.

```js
templates.postRender(/\.md$/, toc(template));
```

**Rendering templates**

Once the middlware is setup, any templates with a `.md` extension and a `<!-- toc -->` comment will have a TOC injected.

```js
templates.render('README.md', function(err, content) {
  console.log(content);
});
```

Visit [templates](https://github.com/jonschlinkert/templates) for documentation.

### Verb

Usage with [verb](https://github.com/verbose/verb):

```js
var verb = require('verb'); // or assemble
var app = verb();

app.preRender(/\.md$/, toc(app));
// or 
app.postRender(/\.md$/, toc(app));
```

Visit [verb](https://github.com/verbose/verb) for documentation.

### Assemble

Usage with [assemble](http://assemble.io):

```js
var assemble = require('assemble'); 
var app = assemble();

app.preRender(/\.md$/, toc(app));
// or 
app.postRender(/\.md$/, toc(app));
```

Visit [assemble](http://assemble.io) for documentation.

## Pro-tip

**Escaping**

If, for some reason, you need to use the `<!- toc -->` comment in documentation and you do not want it to be rendered, just add an extra `!` after the first angle bracket, and the extra `!` will be removed but the TOC will not be rendered.

```html
<!-- toc -->
```

## Related projects

* [markdown-toc](https://www.npmjs.com/package/markdown-toc): Generate a markdown TOC (table of contents) with Remarkable. | [homepage](https://github.com/jonschlinkert/markdown-toc)
* [markdown-utils](https://www.npmjs.com/package/markdown-utils): Micro-utils for creating markdown snippets. | [homepage](https://github.com/jonschlinkert/markdown-utils)
* [remarkable](https://www.npmjs.com/package/remarkable): Markdown parser, done right. 100% Commonmark support, extensions, syntax plugins, high speed - all in… [more](https://www.npmjs.com/package/remarkable) | [homepage](https://github.com/jonschlinkert/remarkable)
* [templates](https://www.npmjs.com/package/templates): System for creating and managing template collections, and rendering templates with any node.js template engine.… [more](https://www.npmjs.com/package/templates) | [homepage](https://github.com/jonschlinkert/templates)
* [verb](https://www.npmjs.com/package/verb): Documentation generator for GitHub projects. Verb is extremely powerful, easy to use, and is used… [more](https://www.npmjs.com/package/verb) | [homepage](https://github.com/verbose/verb)

## Running tests

Install dev dependencies:

```sh
$ npm i -d && npm test
```

## Contributing

Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/template-toc/issues/new).

## Author

**Jon Schlinkert**

* [github/jonschlinkert](https://github.com/jonschlinkert)
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert)

## License

Copyright © 2015 [Jon Schlinkert](https://github.com/jonschlinkert)
Released under the MIT license.

***

_This file was generated by [verb](https://github.com/verbose/verb) on December 15, 2015._

---
_Source: https://npm.io/package/template-toc · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
