Reusable snippets and variables
Write repeated content once and reference it everywhere — snippet files, parameterized snippets, page variables, and conditional content.
Snippet files
A snippet is just a regular .mdx / .md file that can contain any Markdown and components. By convention they live in the snippets/ directory at the project root. Here's this site's snippets/install-cli.mdx:
<CodeGroup>
```bash npm
npm install -g bitidocs
```
```bash pnpm
pnpm add -g bitidocs
```
```bash yarn
yarn global add bitidocs
```
</CodeGroup>Reference with a component
<Markdown src="/snippets/install-cli.mdx" />npm install -g bitidocspnpm add -g bitidocsyarn global add bitidocs<Snippet> and <Include> are aliases of <Markdown>; the path prop can also be written as file or path. Path resolution rules:
| Syntax | Resolves to |
|---|---|
/snippets/x.mdx (or @/snippets/x.mdx) | A path under the project root |
./x.mdx, ../shared/x.mdx | Relative to the current file |
x.mdx | snippets/x.mdx at the project root |
Reference with import
You can also import a snippet and use it as a component:
import InstallCli from "/snippets/install-cli.mdx"
<InstallCli />Only .md / .mdx files can be imported, and only default imports are supported; importing a JavaScript module reports an mdx/import error. Snippets can reference other snippets in turn (up to 5 levels deep).
Parameterized snippets
Extra props passed to a snippet replace placeholders in the snippet body. Since {…} is an expression in MDX, placeholders in snippets must be escaped as \{name\}:
<Info title="环境要求 · Requirements">
Node.js ≥ \{node\} · `biti --version` ≥ \{cli\}
</Info><Markdown src="node-requirement.mdx" node="22.13" cli="0.1.0" />Node.js ≥ 22.13 · biti --version ≥ 0.1.0
Placeholders without a passed-in value are shown as-is.
If you write an unescaped {node} directly in a snippet, MDX evaluates it as an expression; node isn't a known variable, so the expression is dropped with an mdx/expression warning, and no replacement can happen.
Page variables
Declare literals with export const at the top of the page, then use them in the body or in component props:
export const cliVersion = "0.1.0"
export const minNode = "22.13"
The current CLI version is {cliVersion} and requires Node.js {minNode} or later.
<Badge intent="success">{"v" + cliVersion}</Badge>The current CLI version is 0.1.0 and requires Node.js 22.13 or later.
v0.1.0Variable values can only be literals: strings, numbers, booleans, arrays, plain objects, template strings, plus + concatenation between them, ternary expressions, and member access (such as {plans.pro.price}). Functions, classes, regular expressions, and any calls are not allowed; see Safe subset.
Conditional content
<If> (alias <Conditional>) decides whether to render content based on the locale, version, or product of the current page, without producing a wrapper element. It's especially useful when a snippet is shared across locales or versions:
<If locales={["zh"]}>
This text only appears on Chinese pages.
</If>
<If versions={["v0"]}>
You're reading the legacy docs.
</If>
<If versions={["v0"]} not>
This only appears outside v0.
</If>roles never match.Snippets and translations
Snippet files aren't split by locale: Chinese and English pages reference the same file. When you need multiple locales, there are two approaches:
- Keep locale-specific text in the pages, and put only locale-independent content such as code in snippets (like this site's
install-cli.mdx). - Wrap paragraphs for different locales in
<If locales>inside the snippet.