From 1845aa9bdf32be19654b0eff3f9d4f976d89869f Mon Sep 17 00:00:00 2001 From: 1nhann Date: Sun, 27 Mar 2022 21:32:00 +0800 Subject: [PATCH 1/7] add number bullet --- README.md | 12 ++++++++- cli.js | 4 ++- index.js | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 88 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b8766c4..a15b50a 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 "*".) - + + --num: 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.num + +Type: `Boolean` + +Default: `false` + +Use something like `1.` 、`2.` 、`3.` for each item in the generated TOC. + ### options.maxdepth Type: `Number` diff --git a/cli.js b/cli.js index 87dd4c0..62c41ac 100755 --- a/cli.js +++ b/cli.js @@ -4,7 +4,7 @@ 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','num'], string: ['append', 'bullets', 'indent'], default: { firsth1: true, @@ -29,6 +29,8 @@ if (args._.length !== 1) { ' --bullets: Bullets to use for items in the generated TOC', ' (Supports multiple bullets: --bullets "*" --bullets "-" --bullets "+")', ' (Default is "*".)', + ' --num: 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..ae04e97 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` @@ -108,7 +109,11 @@ function generate(options) { res.tokens = tokens; if (stripFirst) result = result.slice(1); - res.content = bullets(result, opts); + if(options.num){ + 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.lvl += 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. From 4ceba802fe799b9f89a757a7b6bd2e5d5b49b040 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 27 Mar 2022 22:14:00 +0800 Subject: [PATCH 2/7] do nothing when no heading found --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index ae04e97..3886eaf 100644 --- a/index.js +++ b/index.js @@ -109,7 +109,7 @@ function generate(options) { res.tokens = tokens; if (stripFirst) result = result.slice(1); - if(options.num){ + if(options.num && result.length){ res.content = bullets_num(result); }else{ res.content = bullets(result, opts); From 902a183a108dd6476dc5fc8b32414617417bd9b5 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 27 Mar 2022 22:23:48 +0800 Subject: [PATCH 3/7] add default value for parameter num , fix bug when running with nodejs8 and lower --- cli.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cli.js b/cli.js index 62c41ac..b3e40f4 100755 --- a/cli.js +++ b/cli.js @@ -8,7 +8,8 @@ var args = utils.minimist(process.argv.slice(2), { string: ['append', 'bullets', 'indent'], default: { firsth1: true, - stripHeadingTags: true + stripHeadingTags: true, + num: false } }); From 8cdc74a64708d940e1018ae9f49e1aee0d73ac32 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 27 Mar 2022 22:35:39 +0800 Subject: [PATCH 4/7] change parameter name to number , because num has already been used --- README.md | 2 +- cli.js | 6 +++--- index.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a15b50a..9037dec 100644 --- a/README.md +++ b/README.md @@ -263,7 +263,7 @@ 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.num +### options.number Type: `Boolean` diff --git a/cli.js b/cli.js index b3e40f4..ece3fb6 100755 --- a/cli.js +++ b/cli.js @@ -4,12 +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','num'], + boolean: ['i', 'json', 'firsth1', 'stripHeadingTags','number'], string: ['append', 'bullets', 'indent'], default: { firsth1: true, stripHeadingTags: true, - num: false + number: false } }); @@ -30,7 +30,7 @@ if (args._.length !== 1) { ' --bullets: Bullets to use for items in the generated TOC', ' (Supports multiple bullets: --bullets "*" --bullets "-" --bullets "+")', ' (Default is "*".)', - ' --num: use number bullets , like 1. 2. 3.', + ' --number: use number bullets , like 1. 2. 3.', '', '', ' --maxdepth: Use headings whose depth is at most maxdepth', diff --git a/index.js b/index.js index 3886eaf..0243016 100644 --- a/index.js +++ b/index.js @@ -109,7 +109,7 @@ function generate(options) { res.tokens = tokens; if (stripFirst) result = result.slice(1); - if(options.num && result.length){ + if(options.number && result.length){ res.content = bullets_num(result); }else{ res.content = bullets(result, opts); From 8b92ce0c5d631997f85d6b1b8d0418494fb393ec Mon Sep 17 00:00:00 2001 From: 1nhann Date: Sun, 27 Mar 2022 23:16:28 +0800 Subject: [PATCH 5/7] fix bug --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 0243016..0ed87f6 100644 --- a/index.js +++ b/index.js @@ -47,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; @@ -109,7 +109,7 @@ function generate(options) { res.tokens = tokens; if (stripFirst) result = result.slice(1); - if(options.number && result.length){ + if(opts.number && result.length){ res.content = bullets_num(result); }else{ res.content = bullets(result, opts); From 91c69e2e16c508cc0c2ce7ba4802bed6587cd960 Mon Sep 17 00:00:00 2001 From: 1nhann Date: Mon, 28 Mar 2022 20:27:38 +0800 Subject: [PATCH 6/7] polish README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9037dec..af485a8 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ Usage: markdown-toc [options] (Supports multiple bullets: --bullets "*" --bullets "-" --bullets "+") (Default is "*".) - --num: use number bullets , like 1. 2. 3. + --number: use number bullets , like 1. 2. 3. --maxdepth: Use headings whose depth is at most maxdepth (Default is 6.) @@ -366,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._ From 81b11f7b23d3654f8f5a2b97ccdbf326a4a1c61e Mon Sep 17 00:00:00 2001 From: 1nhann Date: Tue, 29 Mar 2022 00:25:02 +0800 Subject: [PATCH 7/7] fix bug --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 0ed87f6..f579197 100644 --- a/index.js +++ b/index.js @@ -211,7 +211,7 @@ function listitem_num() { parent = parent.parent; } ele.parent = parent.parent; - bullet = parent.lvl += 1; + bullet = parent.bullet += 1; } ele.bullet = bullet;