Getting started
Get started with @mantine/code-highlight package
Docs
Package
License
Installation
After installation import package styles at the root of your application:
Adapters
@mantine/code-highlight package does not depend on any specific code highlighting library.
You can choose one of the default adapters provided by the package or create your own.
Default adapters:
createShikiAdapter– creates shiki adaptercreateHighlightJsAdapter– creates highlight.js adapterplainTextAdapter– does not highlight code, just displays it as plain text (used by default if no adapter is provided)
Usage with shiki
Shiki library provides the most advanced syntax highlighting for TypeScript and CSS/Sass code. It uses textmate grammars to highlight code (same as in VSCode). The Shiki adapter is recommended if you need to highlight advanced TypeScript (generics, jsx nested in props) or CSS code (custom syntaxes, newest features). The Shiki adapter is used for all code highlighting in Mantine documentation.
To use the shiki adapter, you need to install the shiki package:
Then wrap your app with CodeHighlightAdapterProvider and provide createShikiAdapter as the adapter prop:
Lazy languages loading
By default, shiki downloads grammars of all languages given to the highlighter before
the first code block is highlighted. To load grammars on demand, pass resolveLanguage option
to createShikiAdapter. It is called with the language that is not loaded yet, its return value
is passed to shiki highlighter.loadLanguage. Code is displayed as plain text until the grammar is loaded:
Highlighters created with shiki/core do not bundle grammars – return a function that imports
the grammar of the given language instead:
If resolveLanguage returns null or undefined, the language is considered unsupported and
its code is displayed as plain text.
Unavailable languages
If a code block uses a language that is not loaded in the highlighter and cannot be loaded on
demand, its code is displayed as plain text and a warning is logged to the console in development.
To fix it, add the language to the langs option of the highlighter or load it on demand
with resolveLanguage.
Usage with highlight.js
Highlight.js provides less accurate highlighting compared to shiki, but it has a smaller bundle size and better performance. Choose the highlight.js adapter if you need to highlight basic JavaScript, HTML, and CSS code.
To use the highlight.js adapter, you need to install the highlight.js package:
Then wrap your app with CodeHighlightAdapterProvider and provide createHighlightJsAdapter as the adapter prop:
Then you need to add styles from one of the highlight.js themes to your application.
You can do that by importing a css file from the highlight.js package or adding it via
a CDN link to the head of your application:
Create custom adapter
You can create a custom adapter if you want to enhance the default behavior of code highlighting or use a different library.
Example of creating a custom shiki adapter with custom themes and logic: