Skip to content

Commit 61de849

Browse files
committed
Fix custom mdxJsxFlowElement handler: use default handler for cases which it should bypasses
1 parent 3bfe5f5 commit 61de849

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

scripts/migration/src/index.ts

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { fromMarkdown } from "mdast-util-from-markdown";
22
import { toMarkdown } from "mdast-util-to-markdown";
33
import { mdxFromMarkdown, mdxToMarkdown } from "mdast-util-mdx";
4+
import { mdxJsxToMarkdown } from "mdast-util-mdx-jsx";
45
import { mdxjs } from "micromark-extension-mdxjs";
56
import { directive } from "micromark-extension-directive";
67
import { directiveFromMarkdown, directiveToMarkdown } from "mdast-util-directive";
@@ -108,6 +109,11 @@ async function processFile(filePath: string, options: ExtendedTransformerOptions
108109
}
109110

110111
// Convert AST back to MDX
112+
const defaultMdxJsxFlowElement = mdxJsxToMarkdown().handlers?.mdxJsxFlowElement;
113+
if (!defaultMdxJsxFlowElement) {
114+
throw new Error("Default handler for mdxJsxFlowElement not found");
115+
}
116+
111117
const newContent = toMarkdown(ast, {
112118
extensions: [
113119
mdxToMarkdown(),
@@ -166,27 +172,25 @@ async function processFile(filePath: string, options: ExtendedTransformerOptions
166172
return text;
167173
}) as Handle,
168174

169-
// Custom handler to prevent root child indentation of FileTree and Steps components.
170-
// Unfortunately this messes with all other components and will not render them correctly despite the conditional statement.
171-
172-
// mdxJsxFlowElement: ((node, parent, context: State) => {
173-
// if (node.name === "FileTree" || node.name === "Steps") {
174-
// const exit = context.enter("mdxJsxFlowElement");
175-
// const nodeContent = node.children
176-
// .map((child: any) =>
177-
// context.handle(child, node, context, {
178-
// after: "",
179-
// before: "",
180-
// lineShift: 0,
181-
// now: { line: 1, column: 1 },
182-
// }),
183-
// )
184-
// .join("\n");
185-
// exit();
186-
// return `<${node.name}>\n\n${nodeContent}\n\n</${node.name}>`;
187-
// }
188-
// return null;
189-
// }) as Handle,
175+
mdxJsxFlowElement: (node, parent, context, info) => {
176+
if (node.name === "FileTree" || node.name === "Steps") {
177+
const exit = context.enter("mdxJsxFlowElement");
178+
const nodeContent = node.children
179+
.map((child: any) =>
180+
context.handle(child, node, context, {
181+
after: "",
182+
before: "",
183+
lineShift: 0,
184+
now: { line: 1, column: 1 },
185+
}),
186+
)
187+
.join("\n");
188+
exit();
189+
return `<${node.name}>\n\n${nodeContent}\n\n</${node.name}>`;
190+
}
191+
192+
return defaultMdxJsxFlowElement(node, parent, context, info);
193+
},
190194
},
191195
});
192196

0 commit comments

Comments
 (0)