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

Global <MDX> or md`` function support #736

Closed
wangyi7099 opened this issue Aug 18, 2019 · 9 comments
Closed

Global <MDX> or md`` function support #736

wangyi7099 opened this issue Aug 18, 2019 · 9 comments
Labels
🙅 no/wontfix This is not (enough of) an issue for this project

Comments

@wangyi7099
Copy link

Subject of the feature

I have known mdx has supported global component , like so:

<MDXProvider components={/* global components*/}></MDXProvider>

Is there a way to support global function ?

Problem

I want to create a markdown parse function so that I can parse markdown anywhere in mdx.
Likes so:

// global markdown parse function
function mdParse() {
// .......
}

And in a markdown

<AlertComponent title={mdParse`# hellow world!`}/>

But now, I can only use the tag format, like so:

<AlertComponent title={<MD># hellow world!</MD>}/>
function MD({children}) {
 return mdParse(children)
}

It is not convenient!

Is there a change? Thanks!

Expected behavior

<MDXProvider functions={{mdParse}}></MDXProvider>

In markdown:

<AlertComponent title={mdParse`# hellow world!`}/>
@wangyi7099 wangyi7099 added 🙉 open/needs-info This needs some more info 🦋 type/enhancement This is great to have labels Aug 18, 2019
@johno
Copy link
Member

johno commented Aug 18, 2019

Right now you can achieve this with blank lines after the opening and closing tags (as specified by CommonMark):

<AlertComponent>

# Hello, world!

</AlertComponent>

The blank lines "reopen" markdown parsing. Does this fit into what you're wanting to achieve?

@wangyi7099
Copy link
Author

@johno Thanks for the reply!
I'm afraid not. What I mean is that I can pase the markdown anywhere. Like so :

<Button/> md`#hello` 

Using a function seems to be more flexible, and also what if I want to do some other things or pass more markdown as props to more complex component ? Like so:

<Model head={md`#head`} body={md`#body`}>
</Model>

@johno johno added 💬 type/discussion This is a request for comments 🔍 status/open 🦋 type/enhancement This is great to have and removed 🙉 open/needs-info This needs some more info 🦋 type/enhancement This is great to have labels Aug 18, 2019
@johno johno changed the title Global function support Global <MDX> or md`` function support Aug 18, 2019
@johno
Copy link
Member

johno commented Aug 18, 2019

Hmm, yeah I see. 👍

We've discussed this before (though mainly as a <MDX> component). This won't be something we'll look at implementing until after #628. But, I can see how providing more granular usage for MDX strings could be more flexible.

Also, if anyone has thoughts on how this could best be achieved/implemented please chime in!

@wooorm
Copy link
Member

wooorm commented Apr 23, 2020

I think <Button/> md`#hello` is a bit weird: After a button, I would expect more Markdown, not JavaScript?! In an expression, sure: <Button/> {md`#hello`}. But <Button/> *hello* should be a button followed by emphasized text, not something magic?

If we’re going with that, then md can be implemented in userland: it could be a library that users import at the top, and which transforms the given markdown to <h1>hello</h1>.

@johno
Copy link
Member

johno commented May 21, 2020

Really appreciate the suggestion but with v2 we’re going to support much more powerful interleaving but no inline MDX support for expressions. Though this should be achievable with a Babel macro.

@johno johno closed this as completed May 21, 2020
@wooorm wooorm added 🙅 no/wontfix This is not (enough of) an issue for this project and removed 💬 type/discussion This is a request for comments 🔍 status/open 🦋 type/enhancement This is great to have labels Jul 23, 2020
@felipecrs
Copy link

felipecrs commented Apr 12, 2024

I'm REALLY struggling to make something like the below work:

import data from "./changelog.json";

From: {data.from}  
To: {data.to}  
Updated: {data.updated}  

## Releases

{data.releases.map((release) => (

<div>

## ${release.tag}

Date: ${release.date}  

</div>

))}

But I just can't make the contents generated by the map be parsed as markdown.

I would super appreciate any ideas.

@wangyi7099

This comment has been minimized.

@remcohaszing
Copy link
Member

You can create 2 MDX files.

{/* release.mdx */}
<div>

  ## ${props.release.tag}

  Date: ${props.release.date}  

</div>
import data from "./changelog.json";
import Release from './release.mdx';

From: {data.from}  
To: {data.to}  
Updated: {data.updated}  

## Releases

{data.releases.map((release) => <Release release={release} />)}

@felipecrs
Copy link

Oh I didn't need the <div> btw, it was just so accorn wouldn't freak out.

And it worked. SUPER thank you @remcohaszing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🙅 no/wontfix This is not (enough of) an issue for this project
Development

No branches or pull requests

5 participants