You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature: referencing variables from markdown pages (placeholder substitution) #395
difficulty: advancedIssues that are complex, e.g. large scoping for long-term maintainability.featureThis is not a bug or issue with Docusausus, per se. It is a feature request for the future.
First of all, thanks for sharing and supporting Docusaurus! It's been pretty easy to get started.
I'm posting a feature request — I haven't found anything similar in the issue tracker so forgive me if it's been discussed before.
Motivation: sometimes docs need to reference information such as version numbers, URLs etc. and having them manually typed in the markdown pages is not ideal because this type of information often gets referenced multiple times.
Taking that into account, it would be great if Docusaurus supported passing variables into pages (from e.g. siteConfig). It already supports special Markdown markup via the <AUTOGENERATED_TABLE_OF_CONTENTS> marker, and it could potentially do something similar for variables.
For example, typing in<VAR:projectName> would get replaced with the value of siteConfig.projectName. Ideally this works in code blocks too (```).
Reacted by Selcuk Ermaya, Aleksandre Cochran, Joel Marcey, Yangshun Tay, Gabriele Petronella, Thibault Derousseaux, Yotam Barnoy, Anna Spysz, Christophe, Josh Brown and 7 more
Interesting idea. We did something similar for nuclide.io using JS and referencing the package.json file. I think this is something that could definitely be implemented for some upcoming version of Docusaurus.
Julen mentioned "version numbers, URLs, etc." but it would also be great to insert automatically computed markup from JSDoc for instance. Thus, if you decide to implement it, it would be nice to support the injection of Markdown (or HTML) markup and not only scalar values.
Ok, but just to be clear: that plugin will not be included as a dependency of Docusaurus, right? It would be configured by users in their siteConfig.markdownPlugins?
I'm just a bit confused by the fact that this is a feature request for Docusaurus when nothing actually needs to change in Docusaurus for this to be supported
const{Plugin: Embed}=require('remarkable-embed');// Our custom remarkable plugin factory.constcreateVariableInjectionPlugin=variables=>{// `let` binding used to initialize the `Embed` plugin only once for efficiency.// See `if` statement below.letinitializedPlugin;constembed=newEmbed();embed.register({// Call the render method to process the corresponding variable with// the passed Remarkable instance.// -> the Markdown markup in the variable will be converted to HTML.inject: key=>initializedPlugin.render(variables[key])});return(md,options)=>{if(!initializedPlugin){initializedPlugin={render: md.render.bind(md),hook: embed.hook(md,options)};}returninitializedPlugin.hook;};};constsiteVariables={scalar: 'https://example.com',// Since the variables are processed by Docusaurus's Markdown converter,// this will become a nice syntax-highlighted code block.markdown: ['```javascript','const highlighted = true;','```',].join('\n'),// We can use HTML directly too as HTML is valid Markdown.html: ['<details>',' <summary>More details</summary>',' <pre>Some details</pre>','</details>'].join('\n')};constsiteConfig={markdownPlugins: [createVariableInjectionPlugin(siteVariables)],// rest of the config};module.exports=siteConfig;
As you can see, by leveraging remarkable-embed, the implementation of the custom plugin for variable injection is quite small. I'm not even sure it's worth making a dedicated npm package out of it.
The only drawback I can see is that the injected markup isn't pre-processed by Docusaurus. For instance, if one of the variables contains a Markdown title such as ## Custom title, this title won't appear in the secondary on-page navigation.
Reacted by Matthew Francis Brunetti, Diana Holland, Camille Devaux, ChethanUK and Ian Hornik
Reacted by Endi, Agastya Darma, Anna Spysz, harleys-hmh and Dan Homola
This is really sweet tibdex...it is not replacing variables that are inside of a markdown code block though. I guess those are kind of regarded as literals but as I might want some code to depend on a variable, is there anything I should do?
Edit: also realized the original post requests having this feature available in code blocks
constvisit=require('unist-util-visit');constplugin=(options)=>{consttransformer=async(ast)=>{visit(ast,['text','code','link'],(node)=>{// Replace all occurrences of VAR::varName with the value of varNameletvalue;switch(node.type){case"link":
value=node.url;break;case"text":
case"code":
value=node.value;break;}value=value.replace(/VAR::([A-Z_]+)/ig,(match,varName)=>{returnoptions.replacements[varName]||match;});switch(node.type){case"link":
node.url=value;break;case"text":
case"code":
node.value=value;break;}});};returntransformer;};module.exports=plugin;
Note that you must install "unist-util-visit": "2.0.3" for any of this to work. Later versions require ESM, which is not available "here".
changed the title [-]Feature: referencing variables from markdown pages[/-][+]Feature: referencing variables from markdown pages (placeholder substitution)[/+]on Apr 7, 2023
Many thanks to @Zenahr and @alexandernst for the plugin! In testing the latest version posted I found that it doesn't work for inline code snippets. How difficult would it be to expand the plugin to cover this use case?
Many thanks to @Zenahr and @alexandernst for the plugin! In testing the latest version posted I found that it doesn't work for inline code snippets. How difficult would it be to expand the plugin to cover this use case?
Just adding support for AST node type inlineCode in addition to the already supported code type.
Many thanks to @Zenahr and @alexandernst for the plugin! In testing the latest version posted I found that it doesn't work for inline code snippets. How difficult would it be to expand the plugin to cover this use case?
constvisit=require('unist-util-visit');constplugin=(options)=>{consttransformer=async(ast)=>{visit(ast,['text','code','link'],(node)=>{// Replace all occurrences of VAR::varName with the value of varNameletvalue;switch(node.type){case"link":
value=node.url;break;case"text":
case"code":
value=node.value;break;}value=value.replace(/VAR::([A-Z_]+)/ig,(match,varName)=>{returnoptions.replacements[varName]||match;});switch(node.type){case"link":
node.url=value;break;case"text":
case"code":
node.value=value;break;case"inlineCode": // <-- THIS HANDLES INLINE CODE BLOCKS.node.value=value;break;}});};returntransformer;};module.exports=plugin;
@Zenahr you probably also need to visit inlineCode nodes no? And also add it to the first switch? Didn't try but this would look better to me:
constvisit=require(https://github.com/syntax-tree/unist-util-visit);constplugin=(options)=>{consttransformer=async(ast)=>{visit(ast,['text','code','inlineCode','link'],(node)=>{// Replace all occurrences of VAR::varName with the value of varNameletvalue;switch(node.type){case"link":
value=node.url;break;case"text":
case"code":
case"inlineCode":
value=node.value;break;}value=value.replace(/VAR::([A-Z_]+)/ig,(match,varName)=>{returnoptions.replacements[varName]||match;});switch(node.type){case"link":
node.url=value;break;case"text":
case"code":
case"inlineCode":
node.value=value;break;}});};returntransformer;};module.exports=plugin;
difficulty: advancedIssues that are complex, e.g. large scoping for long-term maintainability.featureThis is not a bug or issue with Docusausus, per se. It is a feature request for the future.
Activity
JoelMarcey commentedon Feb 27, 2018
Interesting idea. We did something similar for nuclide.io using JS and referencing the package.json file. I think this is something that could definitely be implemented for some upcoming version of Docusaurus.
tibdex commentedon May 1, 2018
That would be an awesome feature!
Julen mentioned "version numbers, URLs, etc." but it would also be great to insert automatically computed markup from JSDoc for instance. Thus, if you decide to implement it, it would be nice to support the injection of Markdown (or HTML) markup and not only scalar values.
gedeagas commentedon May 26, 2018
HI, @JoelMarcey I am planning to take this after finishing #694 but I think I will work with scalar values first. Is that okay?
JoelMarcey commentedon May 27, 2018
@gedeagas Sounds great! I think this is a feature that can be done in increments.
zenflow commentedon May 27, 2018
Isn't this another thing best left to markdown plugins?
endiliey commentedon May 27, 2018
While I agree with @zenflow, if @gedeagas intends to build his own plugin, that's welcome too☺
gedeagas commentedon May 27, 2018
Hi @zenflow @endiliey, I am planning to do just that 😄 .
Making a plugin for remarkable to support this on docusaurus 📦
zenflow commentedon May 27, 2018
Ok, but just to be clear: that plugin will not be included as a dependency of Docusaurus, right? It would be configured by users in their
siteConfig.markdownPlugins?I'm just a bit confused by the fact that this is a feature request for Docusaurus when nothing actually needs to change in Docusaurus for this to be supported
tibdex commentedon May 31, 2018
Here's how variable injection can be done:
Put this in
siteConfig.js:Then, in one of the doc page, write:
And this is what Docusaurus would render:
As you can see, by leveraging remarkable-embed, the implementation of the custom plugin for variable injection is quite small. I'm not even sure it's worth making a dedicated npm package out of it.
The only drawback I can see is that the injected markup isn't pre-processed by Docusaurus. For instance, if one of the variables contains a Markdown title such as
## Custom title, this title won't appear in the secondary on-page navigation.gedeagas commentedon Jun 2, 2018
Awesome @tibdex.
I guess my comment above is obsolete 😄
cmdcolin commentedon Aug 8, 2018
This is really sweet tibdex...it is not replacing variables that are inside of a markdown code block though. I guess those are kind of regarded as literals but as I might want some code to depend on a variable, is there anything I should do?
Edit: also realized the original post requests having this feature available in code blocks
endiliey commentedon May 14, 2019
This issue is closed due to stale activity.
If you think there is a need for it, please open up a new issue with new template.
44 remaining items
alexandernst commentedon Apr 4, 2023
In case you want to make it work for links:
Note that you must install
"unist-util-visit": "2.0.3"for any of this to work. Later versions require ESM, which is not available "here".[-]Feature: referencing variables from markdown pages[/-][+]Feature: referencing variables from markdown pages (placeholder substitution)[/+]set typedoc revision to main
docs: Fix all links in Docusaurus
docs: Fix all links in Docusaurus
docs: Fix all links in Docusaurus
docs: Fix all links in Docusaurus
docs: Fix all links in Docusaurus
alexfornuto commentedon Sep 18, 2023
Many thanks to @Zenahr and @alexandernst for the plugin! In testing the latest version posted I found that it doesn't work for
inline codesnippets. How difficult would it be to expand the plugin to cover this use case?slorber commentedon Sep 19, 2023
Just adding support for AST node type
inlineCodein addition to the already supportedcodetype.ZenaMel commentedon Sep 20, 2023
@alexfornuto see below.
slorber commentedon Sep 20, 2023
@Zenahr you probably also need to visit
inlineCodenodes no? And also add it to the first switch? Didn't try but this would look better to me:alexfornuto commentedon Sep 20, 2023
Thanks, gents! I used @slorber's version FTW. This is my derp, as I tried doing the same thing but with
preinstead ofinlineCode.de-abreu commentedon Aug 20, 2024
Evening, @slorber. I'm not familiar with Javascript and would like to know how I could install your plugin.
lunny commentedon Sep 1, 2024
Sorry to disturb everyone here. I found a workaround after a long time searching. And it's not mentioned in the above comments. Maybe useful for somebody. The documentation is https://docusaurus.io/docs/api/docusaurus-config#preprocessor
Here is an example of how to use it. https://gitea.com/gitea/docs/src/branch/main/docusaurus.config.js#L213 . This will replace all
@variable@into the value whether it's in a code block or not.