Rspress V2 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.
You can use the ``` syntax to create code blocks. For example:
It will be rendered as:
You can use the title="..."
attribute to add a title to a code block.
It will be rendered as:
You can use the file="./path/to/file"
attribute without writing any code block content to reference the text from an external file.
It will be rendered as:
When using external file code blocks, it's common to use them together with route conventions. Name files starting with _
.
You can use Shiki transformers with the transformerNotationHighlight
and // [!code highlight]
comments to highlight code lines.
It will be rendered as:
The backslash (\
) in [\!code highlight]
is for escaping in Markdown to show the original syntax. Do not include the backslash when actually using this syntax.
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 will be rendered as:
If you want to show code line numbers, you can enable the showLineNumbers
option in the config file:
If you want to enable long code wrapping by default, you can enable the defaultWrapCode
option in the config file:
It will be rendered as:
Rspress V2 uses Shiki for compile-time code highlighting, providing flexible code block capabilities.
You can add custom shiki transformers via markdown.shiki.transformers
for richer code block effects.
In addition to the transformerNotationHighlight mentioned above, Rspress defaults to supporting the following transformers from @shikijs/transformers.
It will be rendered as:
It will be rendered as:
It will be rendered as:
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/core/plugin-twoslash
plugin, which enables Twoslash features in Rspress. See @rspress/plugin-twoslash documentation for details.
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:
It is recommended to use CodeBlockRuntime
only when necessary, as it increases runtime bundle size and cannot benefit from compile-time highlighting performance.