mdx icon indicating copy to clipboard operation
mdx copied to clipboard

RFC: Support variable declaration (const/let/var)

Open johno opened this issue 5 years ago β€’ 4 comments

Currently, users leverage export syntax in order to instantiate local variables for usage in an MDX file. We could introduce const/let/var to better define an MDX document's API by allowing users to not need to overload exports for local variables.

Nothing else would really differ from the export syntax except that we'd read const/let/var directly.

const

const MyComponent = () => <div style={{ padding: 20, backgroundColor: 'tomato' }} />

let

let MyComponent = () => <div style={{ padding: 20, backgroundColor: 'tomato' }} />

var

var MyComponent = () => <div style={{ padding: 20, backgroundColor: 'tomato' }} />

johno avatar Apr 30 '20 21:04 johno

I think there are two alternatives (using a different style to show that they’d allow more too):

  • b) executable code
    # heading
    
    ```js run
    function MyComponent() {
      return <div style={{ padding: 20, backgroundColor: 'tomato' }} />
    }
    ```
    
    paragraph
    
  • c) expressions
    # heading
    
    {
      function MyComponent() {
        return <div style={{ padding: 20, backgroundColor: 'tomato' }} />
      }
    }
    
    paragraph
    

(note: we could also allow imports/exports in those. And even allow both syntax B && C, where syntax C would be a shortcut for B?)

wooorm avatar May 01 '20 07:05 wooorm

This feature would be much appreciated, as export const breaks Fast Refresh when its value is not a component.

aleclarson avatar Apr 26 '21 22:04 aleclarson

I'm not sure my issue related to this. I'm using MDX/React with Parcel bundler, somehow it does not interpolate React expression

export const name = "world";

# Hello {name.toUpperCase()}

{1+1}

<p>{1 + 1}</p>

And how it renders

Hello {name.toUpperCase()}
{1+1}
2

ng-hai avatar Feb 03 '22 08:02 ng-hai

@ng-hai that is unrelated to this proposal. You show MDX 2 features and Parcel does not support MDX 2 apparently.

wooorm avatar Feb 03 '22 09:02 wooorm