GitXplorerGitXplorer
n

prettier-plugin-mdx-no-text-child

public
2 stars
0 forks
11 issues

Commits

List of commits on branch master.
Unverified
b497139d604f56139f4c7d01e2c9a66b625630df

Chore: Add keywords and description

nncuillery committed 5 years ago
Unverified
ae08b4ae39e61c4282e6dd6210bcba71e70bd5ae

Chore: Add license file

nncuillery committed 5 years ago
Unverified
e83207352eabb6385505f23f5fda9747fc98948a

Chore: Add NPM version badge

nncuillery committed 5 years ago
Unverified
e95563a5269eed02dda6ba4f72eea92cc1c330a6

Chore: Run prepublish only before publishing

nncuillery committed 5 years ago
Unverified
2977ce7a1bab875c33459f9bac1ff13588ec3243

Chore: Add tests coverage

nncuillery committed 5 years ago
Unverified
c7403f6b5af63d5419aea2c5cbdfd272d1c09088

Chore: Use the source version of the plugin in tests

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.