Skip to content
Draft
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
8 changes: 6 additions & 2 deletions doctoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function transformAndSave(files, mode, maxHeaderLevel, minHeaderLevel, minTocIte
function printUsageAndExit(isErr) {
var outputFunc = isErr ? log.error : log.info;

outputFunc('Usage: doctoc [mode] [--entryprefix prefix] [--notitle | --title title] [--maxlevel level] [--minlevel level] [--mintocitems qty] [--toc-pragma-style style] [--toc-header-content content] [--toc-footer-content content] [--toc-items-indentation-width width] [--all] [--loglevel level] [--update-only] [--syntax (' + supportedSyntaxes.join("|") + ')] <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] [--minlevel level] [--mintocitems qty] [--toc-pragma-style style] [--toc-header-content content] [--toc-footer-content content] [--toc-items-indentation-width width] [--toc-items-indentation-style style][--all] [--loglevel level] [--update-only] [--syntax (' + supportedSyntaxes.join("|") + ')] <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 @@ -97,7 +97,7 @@ var mode = modes["github"];
var argv = minimist(process.argv.slice(2),
{
boolean: [ 'h', 'help', 'T', 'notitle', 's', 'stdout', 'all' , 'u', 'update-only', 'd', 'dryrun'].concat(Object.keys(modes)),
string: [ 'title', 't', 'maxlevel', 'm', 'minlevel', 'entryprefix', 'syntax', 'mintocitems', 'toc-title-padding-before', 'toc-header-content', 'toc-footer-content', 'toc-pragma-style', 'toc-items-indentation-width', 'document-lines-min', 'l', 'loglevel' ],
string: [ 'title', 't', 'maxlevel', 'm', 'minlevel', 'entryprefix', 'syntax', 'mintocitems', 'toc-title-padding-before', 'toc-header-content', 'toc-footer-content', 'toc-pragma-style', 'toc-items-indentation-width', 'toc-items-indentation-style', 'document-lines-min', 'l', 'loglevel' ],
unknown: function(a) { return (a[0] == '-' ? (console.error('Unknown option(s): ' + a), printUsageAndExit(true)) : true); }
});

Expand Down Expand Up @@ -158,6 +158,9 @@ var indentWidth = argv['toc-items-indentation-width'];
if (indentWidth !== undefined && isNaN(indentWidth)) { log.error('ToC indentation width: ' + indentWidth + ' is not a number'), printUsageAndExit(true); }
else if (indentWidth === undefined) { indentWidth = (mode === 'bitbucket.org' || mode === 'gitlab.com') ? 4 : 2; }

var indentStyle = argv['toc-items-indentation-style'];
if (indentStyle && indentStyle !== 'space' && indentStyle !== 'tab') { log.error('Indentation style not supported: ' + indentStyle), printUsageAndExit(true); }

var minLines = argv['document-lines-min'] || 0;
if (isNaN(minLines)) { log.error('Document min lines: ' + minLines + ' is not a number'), printUsageAndExit(true); }

Expand All @@ -177,6 +180,7 @@ var options = {
items: {
indentation:{
width: indentWidth,
style: indentStyle,
}
},
title: {
Expand Down
17 changes: 9 additions & 8 deletions lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,14 @@ function detectLineEnding(content, defaultEol) {
}

exports = module.exports = function transform(content, mode, maxHeaderLevel, minHeaderLevel, minTocItems, title, notitle, entryPrefix, processAll, updateOnly, syntax, options) {
options ??= {};
options.toc ??= {};
options.toc.items ??= {};
options.toc.items.indentation ??= {};

syntax = syntax || "md";
options.toc.items.indentation.style ??= 'space';
options.toc.items.indentation.width ??= (mode === 'bitbucket.org' || mode === 'gitlab.com') ? 4 : 2;
var eol = '\n';
eol = detectLineEnding(content, eol);
var skipTag = contentGenerator.skipTag(syntax) + eol;
Expand Down Expand Up @@ -262,13 +269,7 @@ exports = module.exports = function transform(content, mode, maxHeaderLevel, min
var allHeaders = processHeaders(headers, mode);
var lowestRank = allHeaders.reduce((min, h) => Math.min(min, h.rank), Infinity);

var indentation = ' ';
var indentationWidth = options?.toc?.items?.indentation?.width;
// remove this fallback based on mode in v3
if(indentationWidth === undefined){
// 4 spaces required for proper indention on Bitbucket and GitLab
indentationWidth = (mode === 'bitbucket.org' || mode === 'gitlab.com') ? 4 : 2;
}
var indentation = options.toc.items.indentation.style === 'tab' ? '\t' : ' ';

if (options?.toc?.header?.content) { tocLines.push(options.toc.header.content); }
if (padTitle && inferredTitle) { tocLines.push(''); }
Expand All @@ -277,7 +278,7 @@ exports = module.exports = function transform(content, mode, maxHeaderLevel, min

tocLines.push(...allHeaders
.map(function (x) {
return indentation.repeat((x.rank - lowestRank) * indentationWidth) + entryPrefix + ' ' + x.anchor;
return indentation.repeat((x.rank - lowestRank) * options.toc.items.indentation.width) + entryPrefix + ' ' + x.anchor;
}));

tocLines.push('');
Expand Down