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

Markdown: Preserve multiple spaces in inline code #13590

Merged
merged 22 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
15 changes: 15 additions & 0 deletions changelog_unreleased/markdown/13590.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#### Preserve multiple spaces in inline code (#13590 by @kachkaev)
thorn0 marked this conversation as resolved.
Show resolved Hide resolved

Previously, multiple whitespace characters in inline code were collapsed into a single space. This is no longer happening to match [CommonMark spec](https://spec.commonmark.org/0.30/#backtick-string).

<!-- prettier-ignore -->
```markdown
<!-- Input -->
` foo bar baz `

<!-- Prettier stable -->
` foo bar baz `

<!-- Prettier main -->
` foo bar baz `
```
8 changes: 7 additions & 1 deletion src/language-markdown/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ function clean(ast, newObj, parent) {
}

if (ast.type === "inlineCode") {
newObj.value = ast.value.replace(/[\t\n ]+/g, " ");
const valueWithoutNewlines = ast.value.replace(/\n/g, " ");
newObj.value =
valueWithoutNewlines.startsWith(" ") &&
valueWithoutNewlines.endsWith(" ") &&
valueWithoutNewlines.trim().length > 0
? " " + valueWithoutNewlines + " "
: valueWithoutNewlines;
}

if (ast.type === "wikiLink") {
Expand Down
12 changes: 11 additions & 1 deletion src/language-markdown/print-preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ function transformInlineCode(ast) {
return node;
}

return { ...node, value: node.value.replace(/\s+/g, " ") };
const valueWithoutNewlines = node.value.replace(/\n/g, " ");
thorn0 marked this conversation as resolved.
Show resolved Hide resolved

return {
...node,
value:
valueWithoutNewlines.startsWith(" ") &&
valueWithoutNewlines.endsWith(" ") &&
valueWithoutNewlines.trim().length > 0
? " " + valueWithoutNewlines + " "
: valueWithoutNewlines,
};
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ markdown\`
=====================================output=====================================
markdown\`
const cssString = css\\\` background-color: \\$\\{color('base')\\}\\\`;
const cssString = css\\\` background-color: \\$\\{color('base')\\}\\\`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this call markdown -> js -> css and remove the leading space? Because it's not valid css?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a crazy test. const cssString = ... is Markdown here, not JS. The Markdown-in-JS embedder also includes logic for ignoring indentation (no idea what's the story behind that). That's why the output is like that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you explained before... forgot. Sorry.

\`;
markdown\`
Expand Down
25 changes: 25 additions & 0 deletions tests/format/markdown/inlineCode/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,31 @@ proseWrap: "always"
================================================================================
`;

exports[`inline-code-multiple-spaces.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "always"
| printWidth
=====================================input======================================
\` three spaces everywhere \`

\` three spaces
everywhere \`

\` three spaces
everywhere \`

=====================================output=====================================
\` three spaces everywhere \`

\` three spaces everywhere \`

\` three spaces everywhere \`

================================================================================
`;

exports[`long.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
` three spaces everywhere `

` three spaces
everywhere `

` three spaces
everywhere `
161 changes: 159 additions & 2 deletions tests/format/markdown/spec/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,162 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`commonmark-0.30-example-328.md - {"proseWrap":"always"} format 1`] = `
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context: #13591

====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "always"
| printWidth
=====================================input======================================
\`foo\`

=====================================output=====================================
\`foo\`

================================================================================
`;

exports[`commonmark-0.30-example-329.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "always"
| printWidth
=====================================input======================================
\`\` foo \` bar \`\`

=====================================output=====================================
\`\` foo \` bar \`\`

================================================================================
`;

exports[`commonmark-0.30-example-330.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "always"
| printWidth
=====================================input======================================
\` \`\` \`

=====================================output=====================================
\` \`\` \`

================================================================================
`;

exports[`commonmark-0.30-example-331.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "always"
| printWidth
=====================================input======================================
\` \`\` \`

=====================================output=====================================
\` \`\` \`

================================================================================
`;

exports[`commonmark-0.30-example-332.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "always"
| printWidth
=====================================input======================================
\` a\`

=====================================output=====================================
\` a\`

================================================================================
`;

exports[`commonmark-0.30-example-333.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "always"
| printWidth
=====================================input======================================
\` b \`

=====================================output=====================================
\` b \`

================================================================================
`;

exports[`commonmark-0.30-example-334.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "always"
| printWidth
=====================================input======================================
\` \`
\` \`

=====================================output=====================================
\` \` \` \`

================================================================================
`;

exports[`commonmark-0.30-example-335.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "always"
| printWidth
=====================================input======================================
\`\`
foo
bar
baz
\`\`

=====================================output=====================================
\`foo bar baz\`

================================================================================
`;

exports[`commonmark-0.30-example-336.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "always"
| printWidth
=====================================input======================================
\`\`
foo
\`\`

=====================================output=====================================
\`foo \`

================================================================================
`;

exports[`commonmark-0.30-example-337.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "always"
| printWidth
=====================================input======================================
\`foo bar
baz\`
=====================================output=====================================
\`foo bar baz\`

================================================================================
`;

exports[`example-1.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
Expand Down Expand Up @@ -6012,7 +6169,7 @@ proseWrap: "always"
baz\`

=====================================output=====================================
\`foo bar baz\`
\`foo bar baz\`

================================================================================
`;
Expand Down Expand Up @@ -10593,7 +10750,7 @@ proseWrap: "always"
span\`

=====================================output=====================================
\`code span\`
\`code span\`

================================================================================
`;
Expand Down
1 change: 1 addition & 0 deletions tests/format/markdown/spec/commonmark-0.30-example-328.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`foo`
1 change: 1 addition & 0 deletions tests/format/markdown/spec/commonmark-0.30-example-329.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`` foo ` bar ``
1 change: 1 addition & 0 deletions tests/format/markdown/spec/commonmark-0.30-example-330.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
` `` `
1 change: 1 addition & 0 deletions tests/format/markdown/spec/commonmark-0.30-example-331.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
` `` `
1 change: 1 addition & 0 deletions tests/format/markdown/spec/commonmark-0.30-example-332.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
` a`
1 change: 1 addition & 0 deletions tests/format/markdown/spec/commonmark-0.30-example-333.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
` b `
2 changes: 2 additions & 0 deletions tests/format/markdown/spec/commonmark-0.30-example-334.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
` `
` `
5 changes: 5 additions & 0 deletions tests/format/markdown/spec/commonmark-0.30-example-335.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
``
foo
bar
baz
``
3 changes: 3 additions & 0 deletions tests/format/markdown/spec/commonmark-0.30-example-336.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
``
foo
``
2 changes: 2 additions & 0 deletions tests/format/markdown/spec/commonmark-0.30-example-337.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`foo bar
baz`