Giter Club home page Giter Club logo

prettier-plugin-mdx-no-text-child's Introduction

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 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: mdx-js/mdx#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.

prettier-plugin-mdx-no-text-child's People

Contributors

ncuillery avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

prettier-plugin-mdx-no-text-child's Issues

[Proposal] Authorize text child for some components/tags

It's impossible to have a simple badge component with a simple text child like:

// Example 1: Custom component
<Badge>WIP</Badge>

// Example 2: HTML tag
<span>This shouldn't be transformed into a block</span>

Idea 1:

Provide an option to force blocks children only if the node has multiple children. So

<Info>
This is an info block with only one text child.
</Info>

<Info>
This is an info block.

It has 2 children.
</Info>

will be formatted to:

<Info>This is an info block with only one text child.</Info>

<Info>

This is an info block.

It has 2 children.

</Info>

Idea 2

if Idea 1 is not enough, we can provide another option of type array to either:

  • prevent some tags/components from transformation (the plugin transforms everything by defaut and exceptions are provided)
  • or transform only the tags/components mentioned in this array ("optin" behavior)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.