fix(markdown): handle CRLF correctly (#5414)

master
Ika 2018-11-11 00:24:37 +08:00 committed by GitHub
parent 6cedf7d5d9
commit 2bb95d8924
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 6 deletions

View File

@ -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) {

View File

@ -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

View File

@ -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 | );

View File

@ -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();
});