GitXplorerGitXplorer
n

prettier-plugin-mdx-no-text-child

public
2 stars
0 forks
11 issues

Commits

List of commits on branch master.
Unverified
68a81f05365657b3f4c59db8b7c097f9e167bc1e

Chore: README fixes

nncuillery committed 5 years ago
Verified
6d076e848d27ea3882b2c2d134fd2be04f9e2c16

Add CI status badge

nncuillery committed 5 years ago
Unverified
eb9db538ba1e5a1ce8be7f9f70bd0ef3932cd501

Add CI workflow using Github actions

nncuillery committed 5 years ago
Unverified
9803a4e213034d216b450c0da539061247029e23

Initial commit

nncuillery committed 5 years ago

README

The README file for this repository.

CI Coverage Status npm

Usage

Add this plugin as a dev dependency:

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

# or

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 markup JSX result
<Info>

This is a **paragraph**.

</Info>
<Info>
  <p>
    This is a <strong>paragraph</strong>.
  </p>
</Info>
MDX markup JSX 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 markup JSX 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 also need more test cases, there are a LOT of untested situations where the formatting can go wrong.