Skip to content

Commit

Permalink
add --all flag (#186)
Browse files Browse the repository at this point in the history
Supersedes #183
  • Loading branch information
AndrewSouthpaw committed Oct 13, 2020
2 parents de06803 + 751f031 commit f146558
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 11 deletions.
15 changes: 10 additions & 5 deletions doctoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ function cleanPath(path) {
return homeExpanded.replace(/\s/g, '\\ ');
}

function transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPrefix, stdOut) {
function transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll, stdOut) {
if (processAll) {
console.log('--all flag is enabled. Including headers before the TOC location.')
}

console.log('\n==================\n');

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

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

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('Usage: doctoc [mode] [--entryprefix prefix] [--notitle | --title title] [--maxlevel level] [--all] <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 @@ -75,7 +79,7 @@ var modes = {
var mode = modes['github'];

var argv = minimist(process.argv.slice(2)
, { boolean: [ 'h', 'help', 'T', 'notitle', 's', 'stdout'].concat(Object.keys(modes))
, { boolean: [ 'h', 'help', 'T', 'notitle', 's', 'stdout', 'all' ].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 @@ -93,6 +97,7 @@ for (var key in modes) {
var title = argv.t || argv.title;
var notitle = argv.T || argv.notitle;
var entryPrefix = argv.entryprefix || '-';
var processAll = argv.all;
var stdOut = argv.s || argv.stdout

var maxHeaderLevel = argv.m || argv.maxlevel;
Expand All @@ -110,7 +115,7 @@ for (var i = 0; i < argv._.length; i++) {
files = [{ path: target }];
}

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

console.log('\nEverything is OK.');
}
Expand Down
8 changes: 4 additions & 4 deletions lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ function countHeaders (headers) {
return headers;
}

function getLinesToToc (lines, currentToc, info) {
if (!currentToc) return lines;
function getLinesToToc (lines, currentToc, info, processAll) {
if (processAll || !currentToc) return lines;

var tocableStart = 0;

Expand All @@ -106,7 +106,7 @@ function determineTitle(title, notitle, lines, info) {
return info.hasStart ? lines[info.startIdx + 2] : defaultTitle;
}

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

Expand All @@ -119,7 +119,7 @@ exports = module.exports = function transform(content, mode, maxHeaderLevel, tit
var inferredTitle = determineTitle(title, notitle, lines, info);

var currentToc = info.hasStart && lines.slice(info.startIdx, info.endIdx + 1).join('\n')
, linesToToc = getLinesToToc(lines, currentToc, info);
, linesToToc = getLinesToToc(lines, currentToc, info, processAll);

var headers = getMarkdownHeaders(linesToToc, maxHeaderLevel)
.concat(getHtmlHeaders(linesToToc, maxHeaderLevelHtml))
Expand Down
73 changes: 71 additions & 2 deletions test/transform.js
Original file line number Diff line number Diff line change
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, notitle, entryPrefix) {
function check(md, anchors, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll) {
test('transforming', function (t) {
var res = transform(md, mode, maxHeaderLevel, title, notitle, entryPrefix)
var res = transform(md, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll)

// remove wrapper
var data = res.data.split('\n');
Expand Down Expand Up @@ -284,6 +284,75 @@ test('transforming when old toc exists', function (t) {
t.end()
})

test('transforming when old toc exists and --all flag is set', function (t) {
var md = [
'# Header above'
, ''
, 'The above header should be ignored since it is above the existing toc'
, ''
, '<!-- START doctoc generated TOC please keep comment here to allow auto update -->'
, '<!-- DON\'T EDIT THIS SECTION INSTEAD RE-RUN doctoc TO UPDATE -->'
, '**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*'
, ''
, '- [OldHeader](#oldheader)'
, ''
, '<!-- END doctoc generated TOC please keep comment here to allow auto update -->'
, '## Header'
, 'some content'
, ''
].join('\n')

var res = transform(md, undefined, undefined, undefined, undefined, undefined, true)

t.ok(res.transformed, 'transforms it')

t.deepEqual(
res.toc.split('\n')
, [ '**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*',
'',
'- [Header above](#header-above)',
' - [Header](#header)',
'' ]
, 'replaces old toc'
)

t.deepEqual(
res.wrappedToc.split('\n')
, [ '<!-- START doctoc generated TOC please keep comment here to allow auto update -->',
'<!-- DON\'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->',
'**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*',
'',
'- [Header above](#header-above)',
' - [Header](#header)',
'',
'<!-- END doctoc generated TOC please keep comment here to allow auto update -->'
]
, 'wraps old toc'
)

t.deepEqual(
res.data.split('\n')
, [ '# Header above',
'',
'The above header should be ignored since it is above the existing toc',
'',
'<!-- START doctoc generated TOC please keep comment here to allow auto update -->',
'<!-- DON\'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->',
'**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*',
'',
'- [Header above](#header-above)',
' - [Header](#header)',
'',
'<!-- END doctoc generated TOC please keep comment here to allow auto update -->',
'## Header',
'some content',
'' ]
, 'updates the content with the new toc and ignores header before existing toc'
)
t.end()
})


// bigbucket.org
check(
[ '# My Module'
Expand Down

0 comments on commit f146558

Please sign in to comment.