Skip to content

Commit 659cd72

Browse files
committed
Minor refactor to markdown embed print
1 parent d3e3025 commit 659cd72

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/language-markdown/embed.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,39 @@ import { getFencedCodeBlockValue } from "./utilities.js";
66
function embed(path, options) {
77
const { node } = path;
88

9-
if (node.type === "code" && node.lang !== null) {
10-
const parser = inferParser(options, { language: node.lang });
11-
if (parser) {
9+
switch (node.type) {
10+
case "code": {
11+
const { lang: language } = node;
12+
if (!language) {
13+
return;
14+
}
15+
16+
const parser = inferParser(options, { language });
17+
if (!parser) {
18+
return;
19+
}
20+
1221
return async (textToDoc) => {
13-
const styleUnit = options.__inJsTemplate ? "~" : "`";
14-
const style = styleUnit.repeat(
15-
Math.max(3, getMaxContinuousCount(node.value, styleUnit) + 1),
16-
);
17-
const newOptions = { parser };
22+
const textToDocOptions = { parser };
1823

1924
// Override the filepath option.
2025
// This is because whether the trailing comma of type parameters
2126
// should be printed depends on whether it is `*.ts` or `*.tsx`.
2227
// https://github.com/prettier/prettier/issues/15282
23-
if (node.lang === "ts" || node.lang === "typescript") {
24-
newOptions.filepath = "dummy.ts";
25-
} else if (node.lang === "tsx") {
26-
newOptions.filepath = "dummy.tsx";
28+
if (language === "ts" || language === "typescript") {
29+
textToDocOptions.filepath = "dummy.ts";
30+
} else if (language === "tsx") {
31+
textToDocOptions.filepath = "dummy.tsx";
2732
}
2833

2934
const doc = await textToDoc(
3035
getFencedCodeBlockValue(node, options.originalText),
31-
newOptions,
36+
textToDocOptions,
37+
);
38+
39+
const styleUnit = options.__inJsTemplate ? "~" : "`";
40+
const style = styleUnit.repeat(
41+
Math.max(3, getMaxContinuousCount(node.value, styleUnit) + 1),
3242
);
3343

3444
return markAsRoot([
@@ -42,9 +52,7 @@ function embed(path, options) {
4252
]);
4353
};
4454
}
45-
}
4655

47-
switch (node.type) {
4856
// MDX
4957
case "import":
5058
case "export":

0 commit comments

Comments
 (0)