mdx-js / mdx

Markdown for the component era
https://mdxjs.com
MIT License
17.72k stars 1.14k forks source link

created a new recma plugin `recma-mdx-escape-missing-components` #2434

Closed talatkuyuk closed 8 months ago

talatkuyuk commented 9 months ago

Initial checklist

Problem

I created a new recma plugin namely recma-escape-missing-components. I want to support and encourage the mdx community to reach that recma plugin.

Basically, the recma-escape-missing-components;

looks for a declaration statement in an object pattern initiated by the _components

const {Component1, Component2} = _components;

converts it as the destructed properties (Components) having a default value () => null

const {Component1 = () => null, Component2 = () => null} = _components;

in order not to receive a runtime error in case that component(s) is/are not provided into MDXContent / MDXProvider.

Thanks.

Solution

Is it possible to add the plugin into the docs somewhere available.

Alternatives

Actually it is more needed to have List of Recma Plugins page like "remak plugins" and "rehype plugins".

wooorm commented 9 months ago

Nice!

There's a list of things specifically to mdx here on the website: https://mdxjs.com/docs/extending-mdx/#list-of-plugins. Want to PR it there?

remcohaszing commented 9 months ago

Nice plugin! One small tip: It would be better to declare the fallback function once, then assign it:

const _EmptyComponent = () => null;

// …

function _createMdxContent(props) {
  // …
  const {Component1 = _EmptyComponent, Component2 = _EmptyComponent} = _components;

  // …
}

This saves needless rerenders in frameworks that rely on the function identity, such as React and Preact.

talatkuyuk commented 9 months ago

Nice!

There's a list of things specifically to mdx here on the website: https://mdxjs.com/docs/extending-mdx/#list-of-plugins. Want to PR it there?

Thanks for your positive feedback @wooorm , I am going to create a PR soon.

talatkuyuk commented 9 months ago

Nice plugin! One small tip: It would be better to declare the fallback function once, then assign it:

const _EmptyComponent = () => null;

// …

function _createMdxContent(props) {
  // …
  const {Component1 = _EmptyComponent, Component2 = _EmptyComponent} = _components;

  // …
}

This saves needless rerenders in frameworks that rely on the function identity, such as React and Preact.

Thanks for your positive feedback @remcohaszing, I will try to implement as you suggested in the next version.

talatkuyuk commented 9 months ago

Hi @remcohaszing,

I added the declaration const _EmptyComponent = () => null; and published the new version v.1.0.2

wooorm commented 9 months ago

Ah, one thing I would recommend, is to use recma-mdx-escape-missing-components as a plugin name. In the future, recma plugins could be used for any javascript. This plugin is specific to MDX, so I think it’s best to make that explicit in the plugin name!

talatkuyuk commented 9 months ago

Ah, one thing I would recommend, is to use recma-mdx-escape-missing-components as a plugin name. In the future, recma plugins could be used for any javascript. This plugin is specific to MDX, so I think it’s best to make that explicit in the plugin name!

Okey, I have to delete the package from npm. I'll take care.

talatkuyuk commented 9 months ago

Hi @wooorm,

The new recma plugin recma-mdx-escape-missing-components is published as v1.0.3.

ChristianMurphy commented 9 months ago

Thanks for sharing @talatkuyuk! 👍 🎉 🙇 I'd second @wooorm's suggestion to add this to the recma plugin list.

Some optional thoughts and ideas for the project

talatkuyuk commented 9 months ago

Thanks to @ChristianMurphy for great advices.

I've created about 10 plugins. I think my plugins are small projects; actually I didn't get any PR, but only few issues for one year.

Although my thoughts, I will employ your advices in the phase of refinement of this and my all other plugins.

Thanks again.

talatkuyuk commented 8 months ago

The plugin is added into the docs. Thanks.