-
Notifications
You must be signed in to change notification settings - Fork 16
fix: resolve variables in TOC headings for MDX and MDXish #1292
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
base: next
Are you sure you want to change the base?
fix: resolve variables in TOC headings for MDX and MDXish #1292
Conversation
lib/plain.ts
Outdated
| variables?: Record<string, string>; | ||
| } | ||
|
|
||
| interface MdxExpressionNode { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this structure from a library? If so I wonder if we can just import it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are full types available from mdast-util-mdx-expression (MdxTextExpressionHast) and estree-jsx (MemberExpression, Identifier, etc.), but using them requires more verbose type narrowing since ESTree types are unions that can represent any JS expression, not just user.name. This interface would document exactly the shape we expect and avoids multiple type assertions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How much verbose it would be? I'm just worried we'll accidentally access some attribute that we didn't account for, or the type from the library changed unknowingly and we would have to update this. Could you explain how we get this type, or provide a link to the type from the library this is based on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The node structure comes from two libraries:
-
mdast-util-mdx-expression- defines MdxTextExpressionHast for the node wrapper
npm | types source -
estree-jsx(ESTree spec) - defines the Program, MemberExpression, Identifier structure inside data.estree
npm | ESTree spec
But yea it would be more maintainable using library types, have refactored it!
Variables like {user.name} in headings were not resolving in the Table
of Contents, showing blank or the raw expression instead of the value.
Changes:
- Add ESTree AST inspection in plain.ts to resolve MDX expressions
(matches approach in processor/transform/variables.ts)
- Handle lowercase 'variable' tagName for MDXish (rehypeRaw normalizes
custom element names to lowercase)
- Flatten variables in tocToHast() to use user values or default
The fix ensures both rendering paths resolve variables:
- MDX: mdxTextExpression nodes → ESTree AST inspection
- MDXish: <variable> elements → tagName switch case
Adds tests for variable resolution in TOC headings for both MDX and
MDXish rendering paths.
Replace custom MdxExpressionNode interface with proper types from mdast-util-mdx-expression and estree for better type safety and maintainability.
cfd37a3 to
f3430f9
Compare
🧰 Changes
Variables like {user.name} in headings were not resolving in the Table of Contents, showing blank or the raw expression instead of the value.
Changes:
The fix ensures both rendering paths resolve variables:
🧬 QA & Testing
Adds tests for variable resolution in TOC headings for both MDX and MDXish rendering paths.