withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
46.81k stars 2.49k forks source link

Bug with MDX optimization - `set:html=""` #11971

Closed stereobooster closed 2 months ago

stereobooster commented 2 months ago

Astro Info

Astro                    v4.15.4
Node                     v18.17.1
System                   macOS (x64)
Package Manager          yarn
Output                   static
Adapter                  none
Integrations             @astrojs/starlight
                         @astrojs/mdx

If this issue only occurs in one browser, which browser is a problem?

n/a

Describe the Bug

When mdx optimization turned on (default in starlight 0.23+) it seems it doesn't correctly render tag in MDX

<figure class="beoe mermaid not-content" set:html="<div></div>"></figure>

I know that documentation says that it may break behavior of custom tags. But I think in this specific case this is a bug - all html is there, it just "forgot" to move content from set:html inside the tag

Related https://github.com/stereobooster/beoe/issues/2

What's the expected result?

So it would actually output contents from set:html inside the tag

Link to Minimal Reproducible Example

https://github.com/jarodtaylor/starlight-mermaid

Participation

bluwy commented 2 months ago

I've sent a fix for this, however there's a bug in @beoe/rehype-mermaid that is injecting root hast nodes inside a tree, which is incorrect following the spec:

Root can be used as the root of a tree, or as a value of the content field on a 'template' Element, never as a child.

And it trips up the MDX optimization as it didn't expect this case. However, since MDX still handles fine with it, I fixed it anyway to not cause any inconsistencies.

Still, it would be best if you update your plugin to not inject the root node. You can take its children and inject that instead. I tested with the main rehype-mermaid package and it didn't have this issue.

stereobooster commented 2 months ago

Root can be used as the root of a tree, or as a value of the content field on a 'template' https://github.com/withastro/astro/issues/11971#element, never as a child.

Makes sense. Thanks for pointing out this issue - I will fix it