Skip to content

Commit 543607d

Browse files
committed
support indented mdx code blocks
1 parent 2eecd70 commit 543607d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,36 @@ text\r
12291229
`);
12301230
});
12311231

1232+
it('can unwrap indented mdx code block', () => {
1233+
expect(
1234+
unwrapMdxCodeBlocks(dedent`
1235+
# Title
1236+
1237+
<div>
1238+
\`\`\`mdx-code-block
1239+
content
1240+
\`\`\`
1241+
</div>
1242+
1243+
\`\`\`\`mdx-code-block
1244+
content2
1245+
\`\`\`\`
1246+
1247+
text
1248+
`),
1249+
).toEqual(dedent`
1250+
# Title
1251+
1252+
<div>
1253+
content
1254+
</div>
1255+
1256+
content2
1257+
1258+
text
1259+
`);
1260+
});
1261+
12321262
it('works for realistic example', () => {
12331263
expect(
12341264
unwrapMdxCodeBlocks(dedent`

packages/docusaurus-utils/src/markdownUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ export function escapeMarkdownHeadingIds(content: string): string {
7070
export function unwrapMdxCodeBlocks(content: string): string {
7171
// We only support 3/4 backticks on purpose, should be good enough
7272
const regexp3 =
73-
/(?<begin>^|\r?\n)```(?<spaces>\x20*)mdx-code-block\r?\n(?<children>.*?)\r?\n```(?<end>\r?\n|$)/gs;
73+
/(?<begin>^|\r?\n)(?<indentStart>\x20*)```(?<spaces>\x20*)mdx-code-block\r?\n(?<children>.*?)\r?\n(?<indentEnd>\x20*)```(?<end>\r?\n|$)/gs;
7474
const regexp4 =
75-
/(?<begin>^|\r?\n)````(?<spaces>\x20*)mdx-code-block\r?\n(?<children>.*?)\r?\n````(?<end>\r?\n|$)/gs;
75+
/(?<begin>^|\r?\n)(?<indentStart>\x20*)````(?<spaces>\x20*)mdx-code-block\r?\n(?<children>.*?)\r?\n(?<indentEnd>\x20*)````(?<end>\r?\n|$)/gs;
7676

7777
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7878
const replacer = (substring: string, ...args: any[]) => {

0 commit comments

Comments
 (0)