diff --git a/src/language-markdown/embed.js b/src/language-markdown/embed.js index b00b7903b..ab0967330 100644 --- a/src/language-markdown/embed.js +++ b/src/language-markdown/embed.js @@ -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":