fix(mdx-loader): allow spaces before mdx-code-block info string (#9776)

This commit is contained in:
eitsupi 2024-01-24 22:12:54 +09:00 committed by GitHub
parent c827b6de2d
commit 0266549a51
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 2 deletions

View file

@ -1251,6 +1251,38 @@ describe('unwrapMdxCodeBlocks', () => {
</VersionsProvider> </VersionsProvider>
`); `);
}); });
it('allow spaces before mdx-code-block info string', () => {
expect(
unwrapMdxCodeBlocks(dedent`
# Title
\`\`\` mdx-code-block
import Comp, {User} from "@site/components/comp"
<Comp prop="test">
<User user={{firstName: "Sébastien"}} />
</Comp>
export const age = 36
\`\`\`
text
`),
).toEqual(dedent`
# Title
import Comp, {User} from "@site/components/comp"
<Comp prop="test">
<User user={{firstName: "Sébastien"}} />
</Comp>
export const age = 36
text
`);
});
}); });
describe('admonitionTitleToDirectiveLabel', () => { describe('admonitionTitleToDirectiveLabel', () => {

View file

@ -70,9 +70,9 @@ export function escapeMarkdownHeadingIds(content: string): string {
export function unwrapMdxCodeBlocks(content: string): string { export function unwrapMdxCodeBlocks(content: string): string {
// We only support 3/4 backticks on purpose, should be good enough // We only support 3/4 backticks on purpose, should be good enough
const regexp3 = const regexp3 =
/(?<begin>^|\n)```mdx-code-block\n(?<children>.*?)\n```(?<end>\n|$)/gs; /(?<begin>^|\n)```(?<spaces>\x20*)mdx-code-block\n(?<children>.*?)\n```(?<end>\n|$)/gs;
const regexp4 = const regexp4 =
/(?<begin>^|\n)````mdx-code-block\n(?<children>.*?)\n````(?<end>\n|$)/gs; /(?<begin>^|\n)````(?<spaces>\x20*)mdx-code-block\n(?<children>.*?)\n````(?<end>\n|$)/gs;
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
const replacer = (substring: string, ...args: any[]) => { const replacer = (substring: string, ...args: any[]) => {