2018-04-12 19:27:34 +03:00
|
|
|
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";
|
2018-08-01 10:44:37 +03:00
|
|
|
case "json":
|
|
|
|
case "json-stringify":
|
|
|
|
return "jsonc";
|
|
|
|
case "glimmer":
|
|
|
|
return "hbs";
|
2018-04-12 19:27:34 +03:00
|
|
|
default:
|
|
|
|
return options.parser;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatCLIOptions(cliOptions) {
|
|
|
|
return cliOptions
|
|
|
|
.map(option => {
|
2018-04-19 19:53:42 +03:00
|
|
|
const name = option[0];
|
|
|
|
const value = option[1];
|
2018-04-12 19:27:34 +03:00
|
|
|
return value === true ? name : `${name} ${value}`;
|
|
|
|
})
|
|
|
|
.join("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
function codeBlock(content, syntax) {
|
2018-04-17 23:29:44 +03:00
|
|
|
const backtickSequences = content.match(/`+/g) || [];
|
|
|
|
const longestBacktickSequenceLength = Math.max.apply(
|
2018-04-12 19:27:34 +03:00
|
|
|
null,
|
|
|
|
backtickSequences.map(backticks => backticks.length)
|
|
|
|
);
|
2018-04-17 23:29:44 +03:00
|
|
|
const fenceLength = Math.max(3, longestBacktickSequenceLength + 1);
|
|
|
|
const fence = Array(fenceLength + 1).join("`");
|
2018-04-12 19:27:34 +03:00
|
|
|
return [fence + (syntax || ""), content, fence].join("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = formatMarkdown;
|