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

Add support for MDX 2 comments #11563

Merged
merged 11 commits into from Nov 17, 2021
Merged

Add support for MDX 2 comments #11563

merged 11 commits into from Nov 17, 2021

Conversation

wooorm
Copy link
Contributor

@wooorm wooorm commented Sep 21, 2021

Description

This adds support for MDX 2 comments (which are the same as other
comments inside JSX) in addition to MDX 1 comments (HTML-like
constructs).

Please note that this is my first PR to prettier and I don’t know the codebase well.

Related to GH-10706.
Related to wooorm/xdm#61.

/cc @thorn0 @fisker

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

This adds support for MDX 2 comments (which are the same as other
comments inside JSX) in addition to MDX 1 comments (HTML-like
constructs).

Related to GH-10706.
Related to wooorm/xdm#61.
{ stripTrailingHardline: true }
) +
"}"
);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I’m not sure why the MDX stuff is in embed, but that’s why I added it here

Copy link
Member

@fisker fisker Sep 22, 2021

Choose a reason for hiding this comment

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

I don't think this is the right place to add it, it should be here

When we disable embed language, it will throw.

Playground link

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pushed a fix to also add it to the normal printer. It looks like embed is for formatting embedded languages (so JS in MD), but not always available, hence it’s in two places?

Copy link
Member

Choose a reason for hiding this comment

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

Given they print the same result, I think we can remove this one.

Please add a test in https://github.com/prettier/prettier/tree/main/tests/format/mdx/embedded-language-formatting

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does the embedded parser perhaps break lines in comments or in other ways format them? 🤔

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 was going to ask you about that... I understand that's too much for you, but I failed to update remark-parse(#9397). It will be great, if you can help us for the upgrade sometime later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My goal is to get MDX to an RC in 2 weeks (essentially porting xdm back into it and adding docs). So we do have some time (before but also probably after it).

I can help, yeah. All of unified is now ESM-only. I believe prettier is bundling dependencies so that shouldn’t be a problem for end users. Can ESM be setup internally in prettier/src/language-markdown?

Copy link
Member

@fisker fisker Sep 22, 2021

Choose a reason for hiding this comment

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

Unfortunately, no. We still don't know the problem, but we can't make ES Module work for the tests, even a simple esm file... #11235

I guess we'll have to wait until Jest have better support for modules.

Copy link
Member

Choose a reason for hiding this comment

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

Also, our migration to Pure ESM can be tracked at #10157

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can the markdown language be compiled first before tests?

@@ -9,6 +9,7 @@ function startWithPragma(text) {
const regex = new RegExp(
[
`<!--\\s*${pragma}\\s*-->`,
`{\\s*\\/\\*\\s*${pragma}\\s*\\*\\/\\s*}`,
`<!--.*\r?\n[\\s\\S]*(^|\n)[^\\S\n]*${pragma}[^\\S\n]*($|\n)[\\s\\S]*\n.*-->`,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This other regex looks complex, which is why I didn’t port that to an es-comment either

tests/format/mdx/mdx/__snapshots__/jsfmt.spec.js.snap Outdated Show resolved Hide resolved
@fisker
Copy link
Member

fisker commented Sep 22, 2021

@JounQin Can you please help review this?

@JounQin
Copy link
Member

JounQin commented Sep 22, 2021

Will prettier support mdx@1 and mdx@2 at the same time in the future? Or is this just a temporary workaround?

@wooorm
Copy link
Contributor Author

wooorm commented Sep 22, 2021

I see this as a workaround to at least not format MDX 2 comments.

I recommend to, as soon as possible, support MDX 2 only.


{/* Some more. */}

{/* Some more. */}
Copy link
Member

Choose a reason for hiding this comment

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

Is this duplicate with the above comment? Maybe a test for multiple * here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Remove the duplicate.
Stars are not yet supported, as old remark sees them as lists. Would be nice to have that, but it’s a lot of work, which would also be solved by updating to use latest remark and remark-mdx!

Copy link
Member

Choose a reason for hiding this comment

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

A changelog should be added at https://github.com/prettier/prettier/tree/main/changelog_unreleased/mdx and mention this limitation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How should I do this? I have no experience with this

Note that there are lots of limitations currently. xdm/mdx-js@2 allow any JS expression in there, so there are many limitations anyway, but comments (like this) solve a common use case, even though it's not perfect

Copy link
Member

@sosukesuzuki sosukesuzuki left a comment

Choose a reason for hiding this comment

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

I'm not familiar with MDX, but looks good to me

@fisker
Copy link
Member

fisker commented Sep 26, 2021

@wooorm #11563 (comment)

@wooorm
Copy link
Contributor Author

wooorm commented Sep 26, 2021

@fisker done. How does this look?

@JounQin What would you suggest for this changelog entry?

@sosukesuzuki
Copy link
Member

@fisker #11563 (comment)

@sosukesuzuki sosukesuzuki added this to the 2.5 milestone Oct 6, 2021
@sosukesuzuki
Copy link
Member

We'll merge this when we start to prepare next minor release.

@wooorm
Copy link
Contributor Author

wooorm commented Oct 8, 2021

Awesome!
Is there a rough time known for when that would be?

@sosukesuzuki
Copy link
Member

I think it will be in early November.

@sosukesuzuki sosukesuzuki merged commit cca8469 into prettier:main Nov 17, 2021
@wooorm
Copy link
Contributor Author

wooorm commented Nov 17, 2021

Thanks for merging this @sosukesuzuki, and everyone for the help!

@wooorm wooorm deleted the mdx-es-comments branch November 17, 2021 08:24
@nickrttn nickrttn mentioned this pull request Feb 1, 2022
4 tasks
@kachkaev
Copy link
Member

kachkaev commented Feb 1, 2022

General MDX 2 issues: #12209

@gutenye
Copy link

gutenye commented Jul 20, 2022

Does not support block comment

{/*

hello

*/}

@txp1035

This comment was marked as spam.

medikoo pushed a commit to medikoo/prettier-elastic that referenced this pull request Dec 13, 2023
* Add support for MDX 2 comments

This adds support for MDX 2 comments (which are the same as other
comments inside JSX) in addition to MDX 1 comments (HTML-like
constructs).

Related to prettierGH-10706.
Related to wooorm/xdm#61.

* Fix blank line before comment

* Add `esComment` to `printer-markdown`

* Add embedded test

* Remove embedded formatter

* Add support for es-comments as blocks & test prettier-ignore comments

* Add a test for mdx pragma

* Remove duplicate fixture

* Add changelog

* Add note clarifying support

Co-authored-by: sosukesuzuki <aosukeke@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants