Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: fisker <lionkay@gmail.com>
87 lines
2.1 KiB
JavaScript
87 lines
2.1 KiB
JavaScript
function formatMarkdown({
|
|
input,
|
|
output,
|
|
output2,
|
|
doc,
|
|
version,
|
|
url,
|
|
options,
|
|
cliOptions,
|
|
full,
|
|
}) {
|
|
const syntax = getMarkdownSyntax(options);
|
|
const optionsString = formatCLIOptions(cliOptions);
|
|
const isIdempotent = !output2 || output === output2;
|
|
|
|
return [
|
|
`**Prettier ${version.text}**`,
|
|
`[Playground link](${url})`,
|
|
optionsString === "" ? null : codeBlock(optionsString, "sh"),
|
|
"",
|
|
"**Input:**",
|
|
codeBlock(input, syntax),
|
|
...(doc ? ["", "**Doc:**", codeBlock(doc, "js")] : []),
|
|
...(output === undefined
|
|
? []
|
|
: ["", "**Output:**", codeBlock(output, syntax)]),
|
|
...(isIdempotent
|
|
? []
|
|
: ["", "**Second Output:**", codeBlock(output2, syntax)]),
|
|
...(full ? ["", "**Expected output:**", codeBlock("", syntax)] : []),
|
|
...(full
|
|
? ["", "**Why?**", "", "<!-- short explanation of expected output -->"]
|
|
: []),
|
|
]
|
|
.filter((part) => part !== null)
|
|
.join("\n");
|
|
}
|
|
|
|
function getMarkdownSyntax(options) {
|
|
switch (options.parser) {
|
|
case "babel":
|
|
case "babel-flow":
|
|
case "flow":
|
|
case "hermes":
|
|
case "acorn":
|
|
case "espree":
|
|
case "meriyah":
|
|
case "doc-explorer":
|
|
return "jsx";
|
|
case "babel-ts":
|
|
case "typescript":
|
|
return "tsx";
|
|
case "json":
|
|
case "json-stringify":
|
|
return "jsonc";
|
|
case "glimmer":
|
|
return "hbs";
|
|
case "angular":
|
|
case "lwc":
|
|
case "mjml":
|
|
return "html";
|
|
default:
|
|
return options.parser;
|
|
}
|
|
}
|
|
|
|
function formatCLIOptions(cliOptions) {
|
|
return cliOptions
|
|
.map(([name, value]) => (value === true ? name : `${name} ${value}`))
|
|
.join("\n");
|
|
}
|
|
|
|
function codeBlock(content, syntax) {
|
|
const backtickSequences = content.match(/`+/g) || [];
|
|
const longestBacktickSequenceLength = Math.max(
|
|
...backtickSequences.map(({ length }) => length),
|
|
);
|
|
const prettierIgnoreComment = "<!-- prettier-ignore -->";
|
|
const fenceLength = Math.max(3, longestBacktickSequenceLength + 1);
|
|
const fence = "`".repeat(fenceLength);
|
|
return [prettierIgnoreComment, fence + (syntax || ""), content, fence].join(
|
|
"\n",
|
|
);
|
|
}
|
|
|
|
export default formatMarkdown;
|