fix(markdown): handle CRLF correctly (#5414)
parent
6cedf7d5d9
commit
2bb95d8924
|
@ -209,7 +209,7 @@ function genericPrint(path, options, print) {
|
|||
const alignment = " ".repeat(4);
|
||||
return align(
|
||||
alignment,
|
||||
concat([alignment, join(hardline, node.value.split("\n"))])
|
||||
concat([alignment, replaceNewlinesWith(node.value, hardline)])
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -225,9 +225,9 @@ function genericPrint(path, options, print) {
|
|||
style,
|
||||
node.lang || "",
|
||||
hardline,
|
||||
join(
|
||||
hardline,
|
||||
getFencedCodeBlockValue(node, options.originalText).split("\n")
|
||||
replaceNewlinesWith(
|
||||
getFencedCodeBlockValue(node, options.originalText),
|
||||
hardline
|
||||
),
|
||||
hardline,
|
||||
style
|
||||
|
@ -469,7 +469,7 @@ function getNthListSiblingIndex(node, parentNode) {
|
|||
}
|
||||
|
||||
function replaceNewlinesWith(str, doc) {
|
||||
return join(doc, str.split("\n"));
|
||||
return join(doc, str.replace(/\r\n?/g, "\n").split("\n"));
|
||||
}
|
||||
|
||||
function getNthSiblingIndex(node, parentNode, condition) {
|
||||
|
|
|
@ -148,7 +148,7 @@ function getFencedCodeBlockValue(node, originalText) {
|
|||
const leadingSpaceCount = text.match(/^\s*/)[0].length;
|
||||
const replaceRegex = new RegExp(`^\\s{0,${leadingSpaceCount}}`);
|
||||
|
||||
const lineContents = text.split("\n");
|
||||
const lineContents = text.replace(/\r\n?/g, "\n").split("\n");
|
||||
|
||||
const markerStyle = text[leadingSpaceCount]; // ` or ~
|
||||
const marker = text
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
exports[`html parser should handle CRLF correctly 1`] = `"\\"<!--\\\\r\\\\n test\\\\r\\\\n test\\\\r\\\\n-->\\\\r\\\\n\\""`;
|
||||
|
||||
exports[`markdown parser should handle CRLF correctly 1`] = `"\\"\`\`\`\\\\r\\\\n\\\\r\\\\n\\\\r\\\\n\`\`\`\\\\r\\\\n\\""`;
|
||||
|
||||
exports[`typescript parser should throw the first error when both JSX and non-JSX mode failed 1`] = `
|
||||
"Expression expected. (9:7)
|
||||
7 | );
|
||||
|
|
|
@ -33,3 +33,11 @@ test("html parser should handle CRLF correctly", () => {
|
|||
JSON.stringify(prettier.format(input, { parser: "html" }))
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test("markdown parser should handle CRLF correctly", () => {
|
||||
const input = "```\r\n\r\n\r\n```";
|
||||
expect(
|
||||
// use JSON.stringify to observe CRLF
|
||||
JSON.stringify(prettier.format(input, { parser: "markdown" }))
|
||||
).toMatchSnapshot();
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue