├── .gitignore
├── package.json
├── index.js
├── UNLICENSE
├── test
└── all.js
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | npm-debug.log
3 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "markdown-it-named-headers",
3 | "version": "0.0.4",
4 | "description": "Headers have name attributes for markdown-it.",
5 | "keywords": [
6 | "markdown-it-plugin"
7 | ],
8 | "homepage": "https://github.com/leff/markdown-it-named-headers",
9 | "license": {
10 | "type": "Unlicense",
11 | "url": "http://unlicense.org/"
12 | },
13 | "author": {
14 | "name": "Jason Brackins",
15 | "url": ""
16 | },
17 | "files": [
18 | "README.md",
19 | "UNLICENSE",
20 | "index.js"
21 | ],
22 | "repository": {
23 | "type": "git",
24 | "url": "https://github.com/leff/markdown-it-named-headers"
25 | },
26 | "dependencies": {
27 | "string": "^3.0.1"
28 | },
29 | "devDependencies": {
30 | "markdown-it": "^4.0.1",
31 | "mocha": "^2.2.1",
32 | "test": ">=0.0.1"
33 | },
34 | "scripts": {
35 | "test": "node_modules/.bin/mocha test/all.js"
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | (function(){
2 |
3 | var string = require('string');
4 |
5 | var default_slugify = function(s, used_headers) {
6 | var slug = string(s).slugify().toString();
7 | if( used_headers[slug] ) {
8 | used_headers[slug]++;
9 | slug += used_headers[slug];
10 | } else {
11 | used_headers[slug] += '-' + 1;
12 | }
13 | return slug;
14 | };
15 |
16 | var namedheaders = function(md, opts) {
17 | var slugify = (opts && opts.slugify) ? opts.slugify : default_slugify;
18 | var originalHeadingOpen = md.renderer.rules.heading_open;
19 |
20 | md.renderer.rules.heading_open = function (tokens, idx, something, somethingelse, self) {
21 | var used_headers = {};
22 |
23 | tokens[idx].attrs = tokens[idx].attrs || [];
24 |
25 | var title = tokens[idx + 1].children.reduce(function (acc, t) {
26 | return acc + t.content;
27 | }, '');
28 |
29 | var slug = slugify(title, used_headers);
30 | tokens[idx].attrs.push(['id', slug]);
31 |
32 | if (originalHeadingOpen) {
33 | return originalHeadingOpen.apply(this, arguments);
34 | } else {
35 | return self.renderToken.apply(self, arguments);
36 | }
37 | };
38 | };
39 |
40 | module.exports = namedheaders;
41 |
42 | })();
43 |
--------------------------------------------------------------------------------
/UNLICENSE:
--------------------------------------------------------------------------------
1 | This is free and unencumbered software released into the public domain.
2 |
3 | Anyone is free to copy, modify, publish, use, compile, sell, or
4 | distribute this software, either in source code form or as a compiled
5 | binary, for any purpose, commercial or non-commercial, and by any
6 | means.
7 |
8 | In jurisdictions that recognize copyright laws, the author or authors
9 | of this software dedicate any and all copyright interest in the
10 | software to the public domain. We make this dedication for the benefit
11 | of the public at large and to the detriment of our heirs and
12 | successors. We intend this dedication to be an overt act of
13 | relinquishment in perpetuity of all present and future rights to this
14 | software under copyright law.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | OTHER DEALINGS IN THE SOFTWARE.
23 |
24 | For more information, please refer to