69 lines
1.6 KiB
JavaScript
69 lines
1.6 KiB
JavaScript
![]() |
function formatMarkdown(
|
||
|
input,
|
||
|
output,
|
||
|
output2,
|
||
|
version,
|
||
|
url,
|
||
|
options,
|
||
|
cliOptions,
|
||
|
full
|
||
|
) {
|
||
|
const syntax = getMarkdownSyntax(options);
|
||
|
const optionsString = formatCLIOptions(cliOptions);
|
||
|
const isIdempotent = output2 === "" || output === output2;
|
||
|
|
||
|
return [
|
||
|
`**Prettier ${version}**`,
|
||
|
`[Playground link](${url})`,
|
||
|
optionsString === "" ? null : codeBlock(optionsString, "sh"),
|
||
|
"",
|
||
|
"**Input:**",
|
||
|
codeBlock(input, syntax),
|
||
|
"",
|
||
|
"**Output:**",
|
||
|
codeBlock(output, syntax)
|
||
|
]
|
||
|
.concat(
|
||
|
isIdempotent ? [] : ["", "**Second Output:**", codeBlock(output2, syntax)]
|
||
|
)
|
||
|
.concat(full ? ["", "**Expected behavior:**", ""] : [])
|
||
|
.filter(part => {
|
||
|
return part != null;
|
||
|
})
|
||
|
.join("\n");
|
||
|
}
|
||
|
|
||
|
function getMarkdownSyntax(options) {
|
||
|
switch (options.parser) {
|
||
|
case "babylon":
|
||
|
case "flow":
|
||
|
return "jsx";
|
||
|
case "typescript":
|
||
|
return "tsx";
|
||
|
default:
|
||
|
return options.parser;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function formatCLIOptions(cliOptions) {
|
||
|
return cliOptions
|
||
|
.map(option => {
|
||
|
const [name, value] = option;
|
||
|
return value === true ? name : `${name} ${value}`;
|
||
|
})
|
||
|
.join("\n");
|
||
|
}
|
||
|
|
||
|
function codeBlock(content, syntax) {
|
||
|
var backtickSequences = content.match(/`+/g) || [];
|
||
|
var longestBacktickSequenceLength = Math.max.apply(
|
||
|
null,
|
||
|
backtickSequences.map(backticks => backticks.length)
|
||
|
);
|
||
|
var fenceLength = Math.max(3, longestBacktickSequenceLength + 1);
|
||
|
var fence = Array(fenceLength + 1).join("`");
|
||
|
return [fence + (syntax || ""), content, fence].join("\n");
|
||
|
}
|
||
|
|
||
|
module.exports = formatMarkdown;
|