Skip to content

Commit

Permalink
fix(markdown): handle CRLF correctly (#5414)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Nov 10, 2018
1 parent 6cedf7d commit 2bb95d8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/language-markdown/printer-markdown.js
Expand Up @@ -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)])
);
}

Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/language-markdown/utils.js
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests_integration/__tests__/__snapshots__/format.js.snap
Expand Up @@ -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 | );
Expand Down
8 changes: 8 additions & 0 deletions tests_integration/__tests__/format.js
Expand Up @@ -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();
});

0 comments on commit 2bb95d8

Please sign in to comment.