markdown-toc
Advanced tools
Comparing version 0.8.1 to 0.9.0
55
index.js
@@ -15,4 +15,5 @@ /*! | ||
var Remarkable = require('remarkable'); | ||
var repeat = require('repeat-string'); | ||
var extend = require('extend-shallow'); | ||
var mdu = require('markdown-utils'); | ||
var mdlink = require('markdown-link'); | ||
var pick = require('object.pick'); | ||
@@ -57,2 +58,3 @@ | ||
var opts = extend({firsth1: true, maxdepth: 6}, options); | ||
var stripFirst = opts.firsth1 === false; | ||
@@ -62,5 +64,5 @@ return function(md) { | ||
tokens = tokens.slice(); | ||
var arr = [], i = 0; | ||
var len = tokens.length; | ||
var len = tokens.length, i = 0, num = 0; | ||
var tocstart = -1; | ||
var arr = [], res = {}; | ||
@@ -75,2 +77,3 @@ while (len--) { | ||
tokens[i].lvl = tokens[i - 1].hLevel; | ||
tokens[i].i = num++; | ||
arr.push(tokens[i]); | ||
@@ -80,4 +83,3 @@ } | ||
var line = []; | ||
var res = {}; | ||
var result = []; | ||
res.json = []; | ||
@@ -87,13 +89,17 @@ | ||
// table of contents. | ||
arr.forEach(function(token) { | ||
if (token.lines[0] > tocstart) { | ||
res.json.push(pick(token, ['content', 'lvl'])); | ||
line.push(linkify(token, opts)); | ||
var alen = arr.length, j = 0; | ||
while (alen--) { | ||
var tok = arr[j++]; | ||
if (tok.lines[0] > tocstart) { | ||
res.json.push(pick(tok, ['content', 'lvl', 'i'])); | ||
result.push(linkify(tok, opts)); | ||
} | ||
}); | ||
} | ||
opts.highest = highest(line); | ||
opts.highest = highest(result); | ||
res.highest = opts.highest; | ||
res.tokens = tokens; | ||
res.content = bullets(line, opts); | ||
if(stripFirst) result = result.slice(1); | ||
res.content = bullets(result, opts); | ||
return res; | ||
@@ -122,3 +128,2 @@ }; | ||
unindent = 1; | ||
arr.shift(); | ||
} | ||
@@ -136,3 +141,3 @@ | ||
res.push(mdu.listitem(ele.content, ele.lvl, opts)); | ||
res.push(listitem(ele.content, ele.lvl, opts)); | ||
if (ele.lvl === opts.maxdepth) { | ||
@@ -146,2 +151,21 @@ break; | ||
/** | ||
* Generate a list item. | ||
*/ | ||
function listitem(str, level, options) { | ||
var opts = options || {}; | ||
var ch = opts.bullets || ['-', '*', '+', '~']; | ||
var lvl = level - opts.highest; | ||
var depth = lvl > 0 | ||
? repeat(' ', lvl) | ||
: ''; | ||
var bullet = ch[(lvl) % ch.length]; | ||
return depth | ||
+ (bullet ? bullet : '*') | ||
+ ' ' + str; | ||
} | ||
/** | ||
* Get the highest heading level in the array, so | ||
@@ -158,3 +182,2 @@ * we can un-indent the proper number of levels. | ||
}); | ||
if (res && res.length) { | ||
@@ -178,3 +201,3 @@ return res[0].lvl; | ||
} | ||
ele.content = mdu.link(text, '#' + slug); | ||
ele.content = mdlink(text, '#' + slug); | ||
} | ||
@@ -181,0 +204,0 @@ return ele; |
@@ -19,6 +19,13 @@ 'use strict'; | ||
module.exports = function insert(str, options) { | ||
var re = /(?:<!-- toc(?:\s*stop)? -->)/g; | ||
options = options || {}; | ||
var regex = options.regex || /(?:<!-- toc(?:\s*stop)? -->)/g; | ||
var open = options.open || '<!-- toc -->\n\n'; | ||
var close = options.close || '<!-- tocstop -->'; | ||
var file; | ||
// does the file have front-matter? | ||
if (/^---/.test(str)) { | ||
// extract it temporarily so the syntax | ||
// doesn't get mistaken for a heading | ||
file = matter(str); | ||
@@ -28,15 +35,19 @@ str = file.content; | ||
var sections = split(str, re); | ||
var sections = split(str, regex); | ||
if (sections.length > 3) { | ||
throw new Error('markdown-toc only supports one Table of Contents per file.'); | ||
} | ||
var last = sections[sections.length - 1]; | ||
if (sections.length === 3) { | ||
sections.splice(1, 1, '<!-- toc -->\n\n' + toc(last, options).content); | ||
sections.splice(2, 0, '<!-- tocstop -->'); | ||
sections.splice(1, 1, open + toc(last, options).content); | ||
sections.splice(2, 0, close); | ||
} | ||
if (sections.length === 2) { | ||
sections.splice(1, 0, '<!-- toc -->\n\n' + toc(last, options).content + '\n\n<!-- tocstop -->'); | ||
sections.splice(1, 0, open + toc(last, options).content + '\n\n' + close); | ||
} | ||
var res = sections.join('\n\n'); | ||
// if front-matter was found, put it back now | ||
if (file) { | ||
@@ -54,2 +65,2 @@ return matter.stringify(res, file.data); | ||
return str.trim(); | ||
} | ||
} |
{ | ||
"name": "markdown-toc", | ||
"description": "Generate a markdown TOC (table of contents) with Remarkable.", | ||
"version": "0.8.1", | ||
"version": "0.9.0", | ||
"homepage": "https://github.com/jonschlinkert/markdown-toc", | ||
@@ -10,7 +10,13 @@ "author": { | ||
}, | ||
"repository": "jonschlinkert/markdown-toc", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/jonschlinkert/markdown-toc.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/jonschlinkert/markdown-toc/issues" | ||
}, | ||
"license": "MIT", | ||
"license": { | ||
"type": "MIT", | ||
"url": "https://github.com/jonschlinkert/markdown-toc/blob/master/LICENSE" | ||
}, | ||
"files": [ | ||
@@ -28,10 +34,10 @@ "index.js", | ||
"dependencies": { | ||
"extend-shallow": "^0.2.0", | ||
"gray-matter": "^1.2.5", | ||
"markdown-utils": "^0.3.0", | ||
"extend-shallow": "^1.1.2", | ||
"gray-matter": "^1.3.0", | ||
"markdown-link": "^0.1.0", | ||
"object.pick": "^1.1.1", | ||
"remarkable": "^1.6.0" | ||
"remarkable": "^1.6.0", | ||
"repeat-string": "^1.5.0" | ||
}, | ||
"devDependencies": { | ||
"helper-related": "^0.1.0", | ||
"mocha": "*", | ||
@@ -53,2 +59,4 @@ "should": "*" | ||
"remarkable", | ||
"remarkableplugin", | ||
"plugin", | ||
"render", | ||
@@ -61,2 +69,2 @@ "renderer", | ||
] | ||
} | ||
} |
@@ -200,2 +200,3 @@ # markdown-toc [![NPM version](https://badge.fury.io/js/markdown-toc.svg)](http://badge.fury.io/js/markdown-toc) [![Build Status](https://travis-ci.org/jonschlinkert/markdown-toc.svg)](https://travis-ci.org/jonschlinkert/markdown-toc) | ||
## License | ||
@@ -207,2 +208,2 @@ Copyright (c) 2014-2015 Jon Schlinkert | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on March 02, 2015._ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on March 14, 2015._ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
14812
2
274
208
0
6
+ Addedmarkdown-link@^0.1.0
+ Addedrepeat-string@^1.5.0
+ Addedmarkdown-link@0.1.1(transitive)
- Removedmarkdown-utils@^0.3.0
- Removedarray-slice@0.2.3(transitive)
- Removedexport-files@0.1.4(transitive)
- Removedextend-shallow@0.2.0(transitive)
- Removedis-number@0.1.1(transitive)
- Removedmarkdown-utils@0.3.0(transitive)
Updatedextend-shallow@^1.1.2
Updatedgray-matter@^1.3.0