Skip to content
SprecherEast
Issue 573

feat: Section links inside articles (every heading gets an address)

Back to our plan
Done
enhancementfrontendpriority: high

Problem

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:

  1. 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.tsx already uses for links: use it. Payload already exports slugify from payload/shared, so no slug package is needed. CSS scroll-margin-top handles the sticky header.
  2. 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, where node_modules isn't installed).
  3. Open source: github-slugger (ISC, 2.0.0) gives unique slugs, but Payload's slugify plus a small counter for repeats does the same without a new package.
  4. 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 from slugify and -2, -3 for repeats. The converter and the menu both use it, so their ids always match. Plain-text extraction can reuse toPlainText.ts.
    • a heading converter that renders the same tag with that id and scroll-mt-24 (the site uses scroll-mt-24 for the same reason in Form.client.tsx).
    • Headings inside editorial blocks (Timeline, Steps, OutcomeFork render their own h3) are not sections and get no anchor.

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 example whats-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.tsx checks 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

See it on GitHub, opens in a new tab