Replies: 1 comment
-
This is a behavior that comes directly from MDX, that you should eventually discuss on the MDX repo directly. Turns out this has already been reported multiple times, here are just a few references:
We don't want to diverge from standard MDX, but technically you could decide to write your own little remark plugin that de-duplicates the What you want is to transform this AST: {
"type": "mdxJsxFlowElement",
"name": "p",
"attributes": [],
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "text"
}
]
}
],
} To just this: {
"type": "mdxJsxFlowElement",
"name": "p",
"attributes": [],
"children": [
{
"type": "text",
"value": "text"
}
],
} Here's some pseudo-code to give you an idea of the remark plugin you could write: import {visitParents, SKIP} from 'unist-util-visit-parents'
const updatedTree = visitParents(tree, 'p', (p, parents) => {
const parent = parents[0];
if (parent.type === 'mdxJsxFlowElement' && parent.name === "p") {
parent.children = p.children;
return SKIP;
}
}) Note our new HTML minifier coming with v3.6 will report these HTML markup problems. In my troubleshooting guide I gave another workaround based on React fragments: #10580 This does not duplicate paragraphs: <p>
<>content</>
</p> |
Beta Was this translation helpful? Give feedback.
-
I noticed that when I add MDX documents to the Docusaurus blog, if I include a
<p>
tag with child elements and line breaks, the text content inside gets wrapped with an additional<p>
tag.Example:
It will be rendered as:
This causes the paragraph, which should render on a single line, to be split into multiple lines due to the nested
<p>
tags.I know I can edit the MDX manually to keep the text within the
<p>
tag on a single line, like this:Writing the content of the
<p>
tag on a single line prevents the automatic addition of nested<p>
tags, allowing it to render correctly:The reason I haven't done this is that my entire MDX document is created by saving a webpage as a single MHTML file, then converting it to MDX using a Python script. I use BeautifulSoup4 for this process, and to improve the readability of the converted HTML, I call soup.prettify(), which formats each tag on a new line. This results in the extra
<p>
tags, as shown in my initial MDX code block. If I skip soup.prettify() and simply save str(soup), the HTML content in the MDX file is all on a single line, which makes it hard to read and maintain. I tried filtering out<p>
tags in the script and merging their content onto a single line, but since the final step still requires converting soup to a string, I either end up with prettify() and lose the merging, or I keep everything on a single line, sacrificing readability.My python script to merge
<p>
tags:Is there a better solution, such as preventing MDX from automatically adding nested
<p>
tags?Beta Was this translation helpful? Give feedback.
All reactions