-
-
Notifications
You must be signed in to change notification settings - Fork 486
feat: modularise toc part generation with variable padding length #369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
2c964e8
fbeb8a5
2bb7867
b3d947a
4426341
8b1f3f9
080da6b
376924d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,10 +55,44 @@ function escapedStartTag(syntax) { | |
| return commentTypes[syntax].escapedStart; | ||
| } | ||
|
|
||
| function tocFooter(footerOptions) { | ||
| return tocPart(footerOptions?.padding, footerOptions?.content); | ||
| } | ||
|
|
||
| function tocHeader(headerOptions) { | ||
| return tocPart(headerOptions?.padding, headerOptions?.content); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Either wire up the flags and docs in this PR (which is what the title suggests), or leave |
||
| } | ||
|
|
||
| function tocTitle(titleOptions, existingTitle) { | ||
| if (titleOptions?.remove || (existingTitle === "" && !titleOptions?.content)) return []; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This creates a with Or at least some comments. |
||
| var content; | ||
| if (titleOptions?.content){ | ||
| content = titleOptions.content; | ||
| } | ||
| else if(existingTitle){ | ||
| content = existingTitle; | ||
| } | ||
| else{ | ||
| content = '**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*'; | ||
| } | ||
| return tocPart(titleOptions?.padding, content); | ||
| } | ||
|
|
||
| function tocPart(padding, content){ | ||
| if (content === undefined) return []; | ||
| var lines = Array(Number(padding?.before ?? 0)).fill(''); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The CLI needs validation on non-negative integers, e.g.: Similar for Let's add some CLI arg validation? |
||
| lines.push(content); | ||
| lines.push(...Array(Number(padding?.after ?? 0)).fill('')); | ||
| return lines; | ||
| } | ||
|
|
||
| module.exports = { | ||
| commentedBlock, | ||
| escapedStartTag, | ||
| pragmaMarkers, | ||
| skipTag, | ||
| excludeTag | ||
| excludeTag, | ||
| tocFooter, | ||
| tocHeader, | ||
| tocTitle | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. General thought: add direct unit tests for |
||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,14 +153,8 @@ function processHeaders(headers, mode) { | |
| return headers; | ||
| } | ||
|
|
||
| // When updating an existing TOC, try to preserve the title that was there before. | ||
| // Falls back to CLI args (--title, --notitle) or the default title for new TOCs. | ||
| function determineTitle(nodes, tocPosition, title, notitle, header) { | ||
| var defaultTitle = '**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*'; | ||
|
|
||
| if (notitle) return ""; | ||
| if (title) return title; | ||
| if (!tocPosition?.existing) return defaultTitle; | ||
| function determineTitle(nodes, tocPosition, titleOptions, header) { | ||
| if (titleOptions.remove || titleOptions.content || !tocPosition?.existing) return undefined; | ||
|
|
||
| // Find the TOC's list node so we know where the title region ends | ||
| var listStartIdx = nodes.children.filter(function (x) { | ||
|
|
@@ -231,6 +225,7 @@ exports = module.exports = function transform(content, mode, maxHeaderLevel, min | |
| options.toc.items ??= {}; | ||
| options.toc.items.indentation ??= {}; | ||
| options.toc.list ??= {}; | ||
| options.toc.title ??= {}; | ||
|
|
||
| syntax = syntax || "md"; | ||
| options.toc.items.indentation.style ??= 'space'; | ||
|
|
@@ -241,6 +236,8 @@ exports = module.exports = function transform(content, mode, maxHeaderLevel, min | |
| var ordered = options.toc.list.format === 'ordered'; | ||
| options.toc.items.symbols ??= ordered ? undefined : String(entryPrefix || '-').split(','); | ||
| options.toc.list.style ??= ordered ? 'number' : undefined | ||
| options.toc.title.content ??= title; | ||
| options.toc.title.remove ??= notitle; | ||
|
|
||
| var eol = '\n'; | ||
| eol = detectLineEnding(content, eol); | ||
|
|
@@ -255,11 +252,6 @@ exports = module.exports = function transform(content, mode, maxHeaderLevel, min | |
| minTocItems = 1; | ||
| } | ||
|
|
||
| var padTitle = options?.toc?.title?.padding?.before > 0; | ||
| if (options?.toc?.title?.padding?.before === undefined){ | ||
| padTitle = notitle || false; | ||
| } | ||
|
|
||
| // only limit *HTML* headings by default | ||
| var maxHeaderLevelHtml = maxHeaderLevel || 4; | ||
|
|
||
|
|
@@ -338,16 +330,15 @@ exports = module.exports = function transform(content, mode, maxHeaderLevel, min | |
|
|
||
| if (headers.length >= minTocItems && docNodes.children[docNodes.children.length - 1].loc.end.line >= minLines) { | ||
| var tocLines = []; | ||
| var inferredTitle = determineTitle(docNodes, tocPosition, title, notitle, options?.toc?.header?.content); | ||
| var inferredTitle = determineTitle(docNodes, tocPosition, options.toc.title, options.toc.header?.content); | ||
|
|
||
| var allHeaders = processHeaders(headers, mode); | ||
| var lowestRank = allHeaders.reduce((min, h) => Math.min(min, h.rank), Infinity); | ||
|
|
||
| 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(''); } | ||
| if (inferredTitle) { tocLines.push(inferredTitle); } | ||
| tocLines.push(...contentGenerator.tocHeader(options.toc.header)); | ||
| tocLines.push(...contentGenerator.tocTitle(options.toc.title, inferredTitle)); | ||
| tocLines.push(''); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Similar to footer. Can we add tests to cover this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is expected. The hard coded padding is for the items. It is no different to enabling padding after header & before title. |
||
| var symbolQty = options.toc.items.symbols?.length || 1; | ||
|
|
||
|
|
@@ -383,7 +374,7 @@ exports = module.exports = function transform(content, mode, maxHeaderLevel, min | |
| })); | ||
|
|
||
| tocLines.push(''); | ||
| if (options?.toc?.footer?.content) { tocLines.push(options.toc.footer.content); } | ||
| tocLines.push(...contentGenerator.tocFooter(options.toc.footer)); | ||
|
|
||
| toc = tocLines.join(eol); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.