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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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.

`;

exports[`admonitions remark plugin replace custom keyword 1`] = `
Expand Down
Expand Up @@ -149,8 +149,12 @@ const plugin: Plugin = function plugin(
!isSimpleTextTitle && {
type: admonitionNodeType,
data: {
hName: 'mdxAdmonitionTitle',
hProperties: {},
// Note: this has to be valid markup itself, because it's replaced
// by client code, not MDX loader, so React sees the content
hName: 'template',
hProperties: {
'data-admonition-title': true,
},
},
children: titleNodes,
},
Expand Down
20 changes: 13 additions & 7 deletions packages/docusaurus-theme-common/src/utils/admonitionUtils.tsx
Expand Up @@ -14,15 +14,21 @@ function extractMDXAdmonitionTitle(children: ReactNode): {
rest: ReactNode;
} {
const items = React.Children.toArray(children);
const mdxAdmonitionTitle = items.find(
(item) =>
React.isValidElement(item) &&
(item.props as {mdxType: string} | null)?.mdxType ===
'mdxAdmonitionTitle',
);
const mdxAdmonitionTitle = items.find((item) => {
if (!React.isValidElement(item)) {
return false;
}
const props = item.props as {
mdxType: string;
'data-admonition-title'?: boolean;
} | null;
return (
props?.mdxType === 'template' && props['data-admonition-title'] === true
);
}) as JSX.Element | undefined;
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

rest,
};
}
Expand Down