question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

An alternative “shortcode” (`mdxType` alternative) proposal

See original GitHub issue

Currently, MDX is walking the whole JS program, and adding an mdxType prop to all JSX elements. It does this, so components can be overwritten at runtime.

Take this MDX:

* a
* b

Which turns into (simplified to get the point across, the actual is more complex):

var ol = () => throw "error"
var li = () => throw "error"

<ol mdxType="ol">
  <li mdxType="li" parentName="ol">a</li>
  <li mdxType="li" parentName="ol">b</li>
</ol>

After compiling the JSX away, you get:

var ol = () => throw "error"
var li = () => throw "error"

func(ol {mdxType: "ol"}, [
  func(li, {mdxType: "li", parentName: "ol"}, ["a"]),
  func(li, {mdxType: "li", parentName: "ol"}, ["b"]),
])

Then, the runtime, supports replacing things through components.

It has a func that ignores its first argument, instead taking mdxType (and parentName) to look whether it has a different component defined in components, and uses that instead of the either imported or defined throwing components.

The above is the simplified example for “normal” tags. The same happens for actual components (Capitalized):

var MyComponent = () => throw "error"

func(MyComponent, {mdxType: "MyComponent"}, [])

Which is then overwritten at runtime…

Even weirder is:

import MyComponent from "place"

func(MyComponent, {mdxType: "MyComponent"}, [])

// Passed in runtime:
{components: {"MyComponent": () => /* this is used instead of the imported component! */}}

This ✨ magic ✨ is complex and undocumented. I want to remove that complexity. Here’s one alternative.

We instead compile all standard markdown (html?) things to a namespace object: p becomes mdx.p, strong becomes mdx.strong, etc:

<mdx.ol>
  <mdx.li>a</mdx.li>
  <mdx.li>a</mdx.li>
</mdx.ol>

Then, in mdxContent (the wrapper function around all JSX components), we’re being passed runtime components. We take those and use them to fill mdx.p = components.p || 'p'.

This might solve #821, because the JSX <p>x</p> is not overwritten whereas the markdown paragraph x is, depending on where we implement the changing of p to mdx.p.

This then has no knowledge of JSX: there is no tree traversed for whether <MyComponent /> is imported or not. You have to import it, you can’t just pass it Youtube to components, you have to import it.

/cc @johno, @ChristianMurphy

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
wooormcommented, Dec 17, 2020

Yes, totally, abbreviated that just to be shorter, but it’s currently function MDXContent({components, ...props}) and I don’t intend changing that!

0reactions
wooormcommented, Oct 19, 2021

landed

Read more comments on GitHub >

github_iconTop Results From Across the Web

keO - ALBA.Net
Sprite slam dunk logo, Sep retirement plan self employed, Dc's bar iowa city ... Wp post to pdf shortcode, Buddha charita ncert, Vu...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found