Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(markdown): handle CRLF correctly #5414

Merged
merged 2 commits into from Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
});