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