Parametrised markdown code blocks? #2288
-
I'm trying to understand how I can use variables in various markdown constructs, and I noticed that they work in some cases, like headers, lists, and explicit html code blocks: export const world = 'World'
Hello {world}!
# Hello {world}!
- Hello {world}!
One <code>Hello {world}!</code> Two However I could not make them work in markdown code blocks: export const world = 'World'
Three `Hello {world}!` Four
```bash
Five
Hello {world}!
Six
``` Am I missing something, or this is not yet implemented? |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 34 replies
-
It is indeed not implemented. |
Beta Was this translation helpful? Give feedback.
-
Code may often contain syntax that would conflict with such interpolation. For example, consider the following MDX: This is a destructuring example in JavaScript:
```js
const { name } = person
``` I think it would come as a total surprise if |
Beta Was this translation helpful? Give feedback.
-
Right, the original question was about code because most of my pages include lots of code and manually rewriting them from markdown to JSX would make the biggest part of the migration effort. Also, since Docusaurus did not yet complete the migration to MDX 2, with MDX 1 I also have problems with lists, links, etc, practically the migration requires to fully rewrite every page, there is little to be reused. :-( I'll probably postpone everything until Docusaurus 3 is out, with MDX 2 and full ESM support, and then I'll re-evaluate everything. If I can automate the migration with some reasonably complex sed scripts, I'll try to convert the pages to MDX. If not, I have to decide if a Liquid pre-processing step is realistic or not.
For a new site I'd definitely go for MDX, but unfortunately I have hundreds of pages with liquid substitutions, and I'm trying to find a solution to make the migration less painful. To be exact, I have 20 smaller sites (each with 20-30 pages) and 2 large sites, each with 100+ pages.
I don't understand this. Why MDX cannot parse the page after Liquid does the textual substitutions? By the way, you have a (somehow) similar syntax problem, in JSX you use I am convinced that React/MDX/Docusaurus/etc represent a much more powerful solution, and I'd really like to do everything in the node/npm ecosystem, but unfortunately I do not have unlimited resources for this. FYI, in my desire to stay in the same ecosystem with my embedded system projects too, I created Anyway, regardless of my desire to get rid of Ruby and Jekyll, the Liquid template engine remains a very nice thing, and the JS implementation is really great. |
Beta Was this translation helpful? Give feedback.
-
I mentioned earlier that I also have difficulties to migrate lists and/or links to MDX. Any idea why the following:
is rendered (by the playground) as:
and not as an image with a link, like GitHub does for the same content (the urls are fictive): I tried to enable the
Is this the expected behaviour? |
Beta Was this translation helpful? Give feedback.
-
I think that now things are bit more clear. After Docusaurus will add support for MDX 2, hopefully most of my content will be accepted. The only remaining major issue is with code blocks, which require the In Docusaurus I saw that it is possible to pass a lot of options to code blocks, like line numbers to highlight, etc. I was wondering if it would be possible to add a new option to enable interpolation inside code blocs, something like:
I think that this would solve the problem with a simple solution while preserving the markdown syntax, avoiding thus the rewrite to html, which is more tedious and also less readable. |
Beta Was this translation helpful? Give feedback.
-
Is using JSX still the answer as of today? It isn't practical to use JSX for any non-trivial code block (because I'd need to manually recreate all code highlighting tags). In principle, something like this would work: {/* SomeMarkdown.mdx */}
This is some MDX content.
```jsx
// Interpolated
console.log('Hello !{props.name}') // Displays: console.log('Hello John')
// console.log('Hello Alice')
// Not interpolated
console.log('Hello \!{props.name}') // Displays: console.log('Hello !{props.name}')
// Not interpolated
console.log('Hello \\!{props.name}') // Displays: console.log('Hello \!{props.name}')
``` import SomeMarkdown from './SomeMarkdown.mdx'
Demo of passing parameters to code blocks.
<SomeMarkdown name="John" />
<SomeMarkdown name="Alice" /> I feel like it's one of the last piece missing for MDX to be "conceptually perfect". |
Beta Was this translation helpful? Give feedback.
-
FYI, I finally took some time and started to convert my websites to Docusaurus. The resulting code looks like this: Basically most of the code blocks were rewritten to Quite a lot of work... :-( The only good news is that I managed to organise the code as a template (a Liquid template!) that I instantiate to generate the common code for all 20+ sites, thus making maintaining all those sites less frightening. |
Beta Was this translation helpful? Give feedback.
-
I was also looking for an affordance in MDX today to "escape" from the context of a Markdown fenced code block to interpolate JSX values. Ended up creating a Added it to a new trick in |
Beta Was this translation helpful? Give feedback.
You seem to be coming from a text templating language. It converts text to other text based on interpolation. Any text can be interpolated anywhere in the document, because the content doesn’t have semantic meaning to the templating engine.
MDX is a programming language that gets compiled to JavaScript. It has certain rules of what is valid syntax, and where JavaScript expressions are allowed.
You can write dynamic links for instance in MDX, but you’ll have to use JSX syntax for that, not interpolation:
Likewise, code blo…