Skip to content

TOC should render statically analyzable embedded expressions in headings (esp. imported partials) #9772

Open

Description

@anatolykopyl

Have you read the Contributing Guidelines on issues?

Prerequisites

Description

If a prop is passed to a partial Markdown file, then the ToC displays {props.propname}, instead of the actual value.

As @slorber pointed out in #9684, this is a known limitaion that stems from that even in non-partial files headings don't support expressions.
While having expressions in non-partial headings is a feature, that would be nice for completeness, it's probably not too practical. On the other hand, having expressions in headings of partials can be very useful.

Reproducible demo

No response

Steps to reproduce

  1. Create a partial file that uses a prop in a heading
  2. Import that file
  3. Use the partial

Expected behavior

The ToC shows the value passed to the partial

Actual behavior

The ToC shows {props.propname}

Your environment

No response

Self-service

  • I'd be willing to fix this bug myself.

Activity

  1. added
    bug
    status: needs triage
    on Jan 21, 2024
  2. Josh-Cena commented on Mar 3, 2024

    @Josh-Cena
    Collaborator

    I don't think it's possible to solve in a generic way without us rendering the whole page, but we might be able to solve a few specific and statically analyzable cases. Maybe if the heading contains expressions, we should export the TOC like export toc = (props) => [`Some ${props.propname} value`] and then in the main file export toc = [..._toc1({ propname: "foo" })]?

  3. added
    difficulty: advanced
    domain: markdown
    feature
    and removed
    status: needs triage
    bug
    on Mar 3, 2024
  4. changed the title [-]Props passed to partials will not appear in the ToC if used in a heading.[/-] [+]TOC should render interpolated expressions in headings[/+] on Mar 3, 2024
  5. changed the title [-]TOC should render interpolated expressions in headings[/-] [+]TOC should render statically analyzable embedded expressions in headings[/+] on Mar 3, 2024
  6. changed the title [-]TOC should render statically analyzable embedded expressions in headings[/-] [+]TOC should render statically analyzable embedded expressions in headings (esp. imported partials)[/+] on Mar 3, 2024
  7. slorber commented on Mar 4, 2024

    @slorber
    Collaborator

    👍

    That's also the idea I had in mind to support React components usage in TOC:

    #3915 (comment)

    #9773

    We should rather pass the props to all MDX and all React components toc(props) function.

    Wondering if the "root" (non partial) MDX doc should still export a toc constant or a function. If toc is always a function, this might feel a bit awkward to have to call that function in the theme? 🤷‍♂️

    One interesting thing with a function is that maybe it opens the door to "dynamic TOCs" where TOC items can be different if headings are in tabs for example? 🤔


    Note: another discussion requesting support for JSX expressions in TOC: #10353

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

    difficulty: advanceddomain: markdownfeature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions