From 1f943d88f8c5d2fce299e765859cd2425cef4771 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 4 Sep 2026 17:25:30 +0800 Subject: [PATCH] Extract code for markdown `code` print (#19982) --- src/language-markdown/embed.js | 11 +++--- src/language-markdown/print/code.js | 51 ++++++++++++++++++++++++++++ src/language-markdown/print/mdast.js | 35 ++----------------- 3 files changed, 58 insertions(+), 39 deletions(-) create mode 100644 src/language-markdown/print/code.js diff --git a/src/language-markdown/embed.js b/src/language-markdown/embed.js index fa1a86ee6..1701fbd56 100644 --- a/src/language-markdown/embed.js +++ b/src/language-markdown/embed.js @@ -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, diff --git a/src/language-markdown/print/code.js b/src/language-markdown/print/code.js new file mode 100644 index 000000000..df19a6e0e --- /dev/null +++ b/src/language-markdown/print/code.js @@ -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 }; diff --git a/src/language-markdown/print/mdast.js b/src/language-markdown/print/mdast.js index 5077e2496..f94f2ece2 100644 --- a/src/language-markdown/print/mdast.js +++ b/src/language-markdown/print/mdast.js @@ -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 =