Skip to content

feat: modularise toc part generation with variable padding length - #369

Open
thompson-tomo wants to merge 8 commits into
thlorenz:masterfrom
thompson-tomo:content_generation
Open

feat: modularise toc part generation with variable padding length#369
thompson-tomo wants to merge 8 commits into
thlorenz:masterfrom
thompson-tomo:content_generation

Conversation

@thompson-tomo

Copy link
Copy Markdown
Contributor

This extracts the generation of the toc parts into the content generator module.

This extraction improves testability, functionality scoping & lays the foundation for enabling full control of padding before & after the content.

This approach enables control of the qty of padding before title etc.

@thompson-tomo
thompson-tomo marked this pull request as ready for review June 26, 2026 10:50
@thompson-tomo thompson-tomo changed the title feat: modularise toc part generation feat: modularise toc part generation with variable padding length Jun 26, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors TOC assembly by extracting TOC header/title/footer line generation into lib/content-generation.js, aiming to improve modularity and enable configurable padding around TOC parts.

Changes:

  • Moved TOC header/title/footer line construction into reusable helpers (tocHeader, tocTitle, tocFooter) in lib/content-generation.js.
  • Updated lib/transform.js to use the new content generator helpers and to centralize title options under options.toc.title.
  • Adjusted README.md documentation for TOC title padding.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
README.md Updates documentation around --toc-title-padding-before behavior/expectations.
lib/transform.js Switches TOC composition to use new content-generation helpers and updates title inference flow.
lib/content-generation.js Introduces helpers to generate TOC parts with padding and content selection logic.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/content-generation.js Outdated
Comment thread README.md
Comment thread lib/transform.js Outdated
@AndrewSouthpaw

AndrewSouthpaw commented Jul 14, 2026 via email

Copy link
Copy Markdown
Collaborator

@thompson-tomo

Copy link
Copy Markdown
Contributor Author

No worries at all @AndrewSouthpaw hope that everyone is on the mend. 🤞

Comment thread README.md
Comment thread lib/content-generation.js

function tocPart(padding, content){
if (content === undefined) return [];
var lines = Array(Number(padding?.before ?? 0)).fill('');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CLI needs validation on non-negative integers, e.g.:

$ doctoc --toc-title-padding-before 0.5 README.md
RangeError: Invalid array length
    at tocPart (lib/content-generation.js:83:15)

Similar for 1.5.

Let's add some CLI arg validation?

Comment thread lib/transform.js
if (inferredTitle) { tocLines.push(inferredTitle); }
tocLines.push(...contentGenerator.tocHeader(options.toc.header));
tocLines.push(...contentGenerator.tocTitle(options.toc.title, inferredTitle));
tocLines.push('');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

padding.after stacks on top of this hardcoded blank rather than replacing it, so the new knob is effectively off by one — { toc: { title: { padding: { after: 1 } } } } produces two blank lines:

**Table of Contents**  *generated with [DocToc](...)*
                              ← padding.after
                              ← this line
- [A](#a)

Similar to footer.

Can we add tests to cover this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.

Comment thread lib/content-generation.js
}

function tocHeader(headerOptions) {
return tocPart(headerOptions?.padding, headerOptions?.content);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

header.padding and footer.padding are read here but nothing ever sets them — no flag in doctoc.js, no mention in the README, no test. Same for title.padding.after. Only title.padding.before has a way in.

Either wire up the flags and docs in this PR (which is what the title suggests), or leave padding off header/footer until there's a way to reach it. As-is it reads like a feature but is dead for every CLI user.

Comment thread lib/content-generation.js
}

function tocTitle(titleOptions, existingTitle) {
if (titleOptions?.remove || (existingTitle === "" && !titleOptions?.content)) return [];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates a with determineTitle that nothing spells out: undefined means "nothing known, use the default", "" means "the existing TOC deliberately had no title, keep it that way", and a non-empty string means "preserve this one". Maybe worth a simple object, like:

const titleSetting: {
  default: undefined,
  existingTocNoTitle: "",
  preserveTocTitle: "...",
}

Or at least some comments.

Comment thread lib/content-generation.js
excludeTag,
tocFooter,
tocHeader,
tocTitle

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General thought: add direct unit tests for tocTitle's fallback chain (explicit content → existing title → default → removed).

Remove restriction on padding before title greater than 1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants