diff --git a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts
index 016f89688faa..96f94fa59536 100644
--- a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts
+++ b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts
@@ -1229,6 +1229,36 @@ text\r
`);
});
+ it('can unwrap indented mdx code block', () => {
+ expect(
+ unwrapMdxCodeBlocks(dedent`
+ # Title
+
+
+ \`\`\`mdx-code-block
+ content
+ \`\`\`
+
+
+ \`\`\`\`mdx-code-block
+ content2
+ \`\`\`\`
+
+ text
+ `),
+ ).toEqual(dedent`
+ # Title
+
+
+ content
+
+
+ content2
+
+ text
+ `);
+ });
+
it('works for realistic example', () => {
expect(
unwrapMdxCodeBlocks(dedent`
diff --git a/packages/docusaurus-utils/src/markdownUtils.ts b/packages/docusaurus-utils/src/markdownUtils.ts
index 2fd563c3bb10..1fe1a73e8782 100644
--- a/packages/docusaurus-utils/src/markdownUtils.ts
+++ b/packages/docusaurus-utils/src/markdownUtils.ts
@@ -70,9 +70,9 @@ export function escapeMarkdownHeadingIds(content: string): string {
export function unwrapMdxCodeBlocks(content: string): string {
// We only support 3/4 backticks on purpose, should be good enough
const regexp3 =
- /(?^|\r?\n)```(?\x20*)mdx-code-block\r?\n(?.*?)\r?\n```(?\r?\n|$)/gs;
+ /(?^|\r?\n)(?\x20*)```(?\x20*)mdx-code-block\r?\n(?.*?)\r?\n(?\x20*)```(?\r?\n|$)/gs;
const regexp4 =
- /(?^|\r?\n)````(?\x20*)mdx-code-block\r?\n(?.*?)\r?\n````(?\r?\n|$)/gs;
+ /(?^|\r?\n)(?\x20*)````(?\x20*)mdx-code-block\r?\n(?.*?)\r?\n(?\x20*)````(?\r?\n|$)/gs;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const replacer = (substring: string, ...args: any[]) => {