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

Markdown: Preserve multiple spaces in inline code #13590

merged 22 commits into from Oct 14, 2022

Conversation

kachkaev
Copy link
Member

@kachkaev kachkaev commented Oct 8, 2022

Description

Fixes #13485

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@kachkaev kachkaev added the lang:markdown Issues affecting Markdown label Oct 8, 2022
@kachkaev kachkaev changed the base branch from main to next October 8, 2022 19:14
@@ -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

@kachkaev
Copy link
Member Author

kachkaev commented Oct 8, 2022

Hmmm Example 331 fails second pass:

FULL_TEST=true yarn test markdown/spec/jsfmt
[space][space]``[space][space]
↓
[space]``[space]
↓
``

Screenshot 2022-10-08 at 21 37 09

I don’t understand this because Example 331 after formatting is Example 330 (which works). Going to take a break now. Any thoughts?

UPD: I reverted changes in src/language-markdown/print-preprocess.js locally and commonmark-0.30-example-331.md failed the same way. I moved this file to a subfolder called remark-bug, which seems to be a convention for such cases. Not sure why, but example 331 works well in the Playground (it worked before too).

@kachkaev kachkaev marked this pull request as ready for review October 8, 2022 22:22
@fisker
Copy link
Member

fisker commented Oct 9, 2022

example 331 & example 330 has different value,

newObj.value = ast.value.replace(/[\t\n ]+/g, " ");

@thorn0 thorn0 self-requested a review October 9, 2022 13:32
@kachkaev kachkaev closed this Oct 9, 2022
@kachkaev kachkaev reopened this Oct 9, 2022
@kachkaev
Copy link
Member Author

kachkaev commented Oct 9, 2022

Pressed the wrong button 😅

I included #11373. I still don’t fully understand the relationship between src/language-markdown/print-preprocess.jstransformInlineCode and src/language-markdown/clean.js 🤔 The latter does not have access to options.proseWrap, but tests still pass. If I remove any transforms from either function, some snapshots fail 🤔

@kachkaev
Copy link
Member Author

@thorn0 I merged next into my branch, so PR playground includes #13606 now. Thanks for implementing ‘show preprocessed AST‘!

Do you foresee any other changes we need to make before merging?

return {
...node,
value:
value.startsWith(" ") && value.endsWith(" ") && value.trim().length > 0
Copy link
Member

Choose a reason for hiding this comment

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

Something doesn't look right here. Is this part really needed? Isn't this check and processing done by the parser already?

Copy link
Member

Choose a reason for hiding this comment

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

Okay, I see why you did this. But this should be done in print, not in preprocess.

Copy link
Member Author

Choose a reason for hiding this comment

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

I see, thanks for the fix! 🙌

Do you know why we still need newObj.value = ast.value.replace(/\n/g, " "); in clean.js? This transform is applied regardless of proseWrap. I vaguely remember trying to remove it but tests failed.

Copy link
Member

Choose a reason for hiding this comment

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

Those tests are run only with proseWrap: "always", that's why it works. Need to think what to do.

Copy link
Member

@thorn0 thorn0 Oct 13, 2022

Choose a reason for hiding this comment

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

No, I was wrong. It's some other tests that fail if I add

run_spec(import.meta, ["markdown"], { proseWrap: "preserve" });
run_spec(import.meta, ["markdown"], { proseWrap: "never" });

to jsfmt.spec.js, not related to code spans.

As for clean.js, see how the clean function (AKA massageAST) is used:

prettier/src/cli/format.js

Lines 137 to 169 in 955553b

if (context.argv.debugCheck) {
const pp = prettier.format(input, opt);
const pppp = prettier.format(pp, opt);
if (pp !== pppp) {
throw new errors.DebugError(
"prettier(input) !== prettier(prettier(input))\n" + diff(pp, pppp)
);
} else {
const stringify = (obj) => JSON.stringify(obj, null, 2);
const ast = stringify(
prettier.__debug.parse(input, opt, /* massage */ true).ast
);
const past = stringify(
prettier.__debug.parse(pp, opt, /* massage */ true).ast
);
/* istanbul ignore next */
if (ast !== past) {
const MAX_AST_SIZE = 2097152; // 2MB
const astDiff =
ast.length > MAX_AST_SIZE || past.length > MAX_AST_SIZE
? "AST diff too large to render"
: diff(ast, past);
throw new errors.DebugError(
"ast(input) !== ast(prettier(input))\n" +
astDiff +
"\n" +
diff(input, pp)
);
}
}
return { formatted: pp, filepath: opt.filepath || "(stdin)\n" };
}

It's applied to both the original AST and to the AST built from Prettier's output, and then the ASTs are checked for deep equality. So it makes sense that this replacement is needed. After it, no matter what proseWrap is, the values in the two ASTs are equal.

Co-authored-by: Alexander Kachkaev <alexander@kachkaev.ru>
@thorn0 thorn0 linked an issue Oct 13, 2022 that may be closed by this pull request
Copy link
Member

@fisker fisker left a comment

Choose a reason for hiding this comment

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

Good job! Just a few unimportant questions.

@@ -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.

@@ -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.

@kachkaev
Copy link
Member Author

Great, thanks for helping with this PR @thorn0 and @fisker! 👏

@kachkaev kachkaev deleted the inline-code-spacing branch October 14, 2022 09:58
@sosukesuzuki sosukesuzuki mentioned this pull request Nov 12, 2022
1 task
medikoo pushed a commit to medikoo/prettier-elastic that referenced this pull request Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lang:markdown Issues affecting Markdown
Projects
None yet
3 participants