Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add number bullet #177

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ Usage: markdown-toc [options] <input>
--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.)

Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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._
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on September 19, 2017._
7 changes: 5 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
});

Expand All @@ -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.)',
Expand Down
77 changes: 75 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

var utils = require('./lib/utils');
var querystring = require('querystring');
var repeat = require('repeat-string');

/**
* expose `toc`
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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.
Expand Down