Skip to content

fix(core): never render headings inside component children - #524

Merged
gitfromwildan merged 1 commit into
mainfrom
fix/no-headings-in-components
Nov 10, 2026
Merged

gitfromwildan merged 1 commit into
mainfrom
fix/no-headings-in-components

Conversation

@gitfromwildan

Copy link
Copy Markdown
Member

Summary

A component never renders an <h1>–<h6> among its children now. Before this change, a #…###### line inside a callout, card, tab, accordion, step or GitHub alert became a real heading with an id and an anchor link, and h2–h4 also got a table-of-contents entry. That broke the page contract in three ways:

  • # H1 inside a component put a second <h1> on the page, next to the frontmatter title the layout already renders.
  • A heading in a hidden tab panel or a collapsed accordion (both rendered with hidden) became a TOC link to content the reader could not see. Clicking it did not scroll, and the tab or accordion did not open.
  • Two tabs that each start with ## Install added #install and #install-1 to the TOC, and one of them always pointed at a hidden panel.

Change

  • remarkComponentHeadings (new, packages/core/src/plugins/remarkComponentHeadings.ts) runs in createDefaultRemarkPlugins() after remarkDirectiveToMdx. It turns every heading under an mdxJsxFlowElement into a paragraph that keeps its inline content (bold, code, links). Directives and GitHub alerts are the only components in the v2 md pipeline, so this covers all of them. The plugin is also exported from @docubook/core.
  • Headings outside components are unchanged. That includes a heading in a plain blockquote, which is not a component.
  • Both compileMdx in flame and extractTocsFromRawMdx use the default remark stack, so the rendered HTML and the TOC follow the same rule.
  • The flame search indexer reads raw lines, so it gets the same rule separately. A heading inside a container directive is indexed as content of the enclosing section instead of as its own lvl2+ record. Without this, the record's anchor would point at an id that no longer exists. A heading marker on an alert line (> ## …) is stripped from the indexed text. The :::name and ::: fence lines are no longer indexed as text.
  • extract.test.ts had a case that expected a heading inside :::note in the TOC (from fix: compile TOCs from rendered headings and harden directive fallbacks #373). That was the old contract, so the test now expects the heading to be skipped.
  • Docs: a "No headings inside components" section in getting-started/formatting.mdx, and the plugin in packages/core/README.md.
  • A patch changeset for @docubook/core and @docubook/flame.

No page in the docs site or the template has a heading inside a directive, so no rendered docs page changes.

Known limit

Flame appends user remark plugins after the default stack. A user plugin that inserts a heading into a component element on purpose is not rewritten. Authored content always follows the rule.

Verification

  • Core: a new remarkComponentHeadings.test.ts compiles every level, h1–h6, inside callouts, cards, tabs, accordions, steps and GitHub alerts. It asserts that no heading is emitted and the text is kept, that inline markup survives as a paragraph, that headings outside components and in plain blockquotes stay headings, and that duplicate tab headings stay out of the TOC.
  • Flame: a new component-headings.test.ts runs compileMdx → renderToStaticMarkup on a page that mixes all component types. It asserts that the only heading in the HTML is the page's ## Section, that each component heading renders as <p>…</p>, and that the TOC holds only that section. Before the fix, the same input rendered h1–h6 inside every component and listed them in the TOC.
  • Search indexer: two new cases cover component headings indexed as content under the enclosing section, no ::: or ### text in the records, and headings after a component indexed as sections again.
  • @docubook/core tests (144) and @docubook/flame tests (879) pass. Typecheck, oxlint and prettier are clean, and the pre-push build passed.

A `#`…`######` line inside a callout, card, tab, accordion, step or
GitHub alert became a real h1–h6 with an anchor link, and h2–h4 also
got a table-of-contents entry. That put a second h1 on the page, and a
heading in a hidden tab or a collapsed accordion became a TOC link to
content the reader could not see.

The new remarkComponentHeadings plugin runs in the default remark stack
after remarkDirectiveToMdx and turns every heading under a component
element into a paragraph that keeps its inline content. Headings
outside components, including those in a plain blockquote, are
unchanged. The extract test that expected a directive heading in the
TOC now expects it to be skipped.

The flame search indexer follows the same rule: a heading inside a
container directive is indexed as content of the enclosing section
instead of as its own section, a heading marker in an alert line is
stripped from the indexed text, and the `:::` fence lines themselves
are no longer indexed as text.
@vercel

vercel Bot commented Nov 10, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docu Ignored Ignored Nov 10, 2026 4:55pm UTC

@gitfromwildan gitfromwildan self-assigned this Nov 10, 2026

@pullfrog pullfrog Bot 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.

✅ No new issues found.

Reviewed changes

Mode: standard. Reviewed the single commit that stops components from rendering headings among their children. It changes the core remark stack, the flame search indexer, the tests and the docs.

  • remarkComponentHeadings — a new remark plugin, placed last in createDefaultRemarkPlugins(). It rewrites every heading under an mdxJsxFlowElement into a paragraph, so callouts, cards, tabs, accordions, steps and GitHub alerts produce no h1–h6, no anchor and no TOC entry.
  • TOC parity — extractTocsFromRawMdx (core extract.ts) and flame compileMdx (mdx.ts) both build on the default stack. That means the rendered HTML, rehypeCollectTocs and the raw TOC extraction all apply the same rule.
  • Search indexer — extractRecords tracks componentDepth from :::name and ::: fences. A heading inside a component becomes a standalone content record under the enclosing section. Fence lines are no longer indexed. # markers on alert and blockquote lines are stripped.
  • Contract update — the extract.test.ts case from #373 now expects a heading inside a directive to be left out of the TOC.
  • Docs and release — adds a "No headings inside components" section in formatting.mdx, updates the core README rows, and adds a patch changeset for @docubook/core and @docubook/flame.

Overall assessment: ready to merge. I found no CRITICAL, HIGH or MEDIUM issues.

What I checked:

  • Cache invalidation: cache-key.ts already includes .docu/node/search-indexer.ts in its key material, and renderToolchainStamp() folds in the built @docubook/* packages. Existing caches are therefore invalidated without a BUILD_CACHE_VERSION bump.
  • Unbalanced fences: if :::: arrives while an inner :::tab is still open, micromark closes only the inner container, and the outer one runs to the end of the document. The indexer's depth count also stays above 0 in that case, so the search index matches what is rendered instead of drifting from it.
  • Components that depend on child headings: no component in @docubook/markdown reads headings from its children, so changing them to paragraphs changes no component's behavior.

Pullfrog  | View workflow run | Using claude-opus-5-5 | 𝕏

@gitfromwildan
gitfromwildan merged commit 7c781d0 into main Nov 10, 2026
15 checks passed
@gitfromwildan
gitfromwildan deleted the fix/no-headings-in-components branch November 10, 2026 16:58
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.

1 participant