Skip to content

MDX: support Markdown inside JSX blocks #7041

Closed

Description

@trevordmiller

According to the commonmark spec, it seems that the example below is valid MDX. However, Prettier is throwing an error.

Prettier 1.19.1
Playground link

--parser mdx

Input:

<CodeSurfer>

```js
console.log(1);
```

</CodeSurfer>

Output:

SyntaxError: Unexpected closing tag "CodeSurfer". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags (1:1)
> 1 | <CodeSurfer>
    | ^
  2 | 
  3 | ```js
  4 | console.log(1);

Expected behavior:

If this is valid MDX it should parse it and format it without an error.

Activity

  1. thorn0 commented on Nov 24, 2019

    @thorn0
    Member

    What did the MDX spec say about this?

  2. added
    lang:mdx
    on Nov 24, 2019
  3. karlhorky commented on Nov 24, 2019

    @karlhorky
    Contributor

    Hm, there is no mention of it from my quick glance at https://github.com/mdx-js/specification, but in the issues there is mdx-js/specification#11, which mentions:

    Question:

    Is it possible to nest markdown within a JSX tag?

    Answer:

    This will work if you add newlines between the JSX and Markdown tags

    <Reference title='JS Reference'>
    
    My explanation of `this` 
    
    </Reference>

    It seems that in Prettier, this example does not work.

    Furthermore, it seems as if nesting other Markdown after newlines generally does not work:

    <div>
    
    Some `code`
    
    </div>

    Error:

    SyntaxError: Unexpected closing tag "div". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags (1:1)
    > 1 | <div>
        | ^
      2 | 
      3 | Some `code`
      4 | 
    

    This does work using the markdown parser.

  4. karlhorky commented on Nov 24, 2019

    @karlhorky
    Contributor

    Another mention of it in mdx-js/mdx#628 (admittedly, an RFC):

    <Note>
    
    This *is* **markdown**!
    
    </Note>

    Motivation:

    This approach is currently being used in the wild and in production.

    As jxnblk mentioned previously this approach comes with the benefit that it's in the Commonmark Spec.

    https://spec.commonmark.org/0.29/#example-136
    https://spec.commonmark.org/0.29/#example-137

  5. karlhorky commented on Nov 24, 2019

    @karlhorky
    Contributor

    cc @pomber maybe worth following along here, since your Code Surfer library is relying upon this approach.

  6. karlhorky commented on Nov 24, 2019

    @karlhorky
    Contributor

    One last link: in the Try it editor on the official MDX website, both of the examples work - both the example with the div and the original example with the CodeSurfer component.

  7. thorn0 commented on Nov 24, 2019

    @thorn0
    Member

    The MDX support in Prettier is really basic. Prettier parses MDX like plain Markdown with the only (I guess) difference that if a JSX block is found, it's passed to Babel. That's it. Things like inline JSX and Markdown inside JSX blocks aren't supported as of now.

  8. added
    help wanted
    on Nov 24, 2019
  9. karlhorky commented on Nov 25, 2019

    @karlhorky
    Contributor

    Prettier parses MDX like plain Markdown

    Hm, but the original example actually works with the markdown parser:

    Markdown (no errors)

    It also isn't true for the div MDX <> Markdown comparison that I posted:

    MDX (fails)

    Markdown (no errors)

    If this case was handled the same as the markdown parser (newlines inside tags), maybe this issue would be fixed.

  10. karlhorky commented on Nov 25, 2019

    @karlhorky
    Contributor

    Ah, in my comment above I missed your point about "if a JSX block is found, it's passed to Babel"... maybe that's the reason for this inconsistency?

  11. pomber commented on Nov 25, 2019

    @pomber
    Contributor

    I think it used to work, at least inside of VS Code. Are there playgrounds for older versions of Prettier?

  12. thorn0 commented on Nov 25, 2019

    @thorn0
    Member

    @pomber Depends on what you call "to work". It might kind of work again in the next version because of #6949. The SyntaxError should get fixed, but Prettier won't really format JSX blocks with Markdown inside. It'll just leave them as is like it currently does with inline JSX. So if someone here is blocked by this issue, consider installing Prettier directly from GitHub (npm install prettier/prettier) until this fix is released.

    To my knowledge, there is no playgrounds for older versions.

  13. pomber commented on Nov 25, 2019

    @pomber
    Contributor

    Yes, maybe it was just ignoring the JSX's children. That was good enough for me.

    BTW, I tried 6949 playground and it's still throwing a similar error.

  14. thorn0 commented on Nov 25, 2019

    @thorn0
    Member

    It's a different error though. It happens only on the playground.

  15. 14 remaining items

  16. JounQin commented on Jan 27, 2020

    @JounQin
    Member

    @wooorm We're asking help for sharing ideas about maintaining mdx support in prettier.

  17. karlhorky commented on Jan 27, 2020

    @karlhorky
    Contributor

    Sure, although it has been fixed actually. 🤣

    Where has this been fixed? In an unreleased version of Prettier?

    Ah actually, thanks to @kachkaev's comment (#7176 (comment)), I just tried adding "prettier": "prettier/prettier" to my devDependencies and it seems to format with all examples here! 🚀

    Show gif

    Kapture 2020-01-27 at 11 23 34

    So it looks like this will be fixed as soon as Prettier 1.20.0 or 1.19.2 is released. 🙌

    Workaround for now:

    Change your version of Prettier in your package.json to "prettier": "prettier/prettier". If you're using VS Code, this new local version will be picked up automatically by prettier-vscode.

  18. changed the title [-]MDX parsing error: Unexpected closing tag[/-] [+]MDX: support Markdown inside JSX blocks[/+] on Feb 27, 2020
  19. karlhorky commented on Apr 9, 2020

    @karlhorky
    Contributor

    Seems like this change that worked with the Prettier master branch before [email protected] is not a part of the v2 release 😞

    Screen Shot 2020-04-09 at 21 15 23

    Playground

    Screen Shot 2020-04-09 at 21 15 49

    Playground

  20. bsgreenb commented on May 13, 2020

    @bsgreenb

    This issue has been solved for me via npm i -D prettier@latest

  21. thorn0 commented on May 13, 2020

    @thorn0
    Member

    @bsgreenb It might seem that it works sometimes, but this never was implemented.

    In this code

    <CodeSurfer>
    
    ```js
    console.log(1);
    ```
    
    </CodeSurfer>
    

    the tags aren't parsed as JSX at all. Prettier just leaves them as is, without formatting.

    Whereas in this code

    <CodeSurfer>
    ```js
    console.log(1);
    ```
    </CodeSurfer>
    

    Prettier parses the tags as JSX, but it doesn't parse the text inside as Markdown.

  22. Kosai106 commented on Sep 12, 2020

    @Kosai106

    I just encountered the same issue. Oddly enough, using --require-pragma seems to make the error go away, at least on the Prettier Playground.

    That said, I updated Prettier in my repo and that seems to have worked as well in my case.

  23. karlhorky commented on Sep 12, 2020

    @karlhorky
    Contributor

    @Kosai106 --require-pragma will ignore all files without a pragma at the top of the file. MDX pragma:

    <!-- @prettier -->

    So it was just disabling Prettier for that file. If you try with --require-pragma and the pragma above, you'll have the same errors again.

    Ref: https://prettier.io/docs/en/options.html#require-pragma

  24. Kosai106 commented on Sep 13, 2020

    @Kosai106

    @karlhorky Ah, yeah that would explain it, lol. It was pretty late, I didn't think too much about it.

    Like I said however, upgrading the Prettier version seems to have solved the issue on my end.

  25. kachkaev commented on Feb 1, 2022

    @kachkaev
    Member
  26. kachkaev commented on Feb 1, 2022

    @kachkaev
    Member

    Let’s continue the discussion here:

  27. locked as resolved and limited conversation to collaborators on Mar 28, 2023
  28. added theissue type on Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedlang:mdx

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions