Skip to content

Commit

Permalink
Adds --entryprefix flag to use '*' instead of '-'
Browse files Browse the repository at this point in the history
Fixes #108.

Allows for user-defined prefix, for example

    doctoc --entryprefix='*'
    doctoc --entryprefix='1.'
    doctoc --entryprefix='- [ ]'

This allows flexibility with respect to linters as well as using
numbered or check lists in the Table of Contents.
  • Loading branch information
sheriferson authored and jez committed Jun 13, 2016
1 parent a2476f1 commit ec0157b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 10 deletions.
13 changes: 7 additions & 6 deletions doctoc.js
Expand Up @@ -16,13 +16,13 @@ function cleanPath(path) {
return homeExpanded.replace(/\s/g, '\\ ');
}

function transformAndSave(files, mode, maxHeaderLevel, title, notitle) {
function transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPrefix) {
console.log('\n==================\n');

var transformed = files
.map(function (x) {
var content = fs.readFileSync(x.path, 'utf8')
, result = transform(content, mode, maxHeaderLevel, title, notitle);
, result = transform(content, mode, maxHeaderLevel, title, notitle, entryPrefix);
result.path = x.path;
return result;
});
Expand All @@ -43,7 +43,7 @@ function printUsageAndExit(isErr) {

var outputFunc = isErr ? console.error : console.info;

outputFunc('Usage: doctoc [mode] [--notitle | --title title] [--maxlevel level] <path> (where path is some path to a directory (e.g., .) or a file (e.g., README.md))');
outputFunc('Usage: doctoc [mode] [--entryprefix prefix] [--notitle | --title title] [--maxlevel level] <path> (where path is some path to a directory (e.g., .) or a file (e.g., README.md))');
outputFunc('\nAvailable modes are:');
for (var key in modes) {
outputFunc(' --%s\t%s', key, modes[key]);
Expand All @@ -64,8 +64,8 @@ var modes = {
var mode = modes['github'];

var argv = minimist(process.argv.slice(2)
, { boolean: [ 'h', 'help', 'T', 'notitle' ].concat(Object.keys(modes))
, string: [ 'title', 't', 'maxlevel', 'm' ]
, { boolean: [ 'h', 'help', 'T', 'notitle'].concat(Object.keys(modes))
, string: [ 'title', 't', 'maxlevel', 'm', 'entryprefix' ]
, unknown: function(a) { return (a[0] == '-' ? (console.error('Unknown option(s): ' + a), printUsageAndExit(true)) : true); }
});

Expand All @@ -81,6 +81,7 @@ for (var key in modes) {

var title = argv.t || argv.title;
var notitle = argv.T || argv.notitle;
var entryPrefix = argv.entryprefix || '-';

var maxHeaderLevel = argv.m || argv.maxlevel;
if (maxHeaderLevel && isNaN(maxHeaderLevel) || maxHeaderLevel < 0) { console.error('Max. heading level specified is not a positive number: ' + maxHeaderLevel), printUsageAndExit(true); }
Expand All @@ -97,7 +98,7 @@ for (var i = 0; i < argv._.length; i++) {
files = [{ path: target }];
}

transformAndSave(files, mode, maxHeaderLevel, title, notitle);
transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPrefix);

console.log('\nEverything is OK.');
}
6 changes: 4 additions & 2 deletions lib/transform.js
Expand Up @@ -106,8 +106,10 @@ function determineTitle(title, notitle, lines, info) {
return info.hasStart ? lines[info.startIdx + 2] : defaultTitle;
}

exports = module.exports = function transform(content, mode, maxHeaderLevel, title, notitle) {
exports = module.exports = function transform(content, mode, maxHeaderLevel, title, notitle, entryPrefix) {
mode = mode || 'github.com';
entryPrefix = entryPrefix || '-';

// only limit *HTML* headings by default
var maxHeaderLevelHtml = maxHeaderLevel || 4;

Expand Down Expand Up @@ -143,7 +145,7 @@ exports = module.exports = function transform(content, mode, maxHeaderLevel, tit
var indent = _(_.range(x.rank - lowestRank))
.reduce(function (acc, x) { return acc + indentation; }, '');

return indent + '- ' + x.anchor;
return indent + entryPrefix + ' ' + x.anchor;
})
.join('\n')
+ '\n';
Expand Down
77 changes: 75 additions & 2 deletions test/transform.js
Expand Up @@ -8,9 +8,9 @@ function inspect(obj, depth) {
console.log(require('util').inspect(obj, false, depth || 5, true));
}

function check(md, anchors, mode, maxHeaderLevel, title) {
function check(md, anchors, mode, maxHeaderLevel, title, notitle, entryPrefix) {
test('transforming', function (t) {
var res = transform(md, mode, maxHeaderLevel, title)
var res = transform(md, mode, maxHeaderLevel, title, notitle, entryPrefix)

// remove wrapper
var data = res.data.split('\n');
Expand Down Expand Up @@ -327,3 +327,76 @@ check(

, 'gitlab.com'
)

// check the --entryprefix flag
check(
[ '# My Module'
, 'Some text here'
, '## API'
, '### Method One'
, 'works like this'
, '### Method Two'
, '#### Main Usage'
, 'some main usage here'
].join('\n')
, [ '**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*\n\n'
, '* [My Module](#my-module)\n'
, ' * [API](#api)\n'
, ' * [Method One](#method-one)\n'
, ' * [Method Two](#method-two)\n'
, ' * [Main Usage](#main-usage)\n\n\n'
].join('')
, undefined
, undefined
, undefined
, undefined
, '*' // pass '*' as the prefix for toc entries
)

check(
[ '# My Module'
, 'Some text here'
, '## API'
, '### Method One'
, 'works like this'
, '### Method Two'
, '#### Main Usage'
, 'some main usage here'
].join('\n')
, [ '**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*\n\n'
, '>> [My Module](#my-module)\n'
, ' >> [API](#api)\n'
, ' >> [Method One](#method-one)\n'
, ' >> [Method Two](#method-two)\n'
, ' >> [Main Usage](#main-usage)\n\n\n'
].join('')
, undefined
, undefined
, undefined
, undefined
, '>>' // pass '>>' as the prefix for toc entries)
)

check(
[ '# My Module'
, 'Some text here'
, '## API'
, '### Method One'
, 'works like this'
, '### Method Two'
, '#### Main Usage'
, 'some main usage here'
].join('\n')
, [ '**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*\n\n'
, '1. [My Module](#my-module)\n'
, ' 1. [API](#api)\n'
, ' 1. [Method One](#method-one)\n'
, ' 1. [Method Two](#method-two)\n'
, ' 1. [Main Usage](#main-usage)\n\n\n'
].join('')
, undefined
, undefined
, undefined
, undefined
, '1.' // pass '1.' as the prefix for toc entries
)

0 comments on commit ec0157b

Please sign in to comment.