Variables
Variables let you customize your Markdoc documents at runtime.
Here I am rendering a custom {% $variable %}
You can pass variables in a few ways:
- Through the
variables
field on yourConfig
- Via the
variables
attribute on apartial
tag. - Manually from within your
Node
orTag
transform
functions.
Global variables
Here's an example of how you can pass variables to your config:
const doc = ` {% if $flags.my_feature_flag %} Username: {% $user.name %} {% /if %} `; const config = { variables: { flags: { my_feature_flag: true }, user: { name: 'Dr. Mark' } } }; const ast = Markdoc.parse(doc); const content = Markdoc.transform(ast, config);
which you can then access within your document:
{% if $flags.my_feature_flag %} Username: {% $user.name %} {% /if %}
With partials
To pass variables to a partial, set the variables
attribute:
{% partial variables={sdk: "Ruby", version: 3} file="header.md" /%}
and access the value within your partial file the same way you would a regular variable:
SDK: {% $sdk %} Version: {% $version %}
Caveats
Markdoc doesn't support passing variables to certain nodes, such as the href
of a link
Node. Instead, pass your variable to the href
attribute of a custom link
Tag.