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

fix(mdx-loader): properly unwrap mdxAdmonitionTitle placeholder #8246

Merged
merged 2 commits into from Oct 26, 2022

Conversation

Josh-Cena
Copy link
Collaborator

@Josh-Cena Josh-Cena commented Oct 25, 2022

Pre-flight checklist

Motivation

This is the best fix I can think of—a <template> element seems to be semantically the best fit here, since we are essentially having a "thunk" of HTML elements and we don't care what's in it, just need to transfer it elsewhere to be rendered.

Test Plan

Test links

Deploy preview: https://deploy-preview-8246--docusaurus-2.netlify.app/tests/pages/markdownPageTests#admonitions

You would probably need to test this locally. Go to http://localhost:3000/tests/pages/markdownPageTests#admonitions, and see in the console that the warning (which can be reproduced on main) is gone.

Related issues/PRs

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Oct 25, 2022
@Josh-Cena Josh-Cena added pr: bug fix This PR fixes a bug in a past release. to backport This PR is planned to be backported to a stable version of Docusaurus labels Oct 25, 2022
@@ -51,7 +51,7 @@ exports[`admonitions remark plugin default behavior for custom keyword 1`] = `

exports[`admonitions remark plugin interpolation 1`] = `
"<p>Test admonition with interpolated title/body</p>
<admonition type="tip"><mdxAdmonitionTitle>My <code>interpolated</code> <strong>title</strong> &#x3C;button style={{color: "red"}} onClick={() => alert("click")}>test</mdxAdmonitionTitle><p><code>body</code> <strong>interpolated</strong> content</p></admonition>"
<admonition type="tip"><template data-admonition-title></template><p><code>body</code> <strong>interpolated</strong> content</p></admonition>"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to be a Remark bug that the template content does not get serialized 🤔 The rendered output is fine, though. I'm not sure I should change the tag name just to make testing easier.

@netlify
Copy link

netlify bot commented Oct 25, 2022

[V2]

Name Link
🔨 Latest commit 037606a
🔍 Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/63583be9f252e20009e23821
😎 Deploy Preview https://deploy-preview-8246--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions
Copy link

github-actions bot commented Oct 25, 2022

⚡️ Lighthouse report for the deploy preview of this PR

URL Performance Accessibility Best Practices SEO PWA Report
/ 🟢 91 🟢 97 🟢 100 🟢 100 🟢 90 Report
/docs/installation 🟠 77 🟢 100 🟢 100 🟢 100 🟢 90 Report

@netlify
Copy link

netlify bot commented Oct 25, 2022

[V2]

Name Link
🔨 Latest commit f4d7d47
🔍 Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/63595b299713d80008c0de09
😎 Deploy Preview https://deploy-preview-8246--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions
Copy link

github-actions bot commented Oct 25, 2022

Size Change: 0 B

Total Size: 819 kB

ℹ️ View Unchanged
Filename Size
website/.docusaurus/globalData.json 52.6 kB
website/build/assets/css/styles.********.css 112 kB
website/build/assets/js/main.********.js 614 kB
website/build/index.html 40.8 kB

compressed-size-action

@slorber
Copy link
Collaborator

slorber commented Oct 26, 2022

Dogfood page: http://localhost:3000/docs/markdown-features/diagrams

Another fix I find better (no remark bug at least) that also works is to use uppercase component name and add a placeholder component to MDXComponents:

MDXAdmonitionTitle: (props) => <React.Fragment {...props} />,

What I'd like to understand first is why we have the problem on this specific case, and not on all other lowercase MDX elements like admonition, mermaid, highlight...

I think I saw this warning in the past in other cases but can't remember.

I guess it's likely related to the order in which remark plugins are applied in mdx compiler itself. I'm not sure our test snapshots include the exact same processing pipeline compared to MDX so it's hard to debug.

@Josh-Cena
Copy link
Collaborator Author

Josh-Cena commented Oct 26, 2022

Other lowercase names have their corresponding MDX component mappings, so MDX is able to please React about these tags. OTOH <mdxAdmonitionTitle> is dynamically replaced after React renders with our custom client code, so React does see this tag for one instant, and it's unhappy. I thought about providing the dummy mapping to React.Fragment, but I don't like because it's difficult to explain to users after it becomes part of the public API surface through swizzling (and it will be swizzled a lot).

const rest = <>{items.filter((item) => item !== mdxAdmonitionTitle)}</>;
return {
mdxAdmonitionTitle,
mdxAdmonitionTitle: mdxAdmonitionTitle?.props.children,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey

Actually the template trick seems un-necessary: you just found the issue here

See prod markup:

https://docusaurus.io/tests/pages/markdownPageTests#admonitions

CleanShot 2022-10-26 at 17 58 28@2x

The utils code does not unwrap the element and still let it pass through up to the React reconcilier (which emits the warning)

Just adding this line fixes the issue

Copy link
Collaborator Author

@Josh-Cena Josh-Cena Oct 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, great find! Let me try

@Josh-Cena Josh-Cena changed the title fix(mdx-loader): use valid markup for mdxAdmonitionTitle placeholder fix(mdx-loader): properly unwrap mdxAdmonitionTitle Oct 26, 2022
@Josh-Cena Josh-Cena changed the title fix(mdx-loader): properly unwrap mdxAdmonitionTitle fix(mdx-loader): properly unwrap mdxAdmonitionTitle placeholder Oct 26, 2022
@Josh-Cena
Copy link
Collaborator Author

OK I should have checked dev tools. I was overthinking it

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄 didn't expect such a basic mistake either.

LGTM

@Josh-Cena Josh-Cena merged commit e301523 into main Oct 26, 2022
@Josh-Cena Josh-Cena deleted the fix-admonition-title branch October 26, 2022 16:52
slorber pushed a commit that referenced this pull request Oct 28, 2022
# Conflicts:
#	packages/docusaurus-theme-common/src/utils/admonitionUtils.tsx
@slorber slorber added backported This PR has been backported to a stable version of Docusaurus and removed to backport This PR is planned to be backported to a stable version of Docusaurus labels Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backported This PR has been backported to a stable version of Docusaurus CLA Signed Signed Facebook CLA pr: bug fix This PR fixes a bug in a past release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Admonitions with backticks elicit <mdxAdmonitionTitle /> warnings
3 participants