From ebab8fc7dbc4d37031cc2d4b2d2149ba02d3d3b9 Mon Sep 17 00:00:00 2001
From: eitsupi <50911393+eitsupi@users.noreply.github.com>
Date: Wed, 24 Jan 2024 22:12:54 +0900
Subject: [PATCH] fix(mdx-loader): allow spaces before `mdx-code-block` info
string (#9776)
---
.../src/__tests__/markdownUtils.test.ts | 32 +++++++++++++++++++
.../docusaurus-utils/src/markdownUtils.ts | 4 +--
2 files changed, 34 insertions(+), 2 deletions(-)
diff --git a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts
index 0e04dbf5c2..734baea200 100644
--- a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts
+++ b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts
@@ -1251,6 +1251,38 @@ describe('unwrapMdxCodeBlocks', () => {
`);
});
+
+ it('allow spaces before mdx-code-block info string', () => {
+ expect(
+ unwrapMdxCodeBlocks(dedent`
+ # Title
+
+ \`\`\` mdx-code-block
+ import Comp, {User} from "@site/components/comp"
+
+
+
+
+
+ export const age = 36
+ \`\`\`
+
+ text
+ `),
+ ).toEqual(dedent`
+ # Title
+
+ import Comp, {User} from "@site/components/comp"
+
+
+
+
+
+ export const age = 36
+
+ text
+ `);
+ });
});
describe('admonitionTitleToDirectiveLabel', () => {
diff --git a/packages/docusaurus-utils/src/markdownUtils.ts b/packages/docusaurus-utils/src/markdownUtils.ts
index 87aac88f09..ce0766de6c 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 =
- /(?^|\n)```mdx-code-block\n(?.*?)\n```(?\n|$)/gs;
+ /(?^|\n)```(?\x20*)mdx-code-block\n(?.*?)\n```(?\n|$)/gs;
const regexp4 =
- /(?^|\n)````mdx-code-block\n(?.*?)\n````(?\n|$)/gs;
+ /(?^|\n)````(?\x20*)mdx-code-block\n(?.*?)\n````(?\n|$)/gs;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const replacer = (substring: string, ...args: any[]) => {