Skip to content

feat: configurable location to insert new toc#346

Merged
AndrewSouthpaw merged 23 commits into
thlorenz:masterfrom
thompson-tomo:feat/#153_skip_heading
Jun 6, 2026
Merged

feat: configurable location to insert new toc#346
AndrewSouthpaw merged 23 commits into
thlorenz:masterfrom
thompson-tomo:feat/#153_skip_heading

Conversation

@thompson-tomo

@thompson-tomo thompson-tomo commented Apr 17, 2026

Copy link
Copy Markdown
Contributor

Closes: #153

Insert toc before first heading used in toc if configured to do so and no toc currently exists

@thompson-tomo thompson-tomo changed the title feat: skip heading #153 feat: skip heading Apr 17, 2026
@thompson-tomo thompson-tomo marked this pull request as ready for review April 25, 2026 08:06
@thompson-tomo thompson-tomo changed the title feat: skip heading feat: add toc before first heading in toc Apr 25, 2026

@AndrewSouthpaw AndrewSouthpaw left a comment

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.

I thought the point of the pragma is to make its location customizable? Seems like that meets the purpose here. Can you come up with a use case where this is limiting you?

@thompson-tomo

Copy link
Copy Markdown
Contributor Author

The scenario is when you are not running in update-only mode and a document is discovered which doesn't already have the toc pragma.

With this feature if the document doesn't already have a toc you can specify that the toc is to go before the first h2 heading hence your document will still pass markdownlint.

Comment thread test/transform-location.js
Comment thread test/transform-location.js
Comment thread lib/transform.js
Comment thread test/transform-location.js
Comment thread test/transform-location.js
Comment thread README.md Outdated
Comment thread lib/transform.js
@thompson-tomo thompson-tomo changed the title feat: add toc before first heading in toc feat: configurable location to insert new toc Apr 26, 2026
@thompson-tomo

Copy link
Copy Markdown
Contributor Author

Thanks @AndrewSouthpaw for the additional test cases & spotting the missing parameter. Please take another look.

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

Adds a configurable insertion location for newly generated TOCs, addressing the request to place the TOC below a title/intro instead of always at the very top when no TOC exists.

Changes:

  • Add --toc-location CLI option (default top) and plumb it through to the transformer.
  • Update lib/transform.js to support inserting a new TOC “before the first heading included in the TOC”.
  • Add fixture-based tests for markdown headings and HTML <hX> headings, plus README documentation for the new option.

Reviewed changes

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

Show a summary per file
File Description
lib/transform.js Adds logic to reposition insertion point for new TOCs when toc.location=before.
doctoc.js Introduces --toc-location arg and includes it in options passed to transform().
README.md Documents --toc-location and adds it to the README TOC.
test/transform-location.js Adds coverage for top vs before insertion across markdown + HTML heading scenarios.
test/fixtures/readme-location.md Fixture: basic markdown headings doc.
test/fixtures/readme-location-hTags.md Fixture: basic HTML <h1>/<h2> headings doc.

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

Comment thread lib/transform.js Outdated
Comment thread lib/transform.js
Comment thread lib/transform.js Outdated
Comment thread doctoc.js
Comment thread README.md Outdated
Comment thread test/transform-location.js Outdated
Comment thread test/transform-location.js
@thompson-tomo thompson-tomo force-pushed the feat/#153_skip_heading branch from 0f121bd to 78dd4e8 Compare April 27, 2026 04:12
@thompson-tomo

thompson-tomo commented May 5, 2026

Copy link
Copy Markdown
Contributor Author

@AndrewSouthpaw could you please give this another review.

@thompson-tomo

Copy link
Copy Markdown
Contributor Author

@AndrewSouthpaw could you circle back to this and the other pr's. Thanks.

@AndrewSouthpaw AndrewSouthpaw left a comment

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.

So sorry I dropped off there. LGTM. It looks like TOC before h1 in empty markdown is duplicate to TOC before h1 in empty but nbd.

@AndrewSouthpaw AndrewSouthpaw merged commit 9b9c1e5 into thlorenz:master Jun 6, 2026
5 checks passed
@thompson-tomo thompson-tomo deleted the feat/#153_skip_heading branch June 6, 2026 16:34
@thompson-tomo

Copy link
Copy Markdown
Contributor Author

No worries at all @AndrewSouthpaw, let's aim for a release once #344 & #353 are merged.

@AndrewSouthpaw

Copy link
Copy Markdown
Collaborator

2.5.0 released.

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.

[Feature request, help wanted] Add doctoc by default below the title if it's at the top of the page

3 participants