Code blocks
Rspress uses Shiki for syntax highlighting at compile time, which means better runtime performance.
When using code blocks in multiple languages, the corresponding language is automatically detected at compile time, and the runtime bundle size does not increase. For supported programming languages, refer to the Shiki supported languages list.
Basic usage
You can use the ``` syntax to create code blocks. For example:
It renders as:
Code block title
You can use the title="..." attribute to add a title to a code block.
It renders as:
File code block
You can use the file="./path/to/file" attribute without writing any code block content to reference the text from an external file.
Relative paths
Use relative paths starting with ./ or ../ to reference files relative to the current MDX file:
It renders as:
Absolute paths with / prefix
Use the / prefix to reference files using absolute paths relative to the docs directory. This is useful when you need to reference shared code files from different locations in your documentation:
For example, if your docs directory is /project/docs, then /components/Button.tsx will resolve to /project/docs/components/Button.tsx.
Absolute paths with <root>/ prefix
Use the <root>/ prefix to reference files using absolute paths relative to the project root directory. This is useful when you need to reference shared code files from different locations in your documentation:
For example, if your project root is /project, then <root>/src/components/Button.tsx will resolve to /project/src/components/Button.tsx.
When using external file code blocks, it's common to use them together with route conventions. Name files starting with _.
Notation line highlight
Use // [!code highlight] comments to highlight code lines. This syntax is enabled by default; no transformer configuration is required.
It renders as:
Meta line highlight
When using meta info for line highlighting, be aware that formatting tools may change line numbers. For maintainability, Notation Line Highlight is recommended.
You can use @rspress/core/shiki-transformers with transformerCompatibleMetaHighlight and meta info comments to highlight code lines.
It renders as:
Show code line numbers
You can show line numbers for individual code blocks using the lineNumbers meta attribute:
It renders as:
You can also enable line numbers globally by setting the showLineNumbers option in the config file:
When showLineNumbers is enabled globally, all code blocks will show line numbers by default. You can disable line numbers for a specific code block by using lineNumbers=false:
Wrap code
You can enable code wrapping for individual code blocks using the wrapCode meta attribute:
It renders as:
You can also enable code wrapping globally by setting the defaultWrapCode option in the config file:
When defaultWrapCode is enabled globally, all code blocks will wrap long lines by default. You can disable code wrapping for a specific code block by using wrapCode=false:
Code block height
You can control the height behavior of code blocks using the height and fold meta attributes. There are three cases:
fold: Collapsible code block with an expand button. Height defaults to 300px.height=X: Fixed height with vertical scrollbar. If combined withfold, the code block will be collapsible at the specified height instead.- No meta: Fully expanded by default. You can change this globally via
markdown.defaultCodeOverflow.
Fold
Use the fold attribute to enable expand/collapse functionality. You can also customize the collapsed height via the height attribute (in pixels, default 300):
It renders as:
The expand/collapse button will not be shown when the actual content height is less than height.
Scroll
Use the height attribute alone (without fold) to set a fixed height with a vertical scrollbar:
It renders as:
Combining meta attributes
You can combine multiple meta attributes together:
It renders as:
Diff code block
It renders as:
Shiki transformers
Rspress uses Shiki for compile-time code highlighting, providing flexible code block capabilities.
Rspress includes several built-in transformers from @shikijs/transformers. You can also add custom transformers via markdown.shiki.transformers.
transformerNotationWordHighlight
Use // [!code word:text] to highlight matching words:
It renders as:
transformerNotationDiff
It renders as:
transformerNotationErrorLevel
It renders as:
transformerNotationFocus
It renders as:
Twoslash
Twoslash is a markup format for TypeScript code, suitable for creating self-contained code samples and letting the TypeScript compiler automatically supplement type information and hints. It is widely used on the official TypeScript website.
Rspress provides the @rspress/plugin-twoslash plugin, which enables Twoslash features in Rspress. See @rspress/plugin-twoslash documentation for details.
Runtime syntax highlighting
When you need to render code blocks dynamically at runtime, such as in interactive docs or fetching code remotely, Rspress provides the CodeBlockRuntime component.
Here is an example:
Use CodeBlockRuntime only when necessary. It increases runtime bundle size and cannot benefit from compile-time highlighting.