Markdown-it Named Headers
A plugin for markdown-it. Makes header elments have identifer attributes.
# Example Header --> <h1 id="example-header">Example</h1>
By default, it uses string.js's slugify to translate header text into a url safe name. You can override this. See Options.
Cribbed heavily from https://github.com/valeriangalliat/markdown-it-anchor
Install
npm install --save-dev markdown-it-named-headers
Usage
Use with plain old node:
var md = mdnh = ; md;
Use as part of a Gulp workflow: (Note: You don't need to require named-headers in your gulpfile. gulp-markdown-it takes care of that for you).
var gulp = md = ;gulp;
Options
Slugify
slugify: my_slug_function
If string.js's slugify doesn't fit your needs, you can simply pass in your own slugify function. Basically, the API is: accept any string, return a string suitable for a name attribute. Example:
{ var output_string = ; return output_string;}
Since we use IDs, we should avoid duplicating them. A second parameter is passed. It is an empty object that will persist across a single call to render. In other words, you can use it to maintain a hash of used_headers per page.
The default slugify method looks something like this:
{ var slug = ; if used_headersslug used_headersslug++; slug += used_headersslug; else used_headersslug += '-' + 1; return slug;}