Skip to content

feat: heading <section> wrapping (P1.2) - #16

Merged
dereuromark merged 1 commit into
mainfrom
feat/section-wrapper-headings
May 21, 2026
Merged

feat: heading <section> wrapping (P1.2)#16
dereuromark merged 1 commit into
mainfrom
feat/section-wrapper-headings

Conversation

@dereuromark

Copy link
Copy Markdown
Contributor

Impl side of carve PR #18 (grammar PART 9 §13). Adopts djot's <section> wrapper around headings.

Behavior

renderHtml now does a stateful section-wrapping pass over top-level blocks. Every heading opens a <section id="{slug}"> holding the heading and the content up to the next same-or-shallower heading. The id moves from the <h*> to the <section>; sections nest by heading level.

# Intro

Text.

## Background

More.

# Next

Body.

<section id="intro">
  <h1>Intro</h1>
  <p>Text.</p>
  <section id="background">
    <h2>Background</h2>
    <p>More.</p>
  </section>
</section>
<section id="next">
  <h1>Next</h1>
  <p>Body.</p>
</section>

Rules

  • Adjacent same-level headings → sibling sections.
  • Skipped levels nest by number (# H1 / ### H3 → h3-section two levels deep, no synthesized h2).
  • Explicit {#id} on a heading goes to <section>; other heading attrs (classes, key-values) stay on <h*>.
  • No-heading / empty docs emit zero <section>s.
  • Nested headings inside containers (blockquote, list item) are not top-level, carry no id, and stay bare <h*>resolveHeadingIds only assigns ids to top-level headings.
  • Fragment URLs (#slug), </#id> cross-references, and [Heading][] implicit references all keep working — only the id emission site moved. Browsers resolve #slug to whichever element carries the id.

Mirrors djot-php's renderDocumentWithSections shape.

Tests

  • New test/section-wrapper.test.ts (11 cases): single/nested/sibling/skip-level wrapping, explicit-id placement, no-heading + empty docs, end-of-doc closing, crossref fragment resolution, nested-heading-not-wrapped.
  • Updated heading-ids + implicit-heading-refs assertions to the section shape.
  • 6 heading corpus fixtures (02-headings*, 17-attributes, 19-heading-ids) temporarily added to KNOWN_GAPS: the impl is now ahead of the vendored spec corpus, which still encodes the bare-<h*> shape. They are re-added once the carve repo re-vendors carve-lib and rewrites those fixtures, then bumps the spec submodule here. Same coordination pattern as the ASCII-slug change (ASCII-safe heading id slugs (baked Unicode -> ASCII translit) #13chore: bump spec submodule (ASCII heading-ids corpus + new examples) #15).

222 tests pass + 6 todo; typecheck clean.

Follow-up sequence

  1. (this PR) carve-js renderer + tests
  2. carve repo: re-vendor carve-lib + rewrite 02-headings* / 17-attributes / 19-heading-ids fixtures to section shape
  3. carve-js: bump spec submodule + un-skip the 6 KNOWN_GAPS entries
  4. carve-php: implement the same section wrapping

Implements grammar PART 9 §13 (carve PR #18). renderHtml now does a
stateful section-wrapping pass over top-level blocks: every heading
opens a <section id="{slug}"> holding the heading and the content up to
the next same-or-shallower heading. The id moves from the <h*> to the
<section>; sections nest by heading level.

- Adjacent same-level headings -> sibling sections.
- Skipped levels nest by number (# H1 / ### H3 -> h3-section two deep).
- Explicit {#id} on a heading goes to <section>; other heading attrs
  (classes, key-values) stay on <h*>.
- No-heading / empty docs emit zero <section>s.
- Nested headings inside containers are not top-level, carry no id, and
  stay bare <h*> (resolveHeadingIds only assigns ids to top-level).
- Fragment URLs (#slug), </#id> crossrefs, and [Heading][] implicit
  refs all keep working -- only the id emission site moved.

Matches djot-php's renderDocumentWithSections shape.

Tests:
- New test/section-wrapper.test.ts (11 cases).
- Updated heading-ids + implicit-heading-refs assertions to the new
  section shape.
- 6 heading corpus fixtures (02-headings*, 17-attributes,
  19-heading-ids) temporarily added to KNOWN_GAPS: the impl is now
  ahead of the vendored spec corpus, which still encodes the bare-<h*>
  shape. They are re-added once the carve repo re-vendors carve-lib and
  rewrites those fixtures, then bumps the spec submodule here.

222 tests pass + 6 todo; typecheck clean.
@dereuromark
dereuromark merged commit ac907bc into main May 21, 2026
2 checks passed
@dereuromark
dereuromark deleted the feat/section-wrapper-headings branch May 21, 2026 11:46
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