Skip to content

feat(book): full title→heading chain in injectBookSectionDefaults - #33

Merged
mmcky merged 4 commits into
mainfrom
feature/book-section-defaults
May 18, 2026
Merged

mmcky merged 4 commits into
mainfrom
feature/book-section-defaults

Conversation

@mmcky

@mmcky mmcky commented May 15, 2026 •

Copy link
Copy Markdown

Summary

Closes #25 and #36. Wires the full title → heading_1 → … → heading_6 chain per section: tag in injectBookSectionDefaults so the chapter-prefix machinery in enumerate.ts composes without authors needing project-level or per-page frontmatter workarounds.

The problem (from #25)

In a project using just numbering.book: true and section: tags:

project:
  numbering:
    book: true
  toc:
    - file: index
    - title: Front matter
      section: frontmatter
      children: [{ file: preface }]
    - title: Chapters
      section: chapters
      children: [{ file: ch_intro }, { file: ch_fps }]

…the enumerate.ts auto-prefix machinery never fires on chapter pages. The page-title H1 (which pandoc-generated chapter files produce as # Introduction) doesn't increment a counter unless numbering.title.enabled: true, so this.enumerator (the chapter prefix) is undefined and figures / sections / theorems render flat: Figure 1 instead of Figure 1.1, Section 1 instead of 1.1, etc.

The existing workaround required setting title.enabled and heading_2/3/4.enabled at project level — but that then leaked into frontmatter/backmatter pages, numbering ## Acknowledgements on the preface as "1". Per-page frontmatter to disable it was the only way out, and isn't viable for book-dp1 where chapter files are pandoc-generated and forbidden to hand-edit.

The fix

In injectBookSectionDefaults (a ~40-line function), wire the matching chain per section:

section: tag New defaults
chapters / appendices title.enabled ??= true, heading_2/3/4/5/6.enabled ??= true (alongside existing heading_1 wiring)
frontmatter / backmatter title.enabled ??= false, heading_2/3/4/5/6.enabled ??= false (matching existing heading_1 disable)

The chain covers all six HTML heading depths because mystmd's numbering schema caps at heading_6 (myst-frontmatter numbering/types.ts) — wiring 1–6 is exhaustive rather than arbitrary.

Also changes the legacy hard h1.enabled = false on frontmatter/backmatter to ??= false to align with the documented precedence model — the hard assignment had violated layer 1 (page) by making a page-explicit override impossible.

Precedence (§3.5(g))

This change only adds defaults, never overrides. The mystmd precedence model — verified against the code in fillNumbering / fillMissingKeys — is fully honored:

  1. Page frontmatter numbering: (highest)
  2. Project numbering:
  3. Per-section TOC defaults (this layer)
  4. Hardcoded MyST defaults (lowest)

All assignments use ??= so any higher layer's setting flows through untouched. A project that explicitly enables, say, heading_2.enabled: true will still cause numbering on a section: frontmatter page — by design, per the documented precedence. Authors who want different behavior on a specific preface page set it explicitly in page frontmatter.

Tests added

10 new tests in bookSection.spec.ts:

  • chapters / appendices seed the full 1–6 chain enabled
  • frontmatter / backmatter seed the full 1–6 chain disabled
  • page override wins on chapters (e.g. author disables heading_2)
  • page override wins on frontmatter (e.g. author opts a preface in via title.enabled: true)
  • project-level numbering still wins over section default

All 19 bookSection.spec.ts tests pass; full myst-cli suite 293/293 green.

End-to-end verification against book-dp1

Branch built locally and npm-linked into book-dp1 (qe-v4 + #25 stripped of the project-level workaround — numbering: reduced to just book: true plus per-kind proof.scope). Verified on rendered output (commit 0b32ad13, pre-#36 extension; depth-4 verified after 33ae25cf):

Check Result
ch_intro: title 1, H2 1.1, H3 1.1.1, Figure 1.1, Theorem 1.2.1, Algorithm 1.1 ✅
ch_fps: counters reset to 2, 2.1, 2.1.1, 2.1 ✅
preface: title unnumbered, all H2s unnumbered, zero semantic enumerators ✅
appA: title A, H2 A.1/A.2/A.3, Theorem A.2.1 ✅
ch_intro: H4 numbered 1.1.1.1 (#36) ✅ (after 33ae25cf)

Notes on review feedback

The first revision overclaimed in the comments and changeset — it said the frontmatter/backmatter section defaults block project-level leakage, which contradicts §3.5(g) (section defaults are layer 3, below project). Copilot flagged the call-ordering issue, and a deep review confirmed the documented precedence is correct and matches the code. The follow-up commit (0b32ad13) reframes the claims around what the change actually does (chapter-side wiring removes the need for the workaround that caused leaks) without changing any code logic. End-to-end testing against book-dp1 then surfaced that the 1–3 chain wasn't enough — #### headings rendered flat — which was folded into this PR as 33ae25cf (closing #36).

Test plan

  • bookSection.spec.ts (19 tests) pass
  • myst-cli full suite (293 tests) pass
  • End-to-end verified against book-dp1 with numbering: { book: true } only — chapter, second-chapter, preface, appendix all render as expected; depth-4 numbering confirmed after Extend injectBookSectionDefaults to heading_4–heading_6 #36 extension

🤖 Generated with Claude Code

Wires `title.enabled`, `heading_2.enabled`, and `heading_3.enabled` per
`section:` tag in `injectBookSectionDefaults` so the chapter-prefix
machinery in enumerate.ts composes without authors needing project-level
workarounds.

Background: `enumerate.ts`'s book-mode auto-prefix only fires when the
page-title heading is itself numbered (it provides `this.enumerator`
for figures/theorems/sub-headings to prepend). The page-title heading
in turn requires `numbering.title.enabled: true`. PR #22 wired
`heading_1.enabled` per section but missed `title`, so chapter pages
rendered `Figure 1` / `Section 1` instead of `Figure 1.1` / `Section 1.1`
on a default `numbering.book: true` config. Authors hit this on pandoc-
generated chapter files (LaTeX `\chapter{}` → MD `# Title`) where they
cannot add per-page frontmatter.

This patch:

- chapters / appendices: seeds `title.enabled ??= true`,
  `heading_2.enabled ??= true`, `heading_3.enabled ??= true` alongside
  the existing `heading_1` defaults.
- frontmatter / backmatter: seeds those same keys to `??= false` so a
  project-level `heading_2.enabled: true` (often used so chapters number
  correctly) doesn't leak through and number the preface's `##` headings.
- Changes the legacy hard `h1.enabled = false` on frontmatter/backmatter
  to `??= false` for symmetry — a page that explicitly wants its title
  numbered (e.g. "Chapter 0: Preface") can now override the section
  default. The hard assignment had made this impossible.

All assignments use `??=` so the layered precedence (page > project >
section default > hardcoded) is preserved. Adds 10 new tests covering
the new wiring and the per-page override paths.

Closes #25.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
Copilot AI review requested due to automatic review settings May 15, 2026 02:31

Copilot AI left a comment

Copy link
Copy Markdown

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 updates injectBookSectionDefaults in myst-cli to seed book-mode numbering defaults per TOC section: tag so that chapter-prefix enumeration composes correctly when a page’s first H1 is absorbed as the page title (e.g., pandoc chapter conversions).

Changes:

  • Extend section-default injection to cover numbering.title, numbering.heading_2, and numbering.heading_3 (in addition to existing heading_1) for chapters/appendices, and seed disabled defaults for frontmatter/backmatter.
  • Add unit tests covering the new defaults and override behavior.
  • Add a changeset documenting the patch release.

Reviewed changes

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

File Description
packages/myst-cli/src/process/mdast.ts Extends injectBookSectionDefaults to seed title + heading_2/3 defaults by book section.
packages/myst-cli/src/process/bookSection.spec.ts Adds tests validating the new seeding/override behavior.
.changeset/book-section-defaults.md Declares a myst-cli patch release and describes the behavior change.
Comments suppressed due to low confidence (1)

packages/myst-cli/src/process/bookSection.spec.ts:200

  • This test (and its comment) uses heading_1.start: 0 to illustrate a “Chapter 0: Preface”, but the frontmatter validator enforces start >= 1 for numbering items (myst-frontmatter validateNumberingItem, min: 1). In the real pipeline a page-level start: 0 will be rejected/ignored, so this example doesn’t reflect a supported override unless validation rules are changed.
  test('page override wins on a frontmatter page: enabling title is preserved', () => {
    // The other direction: an author has a `Preface` they want numbered
    // as part of the chapter sequence ("Chapter 0: Preface"). They write
    // `numbering.heading_1: { enabled: true, start: 0, label: "Chapter %s" }`.
    // The section default uses ??=, so the page wins and the preface
    // gets numbered despite being tagged `section: frontmatter`.
    //
    // This is the symmetry fix: the legacy `h1.enabled = false` (hard
    // assignment) made this impossible.
    const fm: PageFrontmatter = {
      numbering: {
        book: { enabled: true },
        heading_1: { enabled: true, start: 0, label: 'Chapter %s' },
        title: { enabled: true },
      },

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

Comment thread packages/myst-cli/src/process/mdast.ts Resolved
Comment thread .changeset/book-section-defaults.md Resolved
Comment-and-changeset cleanup of injectBookSectionDefaults after a
deep review confirmed the documented precedence chain
(page > project > section > built-in) matches the code. The previous
claim that frontmatter/backmatter section defaults block project-level
numbering leakage contradicted that chain — `injectBookSectionDefaults`
runs after `processPageFrontmatter` merges project numbering into the
page, so `??=` no-ops for any key the project explicitly set.

Reframe the comments and changeset around the real story: the new
chapter-side wiring (title/heading_2/heading_3 seeded true on chapter
pages) means authors no longer need a project-level
`heading_2.enabled: true` workaround, so the leak scenario stops
happening in the common case. A project that explicitly enables those
depths still wins — by design, per §3.5(g).

Also replace the `start: 0` page-override test (rejected by the
`min: 1` validator at validators.ts:148) with a valid override using
`title.enabled: true`.

No code logic changes, no signature changes. All 19 bookSection tests
pass; full myst-cli suite 293/293 green.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
Extend `injectBookSectionDefaults` to wire heading_4/5/6 alongside the
existing title/heading_1/heading_2/heading_3 chain. With the depth-1–3
fix in place, authors who removed the project-level workaround
discovered that `####`+ headings on chapter pages rendered unnumbered
(verified against book-dp1 — ch_intro had H4s rendering with enum=None
instead of `1.1.1.1`).

mystmd's numbering schema caps at heading_6 (matches HTML h1–h6, see
myst-frontmatter numbering/types.ts), so wiring 1–6 is exhaustive
rather than arbitrary — there is no heading_7 to forget.

All assignments stay `??=` per §3.5(g) precedence (page > project >
section > built-in), so a project or page that explicitly disables a
deeper level still wins.

Tests updated to assert the full 1–6 chain on chapters/appendices and
the matching full disable on frontmatter/backmatter. All 19
bookSection tests pass; full myst-cli suite 293/293 green.

Closes #36.

Co-Authored-By: Claude Opus 4.7 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

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

Comment thread packages/myst-cli/src/process/mdast.ts Resolved
Move all numbering mutations inside the `switch` cases so any
`BookSection` value not explicitly handled (currently only `parts`,
which is divider-only) is a clean no-op past the gate checks.

Previously the function pre-initialized `heading_1` and (after the
heading_6 extension in 33ae25c) `title`/`heading_2…heading_6` to
empty `{}` objects before the switch, which leaked into the resolved
frontmatter for any page tagged `section: parts`. The `firstInSection`
branch could also have set `h1.start = 1` on such pages. Both are now
contained to the chapters/appendices and frontmatter/backmatter cases
that actually need them.

Caught by Copilot review on PR #33.

Add a regression test pinning the no-op behavior for `section: parts`
with `firstInSection: true` — the path that previously leaked the most.

20 bookSection tests pass; full myst-cli suite 294/294 green.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@mmcky
mmcky merged commit 043ddcb into main May 18, 2026
5 checks passed
@mmcky
mmcky deleted the feature/book-section-defaults branch May 18, 2026 04:28
mmcky added a commit that referenced this pull request May 20, 2026
PR #33 was merged onto main after qe-v4 but missed the VERSION.yml
append step. Recording it as id 5 with tag qe-v5, and adding its
squash SHA to the book-mode-with-section-scope upstream candidate
(it patches #22's injectBookSectionDefaults).

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
mmcky added a commit that referenced this pull request May 22, 2026
Records PR #40 (#34 — LaTeX `\newtheorem{name}[other]` counter
sharing) as id 6 with squash SHA a63d53f and tag qe-v6, and adds it
to the book-mode-with-section-scope upstream candidate alongside #22,
#28, and #33 — they share the same auto-prefix machinery and form one
coherent upstream story.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
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.

Book mode: injectBookSectionDefaults misses numbering.title and heading_2/3 wiring

2 participants