prettier-plugin-mdx-no-text-child

0.1.0 • Public • Published

Usage

Add this plugin as a dev dependency:

NPMYarn
$ npm i --dev prettier-plugin-mdx-no-text-child
yarn add -D prettier-plugin-mdx-no-text-child

And that's it, Prettier will automatically load the plugin based on the package name (prettier-plugin-*).

General Purpose

MDX is a markdown extension that allows insertion of JSX inside the Markdown markup. This is especially useful for referencing custom component.

Example:

# This is the header
 
<Info>
 
This is an info block.
 
</Info>

In MDX, the line break after the opening tag is meaningful and define what the child node is:

MDX markupJSX result
<Info>
 
This is a **paragraph**.
 
</Info>
<Info>
  <p>
    This is a <strong>paragraph</strong>.
  </p>
</Info>
MDX markupJSX result
<Info>
This is **not** a paragraph.
</Info>
<Info>
  This is **not** a paragraph.
</Info>

As custom components are often used in MDX to wrap other elements (i.e. an image with a caption), this behavior leads to mistakes and unexpected results.

Example:

MDX markupJSX result
<Info>
This is not a **paragraph**.
 
But **this one** is.
</Info>
 
<Info>
  This is not a **paragraph**.
  <p>
    But <strong>this one</strong> is.
  </p>
</Info>

See this issue for more context: https://github.com/mdx-js/mdx/issues/628

Prettier

Prettier supports MDX since the version 1.15.0. By default, Prettier formats the previous example as is:

<Info>
This is not a **paragraph**.
 
But **this one** is.
+ 
</Info>

Prettier inserts a line break before the closing tag, but it doesn't insert a line break after the opening tag because it would change the meaning of the code (and Prettier is only about formatting).

Well, this plugin does:

<Info>
+ 
This is not a **paragraph**.
 
But **this one** is.
+ 
</Info>

Roadmap

This is a 0.x version because this plugin is currently just smart enough to suit my own needs.

To enter 1.0, it needs to be more flexible and have options to exclude specific tags, or insert line breaks only if there are multiple children.

It more need more test cases, there are a LOT of untested situations where the formatting can go wrong.

Readme

Keywords

none

Package Sidebar

Install

npm i prettier-plugin-mdx-no-text-child

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

19.8 kB

Total Files

30

Last publish

Collaborators

  • ncuillery