Skip to content

fix(remark): prevent options mutation across multi-file builds in remark-table-of-contents - #1815

Open
cynicalmindset wants to merge 1 commit into
json-schema-org:mainfrom
cynicalmindset:fix/remark-table-of-contents-options-mutation
Open

cynicalmindset wants to merge 1 commit into
json-schema-org:mainfrom
cynicalmindset:fix/remark-table-of-contents-options-mutation

Conversation

@cynicalmindset

Copy link
Copy Markdown

Summary

Fixes closure state leakage in remark-table-of-contents.js and eliminates phantom empty nested list items in the generated Table of Contents.

Problem

  1. When running Remark over multiple Markdown documents in a single process (such as npm run build -- specs), the plugin reassigns options.skip from an Array into a RegExp in the outer closure. On subsequent documents, invoking options.skip.push() throws TypeError: options.skip.push is not a function.
  2. Heading indentation while-loops executed before evaluating skipRegex.test(headingText), creating an empty nested sublist container (<li><ul></ul></li>) for skipped headings whose depth was greater than currentDepth.

Root cause

  • Unscoped closure variable mutation (options = { ...defaultOptions, ...options }) without local declaration.
  • Reassigning options.skip in place instead of creating a pre-compiled RegExp during plugin instantiation.
  • Positioning the skip test after the depth advancement while-loops.

Changes

  • Scoped options merging and skipRegex compilation to the outer plugin factory function.
  • Preserved caller options array immutability by cloning the skip list.
  • Evaluated skipRegex.test(headingText) before creating child list nodes in the AST.
  • Added comprehensive unit tests in remark/remark-table-of-contents.test.js.
  • Included remark unit tests in vitest-schema.config.js.

Testing

  • remark/remark-table-of-contents.test.js confirms repeated invocations run without TypeError.
  • Confirmed skipped headings do not produce orphan empty bullet items.
  • Conforms to ESLint formatting rules.

Checklist

  • Code conforms to project style and ESLint rules.
  • Surgical, focused patch with zero unrelated changes.
  • Deterministic unit tests added.
  • Existing tests and builds continue to pass.

closes #1814

@cynicalmindset

Copy link
Copy Markdown
Author

@jdesrosiers PTAL

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.

fix(remark-table-of-contents): prevent closure options mutation on multi-file builds and phantom sublists

1 participant