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

MDX 3 #12209

Open
kachkaev opened this issue Feb 1, 2022 · 57 comments
Open

MDX 3 #12209

kachkaev opened this issue Feb 1, 2022 · 57 comments
Assignees
Labels
lang:mdx Issues affecting the MDX extension to Markdown (not general Markdown issues)

Comments

@kachkaev
Copy link
Member

kachkaev commented Feb 1, 2022

MDX 2 was released on 1 Feb 2022: https://github.com/mdx-js/mdx/releases/tag/2.0.0
UPD: MDX 3 was released on 24 Oct 2023: https://github.com/mdx-js/mdx/releases/tag/3.0.0

The changes are summarised in the blog post: https://mdxjs.com/blog/v2/
UPD: https://mdxjs.com/migrating/v3/

Here is a big one for Prettier:

Originally, the format was very close to how markdown, and HTML in markdown, works. We found that the old behavior often yielded unexpected results. In version 2, we shift the format a little bit closer to how JS(X) works.

The example below is from the blog post. It is a valid MDX in v2, but Prettier 2.5.1 is unable to parse it:

Prettier 2.5.1
Playground link

--parser mdx

Input:

<div>*hi*?</div>

<div>
  # hi?
</div>

<main>
  <div>

    # hi?

  </div>
</main>

Output:

SyntaxError: Unexpected token (2:12)
  1 | <$><main>
> 2 |   <div></$>
    |            ^

Expected behavior:

No syntax error


Let’s track MDX 2 3 support here 👀

@sosukesuzuki
Copy link
Member

