Skip to content

Add a REST endpoint exposing the Table of Contents heading resolver class #80786

Open

Description

@getdave

Part of #42229. Depends on #80781.

What

Add a REST endpoint that exposes the heading resolver class (#80781), so the editor can retrieve headings for a post the same way the front end does.

Why

The front end resolves headings through the heading resolver class on the server. The editor currently has no way to call that same class, so it has to keep its own separate logic for working out headings from the block list. That's exactly the kind of duplication the heading resolver class exists to remove, and it's why the editor and the front end can currently disagree about what a table of contents should show.

A REST endpoint gives the editor a way to ask the server the same question the front end asks, using the same code, without needing to reimplement heading discovery in JavaScript.

How

⚠️ Depends on the results of #80794, which is testing whether reusing the existing server-side render mechanism (useServerSideRender) can meet the editor's needs without a dedicated endpoint. If that approach proves viable, this issue may be scaled back or dropped in favour of it.

Tasks:

  • Add a REST endpoint that accepts enough information to resolve headings for a given post (or, eventually, unsaved editor content) and calls the heading resolver class.
  • Keep the endpoint's response shape focused on what the editor needs to render a preview, rather than exposing internal resolver details.
  • Consider whether the endpoint needs to support resolving headings from a supplied block list, not just a saved post, to support live editor previews before saving.

Activity

  1. Mamaduka commented on Jul 28, 2026

    @Mamaduka
    Member

    Why not use the existing SSR method for blocks (useServerSideRender)? The editor can pass post_id, and then the existing front-end code should handle everything.

    Adding a separate REST endpoint for every new and complex block won't be sustainable long term.

  2. added
    Needs Decision
    [Block] Table of contents (experimental)
    on Jul 28, 2026
  3. added
    [Type] Task
    on Jul 28, 2026
  4. getdave commented on Jul 28, 2026

    @getdave
    ContributorAuthor

    It's related to the situation @t-hamano raised where sometimes we won't yet have a Post ID

    #42229 (comment)

  5. Mamaduka commented on Jul 28, 2026

    @Mamaduka
    Member

    The endpoint will also need Post ID, no?

    If the post ID isn't available in a certain editor context, then it's better to display a placeholder state. I think the Breadcrumbs block does something similar.

  6. getdave commented on Jul 28, 2026

    @getdave
    ContributorAuthor

    I wouldn't necessarily, but let's set that aside for a second.

    I think the best approach is that we test the simplest option first. We can do a quick spike PoC using SSR as soon as the Headings Utility class PR is merged. If it works and the UX is ok then there's no need to over engineer this at all 🙏

    I'll rework the plan in the main Issue.

  7. getdave commented on Jul 29, 2026

    @getdave
    ContributorAuthor

    To be clearer about the plan I'm proposing if we decide to go down this route (the one in this Issue):

    • The editor serializes its current block content and sends it, along with relevant post/template context and ToC settings.
    • PHP runs the same heading resolver used by front-end rendering, against that supplied content rather than loading post_content from the database. Obviously the resolver class ultimately operates on the same underlying data structures so whether they come from post_content (via a post ID) or directly as blocks doesn't matter.
    • The endpoint returns either resolved heading data for an editor preview, (or possibly even the rendered ToC HTML itself if exact server markup is the goal).
    • The editor debounces requests while the author types and retains the last successful preview during loading.

    That gives us all three desirable behaviours:

    1. New unsaved post: headings can render without a post ID.
    2. Saved post with unsaved edits: preview updates from the editor’s current content, not the old draft.
    3. Template shown against a post: resolver uses that post’s effective content/context.

    I appreciate this is a bit more complex than the simpler SSR approach but it might (subject to discovery in #80794) prove necessary to achieve a solid UX.

  8. Mamaduka commented on Jul 29, 2026

    @Mamaduka
    Member

    Also left a comment on the PR - #80796 (comment).

    From desired behaviors, what do we think is missing currently? The number #3 - "Template shown against a post: resolver uses that post’s effective content/context."?

    I just did a quick test:

    1. Add ToC to the Single Posts template
    2. Open a post with headings
    3. Enable "Show Template"
    4. ToC block displays headings.

    Mostly trying to figure out if we really need a special endpoint or SSR just to display the ToC preview in the editor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs Decision[Block] Table of contents (experimental)[Type] Task

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions