Skip to content

Commit

Permalink
Markdown: Preserve multiple spaces in inline code (#13590)
Browse files Browse the repository at this point in the history
  • Loading branch information
kachkaev committed Oct 14, 2022
1 parent 001d8b0 commit 8df3b5e
Show file tree
Hide file tree
Showing 23 changed files with 498 additions and 51 deletions.
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 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
Original file line number Diff line number Diff line change
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
11 changes: 0 additions & 11 deletions src/language-markdown/print-preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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" ||
Expand Down
17 changes: 13 additions & 4 deletions src/language-markdown/printer-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
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')\\}\\\`;
\`;
markdown\`
Expand Down

0 comments on commit 8df3b5e

Please sign in to comment.