Remark and MDX ecosystems use Pure ECMAScript Modules Packages. We need to use that parser, and I think the our ECMAScript Modules migration(#10157) block this work.

@sosukesuzuki
Copy link
Member

sosukesuzuki commented Feb 2, 2022

For now, As a first step, I will restart the migration to remark-parser v9 (#11938)

@wooorm
Copy link
Contributor

wooorm commented Feb 2, 2022

As you’re already bundling prettier when releasing, could you bundle mdx-js/mdx / remark-parse and such as CJS?

@sosukesuzuki
Copy link
Member

Do you mean bundling ESM Packages(mdx-js/mdx, remark-parser) into CJS(bundled prettier)? It may be possible, but it will no longer be possible to run the source code as-is in Node.js, as is current Prettier for development.

/cc @fisker

@wooorm
Copy link
Contributor

wooorm commented Feb 2, 2022

but it will no longer be possible to run the source code as-is in Node.js

I’m suggesting something like an npm script that works like this:

npx esbuild @mdx-js/mdx --bundle --platform=node --outfile=vendor/mdx.js

That could be run once in a while. Like, every month? It doesn’t have to affect running Prettier in development?

@sosukesuzuki
Copy link
Member

Thank you! I think we can use the way you suggested. It may solve the problem that we cannot use other Pure ESM packages I'll try it.

@dburrows
Copy link

dburrows commented Feb 10, 2022

Just to add, when the parsing does work, the inline markdown within components in MDX2 is mangled - see the Playground example here

Input

<div>
  ## foo
  
  a paragraph
  
  another paragraph
</div>

Output

<div>## foo a paragraph another paragraph</div>

Expected output

<div>
  ## foo

  a paragraph
  
  another paragraph
</div>

Not sure whether the parser update will fix this.

@sebastienbarre
Copy link

Thank you people for working on this.
If this helps, here is another problem I'm experiencing in a MDX file which I was just told is related to this very issue:

MDX also supports JavaScript expressions inside curly braces, i.e.: {Math.PI * 2}

Prettier automatically changes it to:

MDX also supports JavaScript expressions inside curly braces, i.e.: {Math.PI \* 2}

Which breaks MDX parsers -- this is no longer a valid Javascript/JSX expression and it won't be executed.

Using Prettier's recommended <!-- prettier-ignore --> won't help because it breaks the MDX parser as well (because of the < used for React/JSX components), and using MDX's recommend {/* this is a comment */} doesn't work because it is not recognized by Prettier since the file is treated as Markdown (as it should), not JSX.

Thank you.

@sosukesuzuki
Copy link
Member

we unpin this issue temporary because we should pin #12931

@michrome
Copy link

Hey @cpboyd

I can't recreate the issue you're having. For me, running Prettier on <Custom>Hello World</Custom> doesn't change it. You can see that in the playground too.

That said, I have found inconsistencies with ignoring in MDX. What works most reliably for me is to use start and end in combination with blank lines, like this:

{/* prettier-ignore-start */}

When writing <Abbr a="HTML" />, keeping the document valid is a good first step
towards accessibility. Compared to XHTML, HTML5 validation is surprisingly
lenient, requiring only a <HtmlTag t="DOCTYPE" /> and a <HtmlTag t="title" />
element. The document below is valid even though it doesn’t contain an
<HtmlTag t="html" />, <HtmlTag t="head" /> or <HtmlTag t="body" /> element,
because they’re all considered optional. Also optional is the closing
<HtmlTag t="/p" /> tag.

{/* prettier-ignore-end */}

Without those ignores, Prettier will incorrectly insert new lines before the lines that start with the inline <HtmlTag> components.

@cpboyd
Copy link

cpboyd commented Sep 28, 2023

@michrome You're right with that specific example since it's under the 80 character threshold. I'd left out attributes when I replied. See this playground instead

<Custom test="really-long-string-test-hello-world-get-to-80-characters">Hello World</Custom>

That said, now I'm unable to reproduce in VSCode after restart. {/* prettier-ignore */} seems to be working. Maybe the plugin was out of date or just got into a funky state, which sometimes seems to happen with VSCode formatters.

@michrome
Copy link

{/* prettier-ignore-start */}

<Custom test="really-long-string-test-hello-world-get-to-80-characters">Hello World</Custom>

{/* prettier-ignore-end */}

Doesn't wrap in the playground 👍

@cpboyd
Copy link

cpboyd commented Sep 30, 2023

Maybe I'd had a space after the ignore block previously, because that apparently breaks the ignore.
See this playground

Edit: Seems to happen with a single space after both {/* prettier-ignore-start */} and {/* prettier-ignore */}

@tats-u
Copy link
Contributor

tats-u commented Oct 27, 2023

MDX v3 has been released.
https://mdxjs.com/blog/v3/

We're too legacy now.

@kachkaev kachkaev changed the title MDX 2 MDX 3 Oct 27, 2023
imagoiq added a commit to swisspost/design-system that referenced this issue Oct 31, 2023
### What

Commit essential linting and formatting config from our favourite IDE to
the repository. The aim is to have the same config across the team and
across IDE so we avoid dealing with changes that shouldn't be part of
our PR.

* Add config for Intellij and vscode
  - Remove gitignore on .idea config
  - Disable prettier on MDX files
  - Use LF as default line separator
  - Enable format on save
* Run `prettier --write
"**/*.{**/*,*}.{js,ts,jsx,tsx,scss,css,html,json}"`
* Remove prettier-ignore in mdx files

About MDX:
Disable formatting totally, as the maximum printWidth rule conflicts
with MDX syntax about new line = `<p>`.
Latest issue that we had:
#2128 . Issue on Prettier
repository: prettier/prettier#12209

If this wouldn't be enough, the second option is to ensure that our
files are checked before committed with a git hook:
https://github.com/lint-staged/lint-staged

For review: you can do it commit by commit.

---------

Co-authored-by: Philipp Gfeller <1659006+gfellerph@users.noreply.github.com>
@tats-u
Copy link
Contributor

tats-u commented Jan 3, 2024

We have to note that new MDX parsers will not treat * and ** surrounded by both punctuation (including non-ASCII ones) and non-space character as the beginning or end mark of emphasis.
This is due to a regression in CommonMark 0.14+.
commonmark/commonmark-spec#650
This change is inconvenient mostly for Chinese and Japanese documents.

**この部分は強調とみなされなくなりました。**ご注意ください。

@wooorm
Copy link
Contributor

wooorm commented Jan 5, 2024

@tats-u I think your time is better spent working on a solution rather than spamming all projects that do something with markdown. If you don’t know how to solve it, you could try and motivate other Chinese or Japanese speakers involved.

@tats-u
Copy link
Contributor

tats-u commented Jan 5, 2024

@wooorm I would not like to post an extra comment, but sou should not have used the verb "spam" before evaluating my suggestion on that issue even though I should have posted it on issues or PRs that directly mention on upgrading remark-parse instead.
Your comment this time that chose "spam" as verb looks like you have tried to hinder me from spreading the existence and the bad effect of the eater egg in CM.
I think I have just finished the enlightenment phase in GitHub and do not have to let users of other projects in GitHub know this eater egg.
Prettier users have been relatively sensitive to breaking changes in Markdown written in CJK languages (e.g. #6385).

You must have noted that this change will have to be added in the change log in the version that updates the remark parser and MDX before posting the comment.

@tats-u
Copy link
Contributor

tats-u commented Jan 5, 2024

(looks like I mistakenly somehow posted the same comment twice)

@JounQin
Copy link
Member

JounQin commented Jan 5, 2024

@tats-u That does not matter with this issue at all, your linked issue is caused by upstream, we won't consider that when try to support MDX 2/3. So I would agree with @wooorm that your comments are really spam here, sorry.

@tats-u
Copy link
Contributor

tats-u commented Jan 5, 2024

I forgot to say that some test cases related to CJK languages might have to be modified when upgrading remark to newer versions to support MDX v3 (PRs trying to do are stagnant).
This is also why I mentioned that issue here.
I believe this is not a spam begging Prettier not to upgrade remark but just an admonition for those who are trying to upgrade remark and Chinese and Japanese users who want to use the latest MDX even in Prettier. We basically have to accept this eater egg at least for a while because the latest MDX produces more pros for Prettier users.
I will not add any more comments (at least would like to suppress the number of ones) here as this is not so much related and welcomed as you say.

@JounQin
Copy link
Member

JounQin commented Jan 5, 2024

I won't fix such cases at least, they would be known issues if I'm developing, it is out scope of prettier itself.

medikoo pushed a commit to medikoo/prettier-elastic that referenced this issue Feb 15, 2024
…fixed (prettier#15221)

State that the supported version of MDX is 1
@namka279
Copy link

MDX #15221

huonw added a commit to huonw/pantsbuild.org that referenced this issue Mar 12, 2024
huonw added a commit to pantsbuild/pantsbuild.org that referenced this issue Mar 12, 2024
…ge (#177)

This PR fixes #174 with a new `VersionedDocsLink` component that reads
the docusaurus config to determine what the latest stable version is.

It also adjusts the other dynamic link to the contribution guide to use
this. This link is changed from the latest RC versions (currently 2.20)
to the current `main` version (2.21), i.e. the absolute most up-to-date
version of the contribution guide, which seems most relevant to
contributors since they'll be starting in the `main` branch.

This has a few issues with the component due to
prettier/prettier#12209, which forces us into
a very long line on the `_index.mdx` page.

I'm unclear if a broken link will be detected or not.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lang:mdx Issues affecting the MDX extension to Markdown (not general Markdown issues)
Projects
None yet
Development

No branches or pull requests