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

Interpolate variables in the MDX #5700

Closed
apostololeg opened this issue Oct 13, 2021 · 5 comments
Closed

Interpolate variables in the MDX #5700

apostololeg opened this issue Oct 13, 2021 · 5 comments
Labels
closed: wontfix A fix will bring significant overhead, or is out of scope (for feature requests) feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future.

Comments

@apostololeg
Copy link

Thank you all for Docusaurus, it's really easy and understandable 🙌

💥 Proposal

Following the #395 (comment)
we are really need ability to interpolate variables in MDX markup of *.md files.

For example:

const data = "azaza";

// we wish this:
'''jsx
  const data = "{data}";
'''

// to become:
'''jsx
  const data = "azaza";
'''
@apostololeg apostololeg added status: needs triage This issue has not been triaged by maintainers proposal This issue is a proposal, usually non-trivial change labels Oct 13, 2021
@slorber
Copy link
Collaborator

slorber commented Oct 14, 2021

MDX can already use variables in content: import the data and render it with a JSX node:

# title

some text

import data from "@site/static/data.json"

<span>Some data: {data.myData}</span> 

The only thing we propose in the referenced issue is to configure globally the "mdx scope" so that the data variable is available in all your docs without the need to import it.


If you want to use this data inside a md code block, it's a different story

You'd have to use some <code> jsx instead of ``` syntax, maybe something like this:

# title

some text

import data from "@site/static/data.json"

<code>{`const data = "${data.myData}"}</code> 

Now, if you want to add a custom syntax for placeholders/variables to reduce verbosity and simplify usage, the way to go is to build your own Remark plugin to pre-process the markdown docs and replace placeholders/variables with your values (provided as plugin options)

The Remark ecosystem may already have such a plugin, but I couldn't find a good one.

Maybe try this one: https://github.com/mrzmmr/remark-variables

If someone wants to build such a Remark plugin, I'd be happy to provide support and eventually add it to Docusaurus core if it makes sense. In my opinion, it must be a generic Remark plugin (that other frameworks could use as well) and it should not be developed and battle-tested in Docusaurus core.

Technically, you can pass remark plugins as plugin options, and Docusaurus isn't blocking you from implementing this feature yourself.

@Josh-Cena Josh-Cena added feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future. closed: wontfix A fix will bring significant overhead, or is out of scope (for feature requests) and removed proposal This issue is a proposal, usually non-trivial change status: needs triage This issue has not been triaged by maintainers labels Oct 29, 2021
@derekm
Copy link

derekm commented Nov 15, 2021

For variables in code blocks, see here: mdx-js/mdx#1095 (comment)

@JasonZhLiang
Copy link

@slorber do you have a code example of that remark plugin to substitute the placeholder in MD files?

@slorber
Copy link
Collaborator

slorber commented Dec 14, 2022

No official example I actually tried to share but I guess this could be an example:
https://github.com/angeloashmore/gatsby-remark-find-replace/blob/master/src/index.js

(just found this with Google)

@JasonZhLiang
Copy link

@slorber thanks a lot, you save my day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed: wontfix A fix will bring significant overhead, or is out of scope (for feature requests) feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future.
Projects
None yet
Development

No branches or pull requests

5 participants