Skip to content

Commit

Permalink
feat: flag to keep file without toc untouched
Browse files Browse the repository at this point in the history
add cli flag to prevent doctoc from adding toc to a file without toc.
this is good for CI or a combination with lint-staged.
with -p, doctoc will update toc if md has toc sectioon, otherwise doctoc keep it unmodified.
  • Loading branch information
uetchy committed Oct 14, 2020
1 parent f146558 commit 617c7c5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
9 changes: 5 additions & 4 deletions doctoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function cleanPath(path) {
return homeExpanded.replace(/\s/g, '\\ ');
}

function transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll, stdOut) {
function transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll, stdOut, preserve) {
if (processAll) {
console.log('--all flag is enabled. Including headers before the TOC location.')
}
Expand All @@ -26,7 +26,7 @@ function transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPref
var transformed = files
.map(function (x) {
var content = fs.readFileSync(x.path, 'utf8')
, result = transform(content, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll);
, result = transform(content, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll, preserve);
result.path = x.path;
return result;
});
Expand Down Expand Up @@ -79,7 +79,7 @@ var modes = {
var mode = modes['github'];

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

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 @@ -115,7 +116,7 @@ for (var i = 0; i < argv._.length; i++) {
files = [{ path: target }];
}

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

console.log('\nEverything is OK.');
}
Expand Down
6 changes: 5 additions & 1 deletion lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, processAll) {
exports = module.exports = function transform(content, mode, maxHeaderLevel, title, notitle, entryPrefix, processAll, preserve) {
mode = mode || 'github.com';
entryPrefix = entryPrefix || '-';

Expand All @@ -116,6 +116,10 @@ exports = module.exports = function transform(content, mode, maxHeaderLevel, tit
var lines = content.split('\n')
, info = updateSection.parse(lines, matchesStart, matchesEnd)

if (!info.hasStart && preserve) {
return { transformed: false };
}

var inferredTitle = determineTitle(title, notitle, lines, info);

var currentToc = info.hasStart && lines.slice(info.startIdx, info.endIdx + 1).join('\n')
Expand Down

0 comments on commit 617c7c5

Please sign in to comment.