diff --git a/README.md b/README.md index b8766c4..af485a8 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,9 @@ Usage: markdown-toc [options] --bullets: Bullets to use for items in the generated TOC (Supports multiple bullets: --bullets "*" --bullets "-" --bullets "+") (Default is "*".) - + + --number: use number bullets , like 1. 2. 3. + --maxdepth: Use headings whose depth is at most maxdepth (Default is 6.) @@ -261,6 +263,14 @@ Default: `*` The bullet to use for each item in the generated TOC. If passed as an array (`['*', '-', '+']`), the bullet point strings will be used based on the header depth. +### options.number + +Type: `Boolean` + +Default: `false` + +Use something like `1.` 、`2.` 、`3.` for each item in the generated TOC. + ### options.maxdepth Type: `Number` @@ -356,4 +366,4 @@ Released under the [MIT License](LICENSE). *** -_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on September 19, 2017._ \ No newline at end of file +_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on September 19, 2017._ diff --git a/cli.js b/cli.js index 87dd4c0..ece3fb6 100755 --- a/cli.js +++ b/cli.js @@ -4,11 +4,12 @@ var fs = require('fs'); var toc = require('./index.js'); var utils = require('./lib/utils'); var args = utils.minimist(process.argv.slice(2), { - boolean: ['i', 'json', 'firsth1', 'stripHeadingTags'], + boolean: ['i', 'json', 'firsth1', 'stripHeadingTags','number'], string: ['append', 'bullets', 'indent'], default: { firsth1: true, - stripHeadingTags: true + stripHeadingTags: true, + number: false } }); @@ -29,6 +30,8 @@ if (args._.length !== 1) { ' --bullets: Bullets to use for items in the generated TOC', ' (Supports multiple bullets: --bullets "*" --bullets "-" --bullets "+")', ' (Default is "*".)', + ' --number: use number bullets , like 1. 2. 3.', + '', '', ' --maxdepth: Use headings whose depth is at most maxdepth', ' (Default is 6.)', diff --git a/index.js b/index.js index 90cb8be..f579197 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ var utils = require('./lib/utils'); var querystring = require('querystring'); +var repeat = require('repeat-string'); /** * expose `toc` @@ -46,7 +47,7 @@ toc.insert = require('./lib/insert'); */ function generate(options) { - var opts = utils.merge({firsth1: true, maxdepth: 6}, options); + var opts = utils.merge({firsth1: true, maxdepth: 6, number: false}, options); var stripFirst = opts.firsth1 === false; if (typeof opts.linkify === 'undefined') opts.linkify = true; @@ -108,7 +109,11 @@ function generate(options) { res.tokens = tokens; if (stripFirst) result = result.slice(1); - res.content = bullets(result, opts); + if(opts.number && result.length){ + res.content = bullets_num(result); + }else{ + res.content = bullets(result, opts); + } res.content += (opts.append || ''); return res; }; @@ -159,6 +164,74 @@ function bullets(arr, options) { return res.join('\n'); } + + +/** + * Render markdown list bullets with number , like 1. 2. 3. + * + * @param {Array} `arr` Array of listitem objects + * @return {String} + */ +function bullets_num(arr) { + + var listitem = listitem_num(); + + var len = arr.length; + var res = []; + var i = 0; + var last_ele = arr[0]; + last_ele.parent = null; + last_ele.bullet = 0; + while (i < len) { + var ele = arr[i++]; + res.push(listitem(ele,last_ele)); + last_ele = ele; + } + return res.join('\n'); +} + +/** + * Returns a function to generate a plain-text/markdown list-item with numurous beginning + * Called by `bullets_num()` + * + * @return {String} returns a formatted list item + */ +function listitem_num() { + return function(ele,last_ele) { + var bullet = 1; + if(ele.lvl == last_ele.lvl){ + ele.parent = last_ele.parent; + bullet = last_ele.bullet + 1; + }else if(ele.lvl > last_ele.lvl){ + ele.parent = last_ele; + bullet = 1; + }else if(ele.lvl < last_ele.lvl){ + var parent = last_ele.parent; + for(var i = 1; i < last_ele.lvl - ele.lvl; i += 1){ + parent = parent.parent; + } + ele.parent = parent.parent; + bullet = parent.bullet += 1; + } + ele.bullet = bullet; + + var indent = ' '; + + var prefix = bullet + '. '; + + var res = ''; + res += repeat(indent, ele.lvl); + res += prefix; + res += ele.content; + return res; + }; +} + + + + + + /** * Get the highest heading level in the array, so * we can un-indent the proper number of levels.