
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
template-toc
Advanced tools
Middleware for generating a markdown-formatted table of contents with Template, or Template-based applications
Middleware for generating a markdown-formatted table of contents with Template, or Template-based applications
(TOC generated by verb)
Install with npm
$ npm i template-toc --save
var toc = require('template-toc');
Register with templates, verb, assemble or any other application based on templates
2. Add a TOC marker
Add the following to any markdown file that should get a Table of Contents:
<!-- toc -->
Usage with template.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.
templates.preRender(/\.md$/, toc(template));
This project's verbfile 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.
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.
templates.render('README.md', function(err, content) {
console.log(content);
});
Visit templates for documentation.
Usage with verb:
var verb = require('verb'); // or assemble
var app = verb();
app.preRender(/\.md$/, toc(app));
// or
app.postRender(/\.md$/, toc(app));
Visit verb for documentation.
Usage with assemble:
var assemble = require('assemble');
var app = assemble();
app.preRender(/\.md$/, toc(app));
// or
app.postRender(/\.md$/, toc(app));
Visit assemble for documentation.
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.
<!-- toc -->
Install dev dependencies:
$ npm i -d && npm test
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Jon Schlinkert
Copyright © 2015 Jon Schlinkert Released under the MIT license.
This file was generated by verb on December 15, 2015.
FAQs
Middleware for generating a markdown-formatted table of contents with Template, or Template-based applications
We found that template-toc demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.