Minor refactor to markdown embed print (#18518)

This commit is contained in:
fisker Cheung authored and GitHub committed 2025-12-23 13:27:40 +08:00
1 parent cc243fe154
commit cd0c3292d3
1 file changed
+23 -15
+23 -15
View File
@@ -6,29 +6,39 @@ import { getFencedCodeBlockValue } from "./utilities.js";
function embed(path, options) {
const { node } = path;
if (node.type === "code" && node.lang !== null) {
const parser = inferParser(options, { language: node.lang });
if (parser) {
switch (node.type) {
case "code": {
const { lang: language } = node;
if (!language) {
return;
}
const parser = inferParser(options, { language });
if (!parser) {
return;
}
return async (textToDoc) => {
const styleUnit = options.__inJsTemplate ? "~" : "`";
const style = styleUnit.repeat(
Math.max(3, getMaxContinuousCount(node.value, styleUnit) + 1),
);
const newOptions = { parser };
const textToDocOptions = { parser };
// Override the filepath option.
// This is because whether the trailing comma of type parameters
// should be printed depends on whether it is `*.ts` or `*.tsx`.
// https://github.com/prettier/prettier/issues/15282
if (node.lang === "ts" || node.lang === "typescript") {
newOptions.filepath = "dummy.ts";
} else if (node.lang === "tsx") {
newOptions.filepath = "dummy.tsx";
if (language === "ts" || language === "typescript") {
textToDocOptions.filepath = "dummy.ts";
} else if (language === "tsx") {
textToDocOptions.filepath = "dummy.tsx";
}
const doc = await textToDoc(
getFencedCodeBlockValue(node, options.originalText),
newOptions,
textToDocOptions,
);
const styleUnit = options.__inJsTemplate ? "~" : "`";
const style = styleUnit.repeat(
Math.max(3, getMaxContinuousCount(node.value, styleUnit) + 1),
);
return markAsRoot([
@@ -42,9 +52,7 @@ function embed(path, options) {
]);
};
}
}
switch (node.type) {
// MDX
case "import":
case "export":