Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ncuillery/prettier-plugin-mdx-no-text-child

Prettier plugin to enforce that MDX tags are always separated from their child by a blank line
https://github.com/ncuillery/prettier-plugin-mdx-no-text-child

mdx mdx-js prettier prettier-plugin

Last synced: 22 days ago
JSON representation

Prettier plugin to enforce that MDX tags are always separated from their child by a blank line

Awesome Lists containing this project

README

        

![CI](https://github.com/ncuillery/prettier-plugin-mdx-no-text-child/workflows/CI/badge.svg?branch=master) [![Coverage Status](https://coveralls.io/repos/github/ncuillery/prettier-plugin-mdx-no-text-child/badge.svg?branch=master)](https://coveralls.io/github/ncuillery/prettier-plugin-mdx-no-text-child?branch=master) [![npm](https://img.shields.io/npm/v/prettier-plugin-mdx-no-text-child)](https://www.npmjs.com/package/prettier-plugin-mdx-no-text-child)

# Usage

Add this plugin as a dev dependency:

```bash
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:

```mdx
# This is the header

This is an info block.

```

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

MDX markupJSX result


```mdx

This is a **paragraph**.

```


```jsx


This is a paragraph.

```

MDX markupJSX result


```mdx

This is **not** a paragraph.

```


```jsx

This is **not** a paragraph.

```

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


```mdx

This is not a **paragraph**.

But **this one** is.

````

```jsx

This is not a **paragraph**.


But this one is.

````

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:

```diff

This is not a **paragraph**.

But **this one** is.
+

```

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:

```diff

+
This is not a **paragraph**.

But **this one** is.
+

```

# 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.