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 21 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
@@ -0,0 +1,15 @@
#### Preserve multiple spaces in inline code (#13590 by @kachkaev and @thorn0)

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 `
```
2 changes: 1 addition & 1 deletion src/language-markdown/clean.js
Expand Up @@ -33,7 +33,7 @@ function clean(ast, newObj, parent) {
}

if (ast.type === "inlineCode") {
newObj.value = ast.value.replace(/[\t\n ]+/g, " ");
newObj.value = ast.value.replace(/\n/g, " ");
}

if (ast.type === "wikiLink") {
Expand Down
2 changes: 1 addition & 1 deletion src/language-markdown/print-preprocess.js
Expand Up @@ -31,7 +31,7 @@ function transformInlineCode(ast, options) {
return node;
}

return { ...node, value: node.value.replace(/\s+/g, " ") };
return { ...node, value: node.value.replace(/\n/g, " ") };
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can move this to print? So we can reduce one mapAst call. Can be separate PR.

});
}

Expand Down
13 changes: 10 additions & 3 deletions src/language-markdown/printer-markdown.js
Expand Up @@ -170,9 +170,16 @@ function genericPrint(path, options, print) {
return ["~~", printChildren(path, options, print), "~~"];
case "inlineCode": {
const backtickCount = getMinNotPresentContinuousCount(node.value, "`");
const style = "`".repeat(backtickCount || 1);
const gap = backtickCount && !/^\s/.test(node.value) ? " " : "";
return [style, gap, node.value, gap, style];
const backtickString = "`".repeat(backtickCount || 1);
const padding =
node.value.startsWith("`") ||
node.value.endsWith("`") ||
(/^[\n ]/.test(node.value) &&
/[\n ]$/.test(node.value) &&
/[^\n ]/.test(node.value))
? " "
: "";
return [backtickString, padding, node.value, padding, backtickString];
}
case "wikiLink": {
let contents = "";
Expand Down
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
275 changes: 273 additions & 2 deletions tests/format/markdown/inlineCode/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -21,21 +21,68 @@ proseWrap: "always"

\`\` 2 \`\`\`123\`\`\` \`1\` \`\`

\`\` CODE\` \`\`

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

\`\`12\`34\`\`

\`\` \`12\`\`

\`\`34\` \`\`

\` \`\`\`123\`\`\` \`

\`\`\` 3 \`\`22\`\` \`1\` \`\`\`

\`\` 2 \`\`\`123\`\`\` \`1\` \`\`

\`\` CODE\` \`\`

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

exports[`backtick.md - {"proseWrap":"preserve"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "preserve"
| printWidth
=====================================input======================================
\`\` \`123\` \`\`

\`\`12\`34\`\`

\`\` \`12\`\`

\`\`34\` \`\`

\`\` \`\`\`123\`\`\` \`\`

\`\`\` 3 \`\`22\`\` \`1\` \`\`\`

\`\` 2 \`\`\`123\`\`\` \`1\` \`\`

\`\` CODE\` \`\`

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

\`\` 12\`34 \`\`
\`\`12\`34\`\`

\`\` \`12\`\`

\`\` 34\` \`\`
\`\`34\` \`\`

\` \`\`\`123\`\`\` \`

\`\`\` 3 \`\`22\`\` \`1\` \`\`\`

\`\` 2 \`\`\`123\`\`\` \`1\` \`\`

\`\` CODE\` \`\`

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

Expand All @@ -54,6 +101,21 @@ proseWrap: "always"
================================================================================
`;

exports[`cjk.md - {"proseWrap":"preserve"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "preserve"
| printWidth
=====================================input======================================
\`const x = "中文123"\`

=====================================output=====================================
\`const x = "中文123"\`

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

exports[`escape.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
Expand All @@ -69,6 +131,123 @@ proseWrap: "always"
================================================================================
`;

exports[`escape.md - {"proseWrap":"preserve"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "preserve"
| printWidth
=====================================input======================================
\`1*2*3\`

=====================================output=====================================
\`1*2*3\`

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

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[`inline-code-multiple-spaces.md - {"proseWrap":"preserve"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "preserve"
| printWidth
=====================================input======================================
\` three spaces everywhere \`

\` three spaces
everywhere \`

\` three spaces
everywhere \`

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

\` three spaces
everywhere \`

\` three spaces
everywhere \`

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

exports[`inline-code-newline.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "always"
| printWidth
=====================================input======================================
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod \`tempor
incididunt\` ut labore et dolore magna aliqua. Ut enim ad minim veniam, \`quis
nostrud\` exercitation ullamco laboris nisi ut aliquip ex ea commodo \`consequat.
Duis\` aute irure dolor in reprehenderit in voluptate velit esse cillum dolore \`eu
fugiat\` nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.

=====================================output=====================================
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
\`tempor incididunt\` ut labore et dolore magna aliqua. Ut enim ad minim veniam,
\`quis nostrud\` exercitation ullamco laboris nisi ut aliquip ex ea commodo
\`consequat. Duis\` aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore \`eu fugiat\` nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

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

exports[`inline-code-newline.md - {"proseWrap":"preserve"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "preserve"
| printWidth
=====================================input======================================
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod \`tempor
incididunt\` ut labore et dolore magna aliqua. Ut enim ad minim veniam, \`quis
nostrud\` exercitation ullamco laboris nisi ut aliquip ex ea commodo \`consequat.
Duis\` aute irure dolor in reprehenderit in voluptate velit esse cillum dolore \`eu
fugiat\` nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.

=====================================output=====================================
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod \`tempor
incididunt\` ut labore et dolore magna aliqua. Ut enim ad minim veniam, \`quis
nostrud\` exercitation ullamco laboris nisi ut aliquip ex ea commodo \`consequat.
Duis\` aute irure dolor in reprehenderit in voluptate velit esse cillum dolore \`eu
fugiat\` nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.

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

exports[`long.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
Expand All @@ -84,6 +263,21 @@ proseWrap: "always"
================================================================================
`;

exports[`long.md - {"proseWrap":"preserve"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "preserve"
| printWidth
=====================================input======================================
\`this is a long long long long long long long long long long long long long long long inline code\`

=====================================output=====================================
\`this is a long long long long long long long long long long long long long long long inline code\`

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

exports[`simple.md - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
Expand All @@ -98,3 +292,80 @@ proseWrap: "always"

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

exports[`simple.md - {"proseWrap":"preserve"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "preserve"
| printWidth
=====================================input======================================
\`123\`

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

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

exports[`snippet: #0 - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "always"
| printWidth
=====================================input======================================
\`
\`
=====================================output=====================================
\` \`

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

exports[`snippet: #0 - {"proseWrap":"preserve"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "preserve"
| printWidth
=====================================input======================================
\`
\`
=====================================output=====================================
\`
\`

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

exports[`snippet: #1 - {"proseWrap":"always"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "always"
| printWidth
=====================================input======================================
\`
a \`
=====================================output=====================================
\` a \`

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

exports[`snippet: #1 - {"proseWrap":"preserve"} format 1`] = `
====================================options=====================================
parsers: ["markdown"]
printWidth: 80
proseWrap: "preserve"
| printWidth
=====================================input======================================
\`
a \`
=====================================output=====================================
\`
a \`

================================================================================
`;
2 changes: 2 additions & 0 deletions tests/format/markdown/inlineCode/backtick.md
Expand Up @@ -11,3 +11,5 @@
``` 3 ``22`` `1` ```

`` 2 ```123``` `1` ``

`` CODE` ``
@@ -0,0 +1,7 @@
` three spaces everywhere `

` three spaces
everywhere `

` three spaces
everywhere `