Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic markdown content? #391

Closed
huchenme opened this issue Feb 1, 2019 · 8 comments
Closed

Dynamic markdown content? #391

huchenme opened this issue Feb 1, 2019 · 8 comments

Comments

@huchenme
Copy link

huchenme commented Feb 1, 2019

It will be nice if markdown content could be dynamic

const firstName = 'John'
const lastName = 'Dow'
const address = 'address'

# Hello {firstName} {lastName}

## My address is {address}
@johno
Copy link
Member

johno commented Feb 6, 2019

Dynamic markdown is accessible as soon as a JSX block is opened. This includes inline JSX blocks, so you can do the following.

export const firstName = 'John'
export const lastName = 'Dow'
export const address = 'address'

# Hello <span children={firstName} /> <span children={lastName} />

<div>My address is {address}</div>

You can also access props

<pre>{JSON.stringify(props, null, 2)}</pre>

Eventually there will also be React.Fragment shorthands (currently under development):

# Hello, <>{props.name}</>

@johno johno closed this as completed Feb 6, 2019
@huchenme
Copy link
Author

Thanks @johno, could you link the issue of React.Fragment for tracking purpose? I am excited about that

@johno
Copy link
Member

johno commented Feb 11, 2019

@skylarmb
Copy link

For future googlers since this is the top result, currently this works for me:

Hello <React.Fragment children={props.name} />, nice to meet you

These do not work even though they are theoretically the same thing

Hello <React.Fragment>{props.name}</React.Fragment>, nice to meet you
Hello <>{props.name}</>, nice to meet you

@karlhorky
Copy link
Contributor

karlhorky commented Mar 11, 2020

@skylarmb what version of @mdx-js/mdx are you using? Or if you're using a different library, which library and which version?

@johno mentioned in mdx-js/specification#15 (comment) that your third example should work since v1 Alpha:

Hello <>{props.name}</>, nice to meet you

Seems like it was probably this pull request (landed in v1.0.0-alpha.9): #470


If it really is a problem, it may be worth it to submit a new issue, since this is supposed to work.

@Nebulis
Copy link

Nebulis commented Mar 11, 2020

Hi,

We face the issue as well with <></> syntax while using React.Fragment is fine.

<Preview>
  <Story name="AddressSingle">
    <AddressSingle title="Holder" name="Bank of China" address="0x28F7aB32C521D13F2E6980d072Ca7CA493020145">
      <>
        <ButtonSolid color="white" isIcon={true}>
          Some
        </ButtonSolid>
      </>
    </AddressSingle>
  </Story>
</Preview>

Error thrown by prettier is:

SyntaxError: Unexpected character ">" (8:9)
   6 | 
   7 | <Meta title="Document Info/AddressSingle" component={AddressSingle} />
>  8 | 
     | ^
   9 | # AddressSingle
  10 | 
  11 | Address Single.
    at oc (/home/nebulis/IdeaProjects/tradetrust-website/node_modules/prettier/parser-markdown.js:20:43697)
    at Pc (/home/nebulis/IdeaProjects/tradetrust-website/node_modules/prettier/parser-markdown.js:132:1736)
    at e (/home/nebulis/IdeaProjects/tradetrust-website/node_modules/prettier/parser-markdown.js:132:4098)
    at Object.parse (/home/nebulis/IdeaProjects/tradetrust-website/node_modules/prettier/parser-markdown.js:132:4955)
    at /home/nebulis/IdeaProjects/tradetrust-website/node_modules/prettier/parser-markdown.js:132:5845
    at e (/home/nebulis/IdeaProjects/tradetrust-website/node_modules/prettier/parser-markdown.js:20:23192)
    at /home/nebulis/IdeaProjects/tradetrust-website/node_modules/prettier/parser-markdown.js:20:23321
    at Array.reduce (<anonymous>)
    at e (/home/nebulis/IdeaProjects/tradetrust-website/node_modules/prettier/parser-markdown.js:20:23291)
    at mapAst (/home/nebulis/IdeaProjects/tradetrust-website/node_modules/prettier/parser-markdown.js:20:23405)
Process finished with exit code -1

The error point to line 8, while the fragment from the code I shared is located line 22.

We use [email protected] and @mdx-js/[email protected].

I can re-try tomorrow (it's late where I live) and open an issue if I am able to reproduce with a simpler example using mdx only (maybe it's a prettier issue also)

@karlhorky
Copy link
Contributor

@Nebulis does adding "prettier": "prettier/prettier" to your package.json help? This will use the master version of Prettier from GitHub.

This fixed some MDX formatting problems for me:

prettier/prettier#7041 (comment)

@Nebulis
Copy link

Nebulis commented Mar 12, 2020

@karlhorky yes it works by using "prettier": "prettier/prettier", will just wait for the new release to come out so.

Thanks for pointing this out 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants