markdown-toc
Advanced tools
Comparing version 0.7.2 to 0.8.0
93
index.js
@@ -60,3 +60,3 @@ /*! | ||
tokens = tokens.slice(); | ||
var res = [], i = 0; | ||
var arr = [], i = 0; | ||
var len = tokens.length; | ||
@@ -73,25 +73,24 @@ var tocstart = -1; | ||
tokens[i].lvl = tokens[i - 1].hLevel; | ||
res.push(tokens[i]); | ||
arr.push(tokens[i]); | ||
} | ||
} | ||
var results = []; | ||
var json = []; | ||
var line = []; | ||
var res = {}; | ||
res.json = []; | ||
// exclude headings that come before the actual | ||
// table of contents. | ||
res.forEach(function(token) { | ||
arr.forEach(function(token) { | ||
if (token.lines[0] > tocstart) { | ||
json.push(pick(token, ['content', 'lvl'])); | ||
results.push(linkify(token, opts)); | ||
res.json.push(pick(token, ['content', 'lvl'])); | ||
line.push(linkify(token, opts)); | ||
} | ||
}); | ||
opts.highest = highest(results); | ||
return { | ||
tokens: tokens, | ||
highest: opts.highest, | ||
content: bullets(results, opts), | ||
json: json | ||
}; | ||
opts.highest = highest(line); | ||
res.highest = opts.highest; | ||
res.tokens = tokens; | ||
res.content = bullets(line, opts); | ||
return res; | ||
}; | ||
@@ -112,2 +111,6 @@ }; | ||
var fn = typeof opts.filter === 'function' | ||
? opts.filter | ||
: null; | ||
// Keep the first h1? This is `true` by default | ||
@@ -127,2 +130,4 @@ if(opts && opts.firsth1 === false) { | ||
if (fn && !fn(ele.content, ele, arr)) { continue; } | ||
res.push(mdu.listitem(ele.content, ele.lvl, opts)); | ||
@@ -133,3 +138,2 @@ if (ele.lvl === opts.maxdepth) { | ||
} | ||
return res.join('\n'); | ||
@@ -147,5 +151,10 @@ } | ||
function highest(arr) { | ||
return arr.slice().sort(function(a, b) { | ||
var res = arr.slice().sort(function(a, b) { | ||
return a.lvl - b.lvl; | ||
})[0].lvl; | ||
}); | ||
if (res && res.length) { | ||
return res[0].lvl; | ||
} | ||
return 0; | ||
} | ||
@@ -158,10 +167,11 @@ | ||
function linkify(ele, opts) { | ||
var slug = slugify(ele.content, opts); | ||
var text = strip(ele.content, opts); | ||
if (ele && ele.content) { | ||
var text = titleize(ele.content, opts); | ||
var slug = slugify(ele.content, opts); | ||
if (opts && typeof opts.linkify === 'function') { | ||
return opts.linkify(ele, slug, opts); | ||
if (opts && typeof opts.linkify === 'function') { | ||
return opts.linkify(ele, slug, opts); | ||
} | ||
ele.content = mdu.link(text, '#' + slug); | ||
} | ||
ele.content = mdu.link(text, '#' + slug); | ||
return ele; | ||
@@ -171,7 +181,9 @@ } | ||
/** | ||
* Slugify links. | ||
* Slugify the url part of a markdown link. | ||
* | ||
* @name options.slugify | ||
* @param {String} `str` The string to slugify | ||
* @param {Object} `opts` Pass a custom slugify function on `slugify` | ||
* @return {String} | ||
* @api public | ||
*/ | ||
@@ -188,4 +200,24 @@ | ||
/** | ||
* Titleize the title part of a markdown link. | ||
* | ||
* @name options.titleize | ||
* @param {String} `str` The string to titleize | ||
* @param {Object} `opts` Pass a custom titleize function on `titleize` | ||
* @return {String} | ||
* @api public | ||
*/ | ||
function titleize(str, opts) { | ||
if (opts && opts.strip) { return strip(str, opts); } | ||
if (opts && opts.titleize === false) return str; | ||
if (opts && typeof opts.titleize === 'function') { | ||
return opts.titleize(str, opts); | ||
} | ||
return str; | ||
} | ||
/** | ||
* Optionally strip specified words from headings. | ||
* | ||
* @name options.strip | ||
* @param {String} `str` | ||
@@ -198,3 +230,2 @@ * @param {String} `opts` | ||
opts = opts || {}; | ||
if (!opts.strip) return str; | ||
@@ -205,6 +236,9 @@ if (typeof opts.strip === 'function') { | ||
var res = opts.strip.join('|'); | ||
var re = new RegExp(res, 'g'); | ||
str = str.trim().replace(re, ''); | ||
return str.replace(/^-|-$/g, ''); | ||
if (Array.isArray(opts.strip) && opts.strip.length) { | ||
var res = opts.strip.join('|'); | ||
var re = new RegExp(res, 'g'); | ||
str = str.trim().replace(re, ''); | ||
return str.replace(/^-|-$/g, ''); | ||
} | ||
return str; | ||
} | ||
@@ -219,2 +253,3 @@ | ||
toc.slugify = slugify; | ||
toc.titleize = titleize; | ||
toc.strip = strip; |
{ | ||
"name": "markdown-toc", | ||
"description": "Generate a markdown TOC (table of contents) with Remarkable.", | ||
"version": "0.7.2", | ||
"version": "0.8.0", | ||
"homepage": "https://github.com/jonschlinkert/markdown-toc", | ||
@@ -28,3 +28,3 @@ "author": { | ||
"extend-shallow": "^0.2.0", | ||
"gray-matter": "^1.2.4", | ||
"gray-matter": "^1.2.5", | ||
"markdown-utils": "^0.3.0", | ||
@@ -58,2 +58,2 @@ "object.pick": "^1.1.1", | ||
] | ||
} | ||
} |
@@ -5,2 +5,9 @@ # markdown-toc [![NPM version](https://badge.fury.io/js/markdown-toc.svg)](http://badge.fury.io/js/markdown-toc) | ||
- Won't mangle markdown in code examples (like headings in gfm fenced code blocks that other TOC generators mistake as being real headings) | ||
- Uses sane defaults, so no customization is necessary, but you can if you need to. | ||
- Get JSON to generate your own TOC from whatever templates you want to use | ||
- [filter](#filter-headings) out headings you don't want | ||
- [Improve](#titleize) the headings you do want | ||
## Install with [npm](npmjs.org) | ||
@@ -30,2 +37,4 @@ | ||
## API | ||
### toc.json | ||
@@ -105,6 +114,30 @@ | ||
## Options | ||
### options.filter | ||
Type: `Function` | ||
Default: `undefined` | ||
From time to time, we might get junk like this in our TOC. | ||
```markdown | ||
[.aaa([foo], ...) another bad heading](#-aaa--foo--------another-bad-heading) | ||
``` | ||
Unless you like that kind of thing, you might want to filter these bad headings out | ||
```js | ||
function removeYunk(str, ele, arr) { | ||
return str.indexOf('...') === -1; | ||
} | ||
var result = toc(str, {filter: removeYunk}); | ||
//=> beautiful TOC, sans "Yunk"? | ||
// wtf is "Yunk" anyway? Regardless, | ||
// it will be gone. | ||
``` | ||
### options.bullet | ||
@@ -162,2 +195,2 @@ | ||
_This file was generated by [verb](https://github.com/assemble/verb) on February 20, 2015._ | ||
_This file was generated by [verb](https://github.com/assemble/verb) on February 24, 2015._ |
12965
244
193
Updatedgray-matter@^1.2.5