Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
7 tasks done
anatolykopyl opened this issue Jan 21, 2024 · 2 comments · May be fixed by #9773
Open
7 tasks done
Labels
difficulty: advanced Issues that are complex, e.g. large scoping for long-term maintainability. domain: markdown Related to Markdown parsing or syntax feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future.

Comments

@anatolykopyl
Copy link
Contributor

Have you read the Contributing Guidelines on issues?

Prerequisites

  • I'm using the latest version of Docusaurus.
  • I have tried the npm run clear or yarn clear command.
  • I have tried rm -rf node_modules yarn.lock package-lock.json and re-installing packages.
  • I have tried creating a repro with https://new.docusaurus.io.
  • I have read the console error message carefully (if applicable).

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.
@anatolykopyl anatolykopyl added bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels Jan 21, 2024
@Josh-Cena
Copy link
Collaborator

Josh-Cena commented Mar 3, 2024

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" })]?

@Josh-Cena Josh-Cena added difficulty: advanced Issues that are complex, e.g. large scoping for long-term maintainability. domain: markdown Related to Markdown parsing or syntax feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future. and removed status: needs triage This issue has not been triaged by maintainers bug An error in the Docusaurus core causing instability or issues with its execution labels Mar 3, 2024
@Josh-Cena Josh-Cena 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 Mar 3, 2024
@Josh-Cena Josh-Cena changed the title TOC should render interpolated expressions in headings TOC should render statically analyzable embedded expressions in headings Mar 3, 2024
@Josh-Cena Josh-Cena changed the title TOC should render statically analyzable embedded expressions in headings TOC should render statically analyzable embedded expressions in headings (esp. imported partials) Mar 3, 2024
@slorber
Copy link
Collaborator

slorber commented Mar 4, 2024

👍

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
Labels
difficulty: advanced Issues that are complex, e.g. large scoping for long-term maintainability. domain: markdown Related to Markdown parsing or syntax feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future.
Projects
None yet
4 participants
@slorber @anatolykopyl @Josh-Cena and others