Skip to content

Commit

Permalink
fix(mdx-loader): mdx-code-block should support CRLF (#9897)
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Feb 29, 2024
1 parent 0589b14 commit 2152572
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
31 changes: 31 additions & 0 deletions packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,37 @@ describe('unwrapMdxCodeBlocks', () => {
`);
});

it('can unwrap a simple mdx code block with CRLF', () => {
// Note: looks like string dedent mess up with \r
expect(
unwrapMdxCodeBlocks(`
# Title\r
\`\`\`mdx-code-block\r
import Comp, {User} from "@site/components/comp"\r
\r
<Comp prop="test">\r
<User user={{firstName: "Sébastien"}} />\r
</Comp>\r
\r
export const age = 36\r
\`\`\`\r
\r
text\r
`),
).toBe(`
# Title\r
import Comp, {User} from "@site/components/comp"\r
\r
<Comp prop="test">\r
<User user={{firstName: "Sébastien"}} />\r
</Comp>\r
\r
export const age = 36\r
\r
text\r
`);
});

it('can unwrap a nested mdx code block', () => {
expect(
unwrapMdxCodeBlocks(dedent`
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus-utils/src/markdownUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
/(?<begin>^|\n)```(?<spaces>\x20*)mdx-code-block\n(?<children>.*?)\n```(?<end>\n|$)/gs;
/(?<begin>^|\r?\n)```(?<spaces>\x20*)mdx-code-block\r?\n(?<children>.*?)\r?\n```(?<end>\r?\n|$)/gs;
const regexp4 =
/(?<begin>^|\n)````(?<spaces>\x20*)mdx-code-block\n(?<children>.*?)\n````(?<end>\n|$)/gs;
/(?<begin>^|\r?\n)````(?<spaces>\x20*)mdx-code-block\r?\n(?<children>.*?)\r?\n````(?<end>\r?\n|$)/gs;

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

0 comments on commit 2152572

Please sign in to comment.