Getting started
Get started with @mantine/code-highlight package
Package
License
Installation
After installation import package styles at the root of your application:
Adapters
@mantine/code-highlight does not depend on a specific highlighting library.
Choose one of the built-in adapters or create your own:
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 uses the same textmate grammars as VSCode and gives the most accurate highlighting. It is recommended for TypeScript and CSS code and is used in Mantine documentation.
Install the shiki package:
Then wrap your app with CodeHighlightAdapterProvider and provide createShikiAdapter as the adapter prop:
Lazy languages loading
By default, shiki loads grammars of all languages passed to the highlighter upfront.
To load grammars on demand, pass resolveLanguage option to createShikiAdapter.
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
Code in a language that is not loaded is displayed as plain text, a warning is logged in development.
To fix it, add the language to the langs option of the highlighter or load it with resolveLanguage.
Usage with highlight.js
Highlight.js is less accurate than shiki but has a smaller bundle size. Choose it for basic JavaScript, HTML and CSS code.
Install the highlight.js package:
Then wrap your app with CodeHighlightAdapterProvider and provide createHighlightJsAdapter as the adapter prop:
Then add one of the highlight.js themes, either as a css import from the highlight.js package or as a CDN link:
Create custom adapter
Create a custom adapter to use another library or to extend the default behavior. Example of a shiki adapter with custom themes and transformers: