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.

[idea] Parse fenced code block metastrings as JSX props

See original GitHub issue

I originally brought this up in https://github.com/mdx-js/mdx/issues/464#issuecomment-474018084 and again in https://github.com/system-ui/theme-ui/issues/212#issuecomment-516951612, where it was suggested that I open a new issue. Here’s the pitch:

Fenced code blocks should be able to specify “first-class” JSX props that get passed down to the MDX tag. Currently, everything after the language identifier is passed along via the metastring prop, which is then passed as key/value strings, so this:

```js live foo=bar
```

…produces a remark code node with properties: {live: true, foo: 'bar'}, which is great! However, the key/value parser doesn’t support anything with strings, such as foo="bar baz". When you test this in the MDX playground:

```jsx live foo=1 bar="baz 2"
```

…it produces a node with properties: {live: true, foo: '1', bar": '"baz', '2"': true}, which is clearly not right. Rather than inventing a new parser for this, I think it would be best to just treat the metastring as JSX props and either parse them “properly” (i.e. with Babel) at build time or inline them directly in the resulting JSX during the HAST → JSX compilation phase. For instance, I would expect the above example to compile to:

<MDXTag tag="pre" className="language-jsx" live foo={1} bar="baz 2" />

Or, to prevent breakage of existing components, pass them in via a separate prop:

<MDXTag tag="pre" className="language-jsx"
  meta={{live: true, foo: '1', bar: 'baz 2'}} />

As a proof of concept, I’ve hacked together remark-fenced-props to show how at least the remark-level transformation of metastringprops could work. It’s very much not production-ready or backward compatible with the key/value pair syntax, but it could easily be made so with a replacement like:

metastring.replace(/(\w+)=([^ ]+)/g, (_, key, value) => {
  return isNaN(value) ? `${key}="${value}"` : `${key}={${value}}`
})

Anyway, lmk what you think!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
RichiCoder1commented, Aug 7, 2019

I just ran into this and assumed (wrongly) that this was how it worked. I’d love to see this and would also be willing to do the above PR.

2reactions
didaktiocommented, Mar 31, 2022

It does. I think you should open a question with a reproduction of your original problem!

Alright. I’ll play around a bit and open one if no progress results 😃

Thanks for the rapid responses. You’ve got an insanely massive body of work here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fenced Code Block Usage - Documentation | NextBook
It's built with MDX, Next.js and React and works by compiling markdown or MDX down to ... Below are the ways you can...
Read more >
Use <code> or similar tags in ReactJS using JSX
Explanation: Parsing your input starts at <pre> in JSX mode. ... If you want that code as literal block, you'll need to use...
Read more >
Changelog for pandoc-2.13 - Hackage - Haskell.org
Fix bug parsing fenced code blocks (#5304). Previously parsing would break if the code block contained a string of backticks of sufficient length...
Read more >
Custom Code Blocks with MDX & Gatsby | Anna Rossetti
The first line in a fenced code block is parsed as a metadata string. ... import Highlight, { defaultProps } from "prism-react-renderer";.
Read more >
Write technical tutorials in style! How to take advantage of ...
Build a tutorial blog with React and Markdown. Enhance your blog's user ... What Markdown does is convert that fenced block of code...
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