Skip to content

Commit 374365f

Browse files
authored
Fix some MDXv3 migration bugs (#270)
1 parent 43aa76d commit 374365f

7 files changed

Lines changed: 32 additions & 16 deletions

File tree

demo/docusaurus.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ const config = {
1818
organizationName: "cloud-annotations", // Usually your GitHub org/user name.
1919
projectName: "docusaurus-openapi", // Usually your repo name.
2020

21+
markdown: {
22+
mdx1Compat: {
23+
comments: false,
24+
admonitions: false,
25+
headingIds: false,
26+
},
27+
},
28+
2129
presets: [
2230
[
2331
"docusaurus-preset-openapi",

demo/examples/petstore.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ info:
3838
OAuth2 - an open protocol to allow secure authorization in a simple
3939
and standard method from web, mobile and desktop applications.
4040
41-
<SecurityDefinitions />
42-
4341
version: 1.0.0
4442
title: Swagger Petstore YAML
4543
termsOfService: "http://swagger.io/terms/"
@@ -442,7 +440,7 @@ paths:
442440
- store
443441
summary: Find purchase order by ID
444442
description: >-
445-
For valid response try integer IDs with value <= 5 or > 10. Other values
443+
For valid response try integer IDs with value \<= 5 or > 10. Other values
446444
will generated exceptions
447445
operationId: getOrderById
448446
parameters:

demo/examples/petstore/_spec_.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ info:
3838
OAuth2 - an open protocol to allow secure authorization in a simple
3939
and standard method from web, mobile and desktop applications.
4040
41-
<SecurityDefinitions />
42-
4341
version: 1.0.0
4442
title: Swagger Petstore YAML
4543
termsOfService: "http://swagger.io/terms/"
@@ -442,7 +440,7 @@ paths:
442440
- store
443441
summary: Find purchase order by ID
444442
description: >-
445-
For valid response try integer IDs with value <= 5 or > 10. Other values
443+
For valid response try integer IDs with value \<= 5 or > 10. Other values
446444
will generated exceptions
447445
operationId: getOrderById
448446
parameters:

packages/docusaurus-plugin-openapi/src/markdown/createDescription.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ export function createDescription(description: string | undefined) {
99
if (!description) {
1010
return "";
1111
}
12-
// Replace usages of <= or >= with \<= or \>= to avoid MDX3 parsing issues.
13-
return `\n\n${description.replace(/([<>]=?)/g, "\\$1")}\n\n`;
12+
return `\n\n${description}\n\n`;
1413
}

packages/docusaurus-plugin-openapi/src/markdown/createSchemaTable.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ export function createSchemaTable({ title, body, type, ...rest }: Props) {
242242
children: create("th", {
243243
style: { textAlign: "left" },
244244
children: [
245-
`${title} `,
245+
create("span", {
246+
children: `${title} `,
247+
}),
246248
...parseTitleLabel({ required: body.required, type }),
247249
create("div", {
248250
children: createDescription(body.description),

packages/docusaurus-plugin-openapi/src/markdown/createVersionBadge.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ export function createVersionBadge(version: string | undefined) {
1515
className: "theme-doc-version-badge badge badge--secondary",
1616
children: `Version: ${escape(version)}`,
1717
}),
18-
`\n\n`,
18+
`\n`,
1919
]);
2020
}

packages/docusaurus-plugin-openapi/src/markdown/utils.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ export function create(tag: string, props: Props): string {
1717
propString += ` ${key}={${JSON.stringify(value)}}`;
1818
}
1919

20-
return `<${tag}${propString}>
21-
${render(children)}
22-
</${tag}>`;
20+
return `<${tag}${propString}>${render(children)}</${tag}>`;
2321
}
2422

2523
export function guard<T>(
@@ -34,8 +32,21 @@ export function guard<T>(
3432
}
3533

3634
export function render(children: Children): string {
37-
if (Array.isArray(children)) {
38-
return children.filter((c) => c !== undefined).join("\n");
35+
const res = Array.isArray(children)
36+
? children.filter((c) => c !== undefined).join("\n")
37+
: children ?? "";
38+
39+
const isMultiline = res.split("\n").length > 1;
40+
41+
// It is not possible to wrap “blocks” if text and tags are on the same line,
42+
// but the corresponding tags are on different lines. This can accidentally
43+
// happen if the rendered item has multiple lines. To be safe, we pad with
44+
// newlines.
45+
//
46+
// See: https://mdxjs.com/migrating/v2/#jsx
47+
if (isMultiline) {
48+
return "\n" + res + "\n";
3949
}
40-
return children ?? "";
50+
51+
return res;
4152
}

0 commit comments

Comments
 (0)