Skip to content

Commit

Permalink
Add option 'bullet'
Browse files Browse the repository at this point in the history
  • Loading branch information
eGavr committed Mar 9, 2015
1 parent 4cf52ac commit 9de1614
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
11 changes: 9 additions & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var path = require('path'),
_ = require('lodash'),
vfs = require('vow-fs'),
promisify = require('vow-node').promisify,
toc = require('./index'),
tocInsert = promisify(toc.insert),
tocClean = promisify(toc.clean),
defaults = require('./defaults'),
log = require('./log'),
TOC_COMMENT = '<!-- TOC -->';

Expand Down Expand Up @@ -37,6 +37,13 @@ module.exports = require('coa').Cmd()
return parseInt(val);
})
.end()
.opt()
.name('bullet')
.title('The bullet (\'*\', \'-\', \'+\') to use for each element in the generated TOC (defaults: \'-\')')
.long('bullet')
.short('b')
.def('-')
.end()
.opt()
.name('clean')
.title('Cleans a TOC')
Expand All @@ -61,7 +68,7 @@ module.exports = require('coa').Cmd()

if (source.indexOf(TOC_COMMENT) === -1) return 'NO TOC COMMENT';

var options = defaults({ maxDepth: opts.maxDepth });
var options = _.pick(opts, ['maxDepth', 'bullet']);
return tocInsert(source, options);
})
.then(function (res) {
Expand Down
11 changes: 9 additions & 2 deletions lib/defaults.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
var _ = require('lodash');
var _ = require('lodash'),
bullets = ['-', '*', '+'];

/**
* Sets options
* @param {Object} [options]
* @param {Number} [options.maxDepth]
* @param {Char} [options.bullet]
* returns {Object}
*/
module.exports = function (options) {
return _.defaults(options || {}, {
options = options || {};
if (bullets.indexOf(options.bullet) === -1) {
options.bullet = '-';
}

return _.defaults(options, {
maxDepth: 6
});
};
1 change: 1 addition & 0 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var marked = require('marked'),
* @param {String} source
* @param {Object} [options]
* @param {Number} [options.maxDepth]
* @param {Char} [options.bullet]
* @returns {TOC}
*/
module.exports = function (source, options) {
Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ module.exports = {
* @param {String} source
* @param {Object} [opts]
* @param {Number} [opts.maxDepth]
* @param {Char} [opts.bullet]
* @param {Function} cb
*/
insert: function (source, opts, cb) {
var callback,
options;

if (arguments.length === 2) {
options = {};
options = defaults();
callback = opts;
} else {
options = defaults(opts);
Expand Down
7 changes: 5 additions & 2 deletions lib/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = inherit({
* @param {String} source
* @param {Object} [options]
* @param {Number} [options.maxDepth]
* @param {Char} [options.bullet]
*/
__constructor: function (source, options) {
this.index = this._geIndex(source);
Expand All @@ -31,7 +32,9 @@ module.exports = inherit({
* @public
*/
addTocElem: function (header, prevToken) {
if (header.depth > this.options.maxDepth) return;
var options = this.options;

if (header.depth > options.maxDepth) return;

var headerText = utils.getHeader(header.text, prevToken);

Expand All @@ -45,7 +48,7 @@ module.exports = inherit({
indent: indent
});

this.data += indent + '- [' + headerText.replace(/\\/g, '\\\\') + '](#' + href + ')' + EOL;
this.data += indent + options.bullet + ' [' + headerText.replace(/\\/g, '\\\\') + '](#' + href + ')' + EOL;
},

/**
Expand Down

0 comments on commit 9de1614

Please sign in to comment.