feat: Section links inside articles (every heading gets an address)
Back to our planProblem
In plain words: headings inside a post or event have no address, so nothing can link straight to a section ("jump to What's next"). The "On this page" menu needs those addresses first. This change gives every section heading a stable link, and leaves room above it so the sticky site header doesn't cover the heading after a jump.
Today (checked): post and event text is drawn by src/platform/richtext/RichText.tsx, which uses Payload's default converters and overrides only links. Payload's heading converter (@payloadcms/richtext-lexical 3.91.0, dist/features/converters/lexicalToJSX/converter/converters/heading.js) renders <h2> etc. with no id. Posts allow h1–h4 in the editor (src/modules/news/cms/posts.ts), events h2–h4 (src/modules/events/cms/events.ts).
Proposed fix
Native first, one line per step:
- Native (Payload, React, CSS): Payload has no heading-id or table-of-contents option in 3.91.0 (its feature folders: align, blockquote, blocks, converters, debug, experimental_table, format, heading, horizontalRule, indent, link, lists, paragraph, relationship, textState, toolbars, upload). Its documented extension point is a custom JSX converter, which
RichText.tsxalready uses for links: use it. Payload already exportsslugifyfrompayload/shared, so no slug package is needed. CSSscroll-margin-tophandles the sticky header. - Official plugins: none for headings or contents in
@payloadcms/*. Lexical's own table-of-contents plugin works inside the editor, not on the published page (from Lexical's docs; not checked in this checkout, wherenode_modulesisn't installed). - Open source:
github-slugger(ISC, 2.0.0) gives unique slugs, but Payload'sslugifyplus a small counter for repeats does the same without a new package. - Our code (small): in
src/platform/richtext/:- a pure
headingAnchors(data)that walks the Lexical document and returns{ id, text, level }for every h2 and h3, with ids fromslugifyand-2,-3for repeats. The converter and the menu both use it, so their ids always match. Plain-text extraction can reusetoPlainText.ts. - a
headingconverter that renders the same tag with thatidandscroll-mt-24(the site usesscroll-mt-24for the same reason inForm.client.tsx). - Headings inside editorial blocks (Timeline, Steps, OutcomeFork render their own
h3) are not sections and get no anchor.
- a pure
Found while reading, not in scope: posts allow h1 inside the article, which makes a second h1 on the page. File a separate issue to drop h1 from the posts editor (and check existing posts) rather than widen this PR.
Acceptance criteria
- Not done: On a post and an event, every h2 and h3 in the article has a unique, readable
id(for examplewhats-next), and the same text gives the same id on every render. - Not done: Opening
/posts/<slug>#<id>lands with the heading fully visible below the sticky header at 320 and 1280 wide, light and dark. - Not done: Unit tests for
headingAnchors: repeated headings, punctuation and accents, empty headings, headings inside blocks ignored. - Not done:
RichText.test.tsxchecks the ids render; axe passes. - Not done: Rich text elsewhere (pages, form blocks) still renders as before.
Area
frontend
Priority
high
Generated by Claude Code, opens in a new tab