Extract code for markdown code print (#19982)
This commit is contained in:
1 parent
a1364d3c3b
commit
1f943d88f8
3 files changed
+58
-39
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { hardline, markAsRoot, replaceEndOfLine } from "../document/index.js";
|
||||
import getMaxContinuousCount from "../utilities/get-max-continuous-count.js";
|
||||
import inferParser from "../utilities/infer-parser.js";
|
||||
import { printCodeFences } from "./print/code.js";
|
||||
import { getFencedCodeBlockValue } from "./utilities.js";
|
||||
|
||||
function embed(path, options) {
|
||||
@@ -8,8 +8,8 @@ function embed(path, options) {
|
||||
|
||||
switch (node.type) {
|
||||
case "code": {
|
||||
const { lang: language } = node;
|
||||
if (!language) {
|
||||
const { isIndented, lang: language } = node;
|
||||
if (isIndented || !language) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -47,10 +47,7 @@ function embed(path, options) {
|
||||
textToDocOptions,
|
||||
);
|
||||
|
||||
const styleUnit = options.__inJsTemplate ? "~" : "`";
|
||||
const style = styleUnit.repeat(
|
||||
Math.max(3, getMaxContinuousCount(node.value, styleUnit) + 1),
|
||||
);
|
||||
const style = printCodeFences(path, options);
|
||||
|
||||
return markAsRoot([
|
||||
style,
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import { align, hardline, replaceEndOfLine } from "../../document/index.js";
|
||||
import getMaxContinuousCount from "../../utilities/get-max-continuous-count.js";
|
||||
import { getFencedCodeBlockValue } from "../utilities.js";
|
||||
|
||||
function printCodeFences(path, options) {
|
||||
const styleUnit = options.__inJsTemplate ? "~" : "`";
|
||||
|
||||
return styleUnit.repeat(
|
||||
Math.max(3, getMaxContinuousCount(path.node.value, styleUnit) + 1),
|
||||
);
|
||||
}
|
||||
|
||||
function printFencedCodeBlock(path, options) {
|
||||
const { node } = path;
|
||||
|
||||
const styleUnit = options.__inJsTemplate ? "~" : "`";
|
||||
const style = styleUnit.repeat(
|
||||
Math.max(3, getMaxContinuousCount(node.value, styleUnit) + 1),
|
||||
);
|
||||
|
||||
return [
|
||||
style,
|
||||
node.lang || "",
|
||||
node.meta ? " " + node.meta : "",
|
||||
hardline,
|
||||
replaceEndOfLine(
|
||||
options.parser === "mdx"
|
||||
? getFencedCodeBlockValue(node, options.originalText)
|
||||
: node.value,
|
||||
hardline,
|
||||
),
|
||||
hardline,
|
||||
style,
|
||||
];
|
||||
}
|
||||
|
||||
function printCode(path, options) {
|
||||
const { node } = path;
|
||||
|
||||
if (node.isIndented) {
|
||||
const alignment = " ".repeat(4);
|
||||
return align(alignment, [
|
||||
alignment,
|
||||
replaceEndOfLine(node.value, hardline),
|
||||
]);
|
||||
}
|
||||
|
||||
return printFencedCodeBlock(path, options);
|
||||
}
|
||||
|
||||
export { printCode, printCodeFences };
|
||||
@@ -13,19 +13,18 @@ import {
|
||||
replaceEndOfLine,
|
||||
softline,
|
||||
} from "../../document/index.js";
|
||||
import getMaxContinuousCount from "../../utilities/get-max-continuous-count.js";
|
||||
import getMinNotPresentContinuousCount from "../../utilities/get-min-not-present-continuous-count.js";
|
||||
import { getPreferredQuote } from "../../utilities/get-preferred-quote.js";
|
||||
import UnexpectedNodeError from "../../utilities/unexpected-node-error.js";
|
||||
import { locEnd, locStart } from "../loc.js";
|
||||
import {
|
||||
getFencedCodeBlockValue,
|
||||
getNthListSiblingIndex,
|
||||
isAutolink,
|
||||
isPrettierIgnore,
|
||||
splitText,
|
||||
} from "../utilities.js";
|
||||
import { printChildren } from "./children.js";
|
||||
import { printCode } from "./code.js";
|
||||
import { printHeading } from "./heading.js";
|
||||
import { printList, printListLegacy } from "./list.js";
|
||||
import { printParagraph } from "./paragraph.js";
|
||||
@@ -213,36 +212,8 @@ function printMdast(path, options, print) {
|
||||
return ["> ", align("> ", printChildren(path, options, print))];
|
||||
case "heading":
|
||||
return printHeading(path, options, print);
|
||||
case "code": {
|
||||
if (node.isIndented) {
|
||||
// indented code block
|
||||
const alignment = " ".repeat(4);
|
||||
return align(alignment, [
|
||||
alignment,
|
||||
replaceEndOfLine(node.value, hardline),
|
||||
]);
|
||||
}
|
||||
|
||||
// fenced code block
|
||||
const styleUnit = options.__inJsTemplate ? "~" : "`";
|
||||
const style = styleUnit.repeat(
|
||||
Math.max(3, getMaxContinuousCount(node.value, styleUnit) + 1),
|
||||
);
|
||||
return [
|
||||
style,
|
||||
node.lang || "",
|
||||
node.meta ? " " + node.meta : "",
|
||||
hardline,
|
||||
replaceEndOfLine(
|
||||
options.parser === "mdx"
|
||||
? getFencedCodeBlockValue(node, options.originalText)
|
||||
: node.value,
|
||||
hardline,
|
||||
),
|
||||
hardline,
|
||||
style,
|
||||
];
|
||||
}
|
||||
case "code":
|
||||
return printCode(path, options);
|
||||
case "html": {
|
||||
const { parent, isLast } = path;
|
||||
const value =
|
||||
|
||||
Reference in new issue
Block a user