Markdown: Preserve multiple spaces in inline code (#13590)

This commit is contained in:
Alexander Kachkaev authored and GitHub committed 2022-10-14 10:58:03 +03:00
1 parent 001d8b01b8
commit 8df3b5ee60
23 files changed
+498 -51

No files matched your search

+15
View File
@@ -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 `
```
+1 -1
View File
@@ -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") {
-11
View File
@@ -6,7 +6,6 @@ const isSingleCharRegex = /^.$/su;
function preprocess(ast, options) {
ast = restoreUnescapedCharacter(ast, options);
ast = mergeContinuousTexts(ast);
ast = transformInlineCode(ast, options);
ast = transformIndentedCodeblockAndMarkItsParentList(ast, options);
ast = markAlignedList(ast, options);
ast = splitTextIntoSentences(ast, options);
@@ -25,16 +24,6 @@ function transformImportExport(ast) {
});
}
function transformInlineCode(ast, options) {
return mapAst(ast, (node) => {
if (node.type !== "inlineCode" || options.proseWrap === "preserve") {
return node;
}
return { ...node, value: node.value.replace(/\s+/g, " ") };
});
}
function restoreUnescapedCharacter(ast, options) {
return mapAst(ast, (node) =>
node.type !== "text" ||
+13 -4
View File
@@ -169,10 +169,19 @@ function genericPrint(path, options, print) {
case "delete":
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 code =
options.proseWrap === "preserve"
? node.value
: node.value.replace(/\n/g, " ");
const backtickCount = getMinNotPresentContinuousCount(code, "`");
const backtickString = "`".repeat(backtickCount || 1);
const padding =
code.startsWith("`") ||
code.endsWith("`") ||
(/^[\n ]/.test(code) && /[\n ]$/.test(code) && /[^\n ]/.test(code))
? " "
: "";
return [backtickString, padding, code, padding, backtickString];
}
case "wikiLink": {
let contents = "";
@@ -109,7 +109,7 @@ markdown\`
=====================================output=====================================
markdown\`
const cssString = css\\\` background-color: \\$\\{color('base')\\}\\\`;
const cssString = css\\\` background-color: \\$\\{color('base')\\}\\\`;
\`;
markdown\`
@@ -21,14 +21,16 @@ proseWrap: "always"
\`\` 2 \`\`\`123\`\`\` \`1\` \`\`
\`\` CODE\` \`\`
=====================================output=====================================
\`\` \`123\` \`\`
\`\` 12\`34 \`\`
\`\`12\`34\`\`
\`\` \`12\`\`
\`\` 34\` \`\`
\`\`34\` \`\`
\` \`\`\`123\`\`\` \`
@@ -36,6 +38,51 @@ proseWrap: "always"
\`\` 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\` \`\`
\` \`\`\`123\`\`\` \`
\`\`\` 3 \`\`22\`\` \`1\` \`\`\`
\`\` 2 \`\`\`123\`\`\` \`1\` \`\`
\`\` CODE\` \`\`
================================================================================
`;
@@ -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"]
@@ -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"]
@@ -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"]
@@ -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 \`
================================================================================
`;
@@ -11,3 +11,5 @@
``` 3 ``22`` `1` ```
`` 2 ```123``` `1` ``
`` CODE` ``
@@ -0,0 +1,7 @@
` three spaces everywhere `
` three spaces
everywhere `
` three spaces
everywhere `
@@ -1 +1,7 @@
run_spec(import.meta, ["markdown"], { proseWrap: "always" });
const fixtures = {
importMeta: import.meta,
snippets: ["` \n `", "` \na `"],
};
run_spec(fixtures, ["markdown"], { proseWrap: "preserve" });
run_spec(fixtures, ["markdown"], { proseWrap: "always" });
@@ -1,26 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
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.
================================================================================
`;
@@ -1 +0,0 @@
run_spec(import.meta, ["markdown"], { proseWrap: "preserve" });
@@ -1,5 +1,162 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`commonmark-0.30-example-328.md - {"proseWrap":"always"} format 1`] = `
====================================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"]
@@ -5964,7 +6121,7 @@ proseWrap: "always"
\`\` foo \` bar \`\`
=====================================output=====================================
\`\` foo \` bar \`\`
\`\`foo \` bar \`\`
================================================================================
`;
@@ -6012,7 +6169,7 @@ proseWrap: "always"
baz\`
=====================================output=====================================
\`foo bar baz\`
\`foo bar baz\`
================================================================================
`;
@@ -6027,7 +6184,7 @@ proseWrap: "always"
\`foo \`\` bar\`
=====================================output=====================================
\` foo \`\` bar \`
\`foo \`\` bar\`
================================================================================
`;
@@ -10593,7 +10750,7 @@ proseWrap: "always"
span\`
=====================================output=====================================
\`code span\`
\`code span\`
================================================================================
`;
@@ -0,0 +1 @@
`foo`
@@ -0,0 +1 @@
`` foo ` bar ``
@@ -0,0 +1 @@
` `` `
@@ -0,0 +1 @@
` `` `
@@ -0,0 +1 @@
` a`
@@ -0,0 +1 @@
` b `
@@ -0,0 +1,2 @@
` `
` `
@@ -0,0 +1,5 @@
``
foo
bar
baz
``
@@ -0,0 +1,3 @@
``
foo
``
@@ -0,0 +1,2 @@
`foo bar
baz`