shiki
This plugin enables syntax highlighting for markdown code fence with Shiki.
Tips
Shiki is the syntax highlighter used by VSCode. It provides higher fidelity highlighting but may be slower than Prism.js, especially when processing many code blocks.
Usage
npm i -D @vuepress/plugin-shiki@nextimport { shikiPlugin } from '@vuepress/plugin-shiki'
export default {
plugins: [
shikiPlugin({
// options
langs: ['ts', 'json', 'vue', 'md', 'bash', 'diff'],
}),
],
}Options
langs
Type:
ShikiLang[]Details: Additional languages to be parsed by Shiki.
Tips
The plugin automatically loads languages used in your markdown files, so manual specification is not required.
Also see:
langAlias
Type:
{ [lang: string]: string }Details: Custom language aliases for Shiki.
Also see:
theme
Type:
ShikiThemeDefault:
'nord'Details: Shiki theme to be applied to code blocks.
Also see:
themes
Type:
{ light: ShikiTheme; dark: ShikiTheme }Details: Dark/light dual themes for Shiki.
The styles of both themes will be injected as
--shiki-lightand--shiki-darkCSS variables to code blocks:<span style="--shiki-light:lightColor;--shiki-dark:darkColor;">code</span>Also see:
lineNumbers
Type:
boolean | number | 'disable'Default:
trueDetails: Controls the display of line numbers.
number: minimum number of lines required to enable line numbers.
For example, setting it to 4 will only enable line numbers when your code block has at least 4 lines.true: enable line numbers globally.false: disable line numbers globally.'disable': completely disable line numbers;:line-numberswill not take effect.
You can add
:line-numbers/:no-line-numbersmarkers to your fenced code blocks to override the config setting, and customize the starting number by adding=after:line-numbers. For example,:line-numbers=2will start line numbers from2.
// line-numbers are enabled
const line2 = 'This is line 2'
const line3 = 'This is line 3'// line-numbers are disabled
const line2 = 'This is line 2'
const line3 = 'This is line 3'// line-numbers are enabled and start from 2
const line3 = 'This is line 3'
const line4 = 'This is line 4'```ts:line-numbers
// line-numbers are enabled
const line2 = 'This is line 2'
const line3 = 'This is line 3'
```
```ts :no-line-numbers
// line-numbers are disabled
const line2 = 'This is line 2'
const line3 = 'This is line 3'
```
```ts :line-numbers=2
// line-numbers are enabled and start from 2
const line3 = 'This is line 3'
const line4 = 'This is line 4'
```highlightLines
- Type:
boolean - Default:
true - Details: Whether to enable code line highlighting. You can highlight specified lines by adding line range markers to your fenced code blocks:
- Line ranges:
{5-8} - Multiple single lines:
{4,7,9} - Combined:
{4,7-13,16,23-27,40}
- Line ranges:
import { defaultTheme } from '@vuepress/theme-default'
import { defineUserConfig } from 'vuepress'
export default defineUserConfig({
title: 'Hello, VuePress',
theme: defaultTheme({
logo: 'https://vuepress.vuejs.org/images/hero.png',
}),
})```ts {1,7-9}
import { defaultTheme } from '@vuepress/theme-default'
import { defineUserConfig } from 'vuepress'
export default defineUserConfig({
title: 'Hello, VuePress',
theme: defaultTheme({
logo: 'https://vuepress.vuejs.org/images/hero.png',
}),
})
```collapsedLines
Type:
boolean | number | 'disable'Default:
'disable'Details: Default behavior of code block collapsing.
number: collapse the code block starting from linenumberby default, for example,12means collapsing the code block starting from line 12.true: Equivalent to15, collapsing the code block starting from line 15 by default.false: Add support for code block collapsing, but disable it globally'disable': Completely disable code block collapsing,:collapsed-lineswill not take effect.
To override global settings, you can add the
:collapsed-lines/:no-collapsed-linesmarker to the code block. You can also add=after:collapsed-linesto customize the starting line number being collapsed, for example,:collapsed-lines=12means collapsing the code block starting from line 12.
html {
margin: 0;
background: black;
height: 100%;
}
body {
margin: 0;
width: 100%;
height: inherit;
}
/* the three main rows going down the page */
body > div {
height: 25%;
}
.thumb {
float: left;
width: 25%;
height: 100%;
object-fit: cover;
}
.main {
display: none;
}html {
margin: 0;
background: black;
height: 100%;
}
body {
margin: 0;
width: 100%;
height: inherit;
}
/* the three main rows going down the page */
body > div {
height: 25%;
}
.thumb {
float: left;
width: 25%;
height: 100%;
object-fit: cover;
}
.main {
display: none;
}html {
margin: 0;
background: black;
height: 100%;
}
body {
margin: 0;
width: 100%;
height: inherit;
}
/* the three main rows going down the page */
body > div {
height: 25%;
}
.thumb {
float: left;
width: 25%;
height: 100%;
object-fit: cover;
}
.main {
display: none;
}<!-- Collapsed by default starting from line 15 -->
```css :collapsed-lines
html {
margin: 0;
background: black;
height: 100%;
}
body {
margin: 0;
width: 100%;
height: inherit;
}
/* the three main rows going down the page */
body > div {
height: 25%;
}
.thumb {
float: left;
width: 25%;
height: 100%;
object-fit: cover;
}
.main {
display: none;
}
```
<!-- Disabled collapsed -->
```css :no-collapsed-lines
html {
margin: 0;
background: black;
height: 100%;
}
body {
margin: 0;
width: 100%;
height: inherit;
}
/* the three main rows going down the page */
body > div {
height: 25%;
}
.thumb {
float: left;
width: 25%;
height: 100%;
object-fit: cover;
}
.main {
display: none;
}
```
<!-- Collapsed starting from line 10 -->
```css :collapsed-lines=10
html {
margin: 0;
background: black;
height: 100%;
}
body {
margin: 0;
width: 100%;
height: inherit;
}
/* the three main rows going down the page */
body > div {
height: 25%;
}
.thumb {
float: left;
width: 25%;
height: 100%;
object-fit: cover;
}
.main {
display: none;
}
```codeBlockTitle
Type:
boolean | CodeBlockTitleRendertype CodeBlockTitleRender = (title: string, code: string) => stringDefault:
trueDetails: Whether to enable code block title rendering. Add
title="Title"after the code block```to set the title.Pass a
CodeBlockTitleRenderfunction to customize title rendering.Example:
console.log('hello')Demo```ts title="foo/baz.js" console.log('hello') ```
notationDiff
Type:
booleanDefault:
falseDetails: Whether to enable notation diff.
Example:
console.log('hewwo') console.log('hello') console.log('goodbye')```ts console.log('hewwo') // [!code --] console.log('hello') // [!code ++] console.log('goodbye') ```Also see:
notationFocus
Type:
booleanDefault:
falseDetails: Whether to enable notation focus.
Example:
console.log('Not focused') console.log('Focused') console.log('Not focused')```ts console.log('Not focused') console.log('Focused') // [!code focus] console.log('Not focused') ```Also see:
notationHighlight
Type:
booleanDefault:
falseDetails: Whether to enable notation highlight.
Example:
console.log('Not highlighted') console.log('Highlighted') console.log('Not highlighted')```ts console.log('Not highlighted') console.log('Highlighted') // [!code highlight] console.log('Not highlighted') ```Also see:
notationErrorLevel
Type:
booleanDefault:
falseDetails: Whether to enable notation error level.
Example:
console.log('No errors or warnings') console.warn('Warning') console.error('Error')```ts console.log('No errors or warnings') console.warn('Warning') // [!code warning] console.error('Error') // [!code error] ```Also see:
notationWordHighlight
Type:
booleanDefault:
falseDetails: Whether to enable notation word highlight.
Word highlights must be written on separate lines.
Example:
Highlight words with comments
const message = 'Hello World' console.log(message) // prints Hello World```ts // [!code word:Hello] const message = 'Hello World' console.log(message) // prints Hello World ```Highlight words based on the meta string provided in the code snippet
const msg = 'Hello World' console.log(msg) // prints Hello WorldDemo```js /Hello/ const msg = 'Hello World' console.log(msg) // prints Hello World ```Also see:
whitespace
Type:
boolean | 'all' | 'boundary' | 'trailing'Default:
falseDetails: Whether to enable whitespace characters (spaces and tabs).
true: enable whitespace rendering but don't render any whitespace by defaultfalse: completely disable whitespace rendering;:whitespacewill not take effect'all': render all whitespace characters'boundary': render leading and trailing whitespace on each line'trailing': render trailing whitespace on each line
You can add
:whitespace/:no-whitespacemarkers to your fenced code blocks to override the config setting, and customize the render type by adding=after:whitespace. For example,:whitespace=boundarywill render leading and trailing whitespace on each line.Example:
<!-- render all whitespace --> A text with trailing spaces indented text<!-- render leading and trailing whitespace on each line --> A text with trailing spaces indented text<!-- render trailing whitespace on each line --> A text with trailing spaces indented text<!-- disable whitespace rendering --> A text with trailing spaces indented textDemo```md :whitespace <!-- render all whitespace --> A text with trailing spaces indented text ``` ```md :whitespace=boundary <!-- render leading and trailing whitespace on each line --> A text with trailing spaces indented text ``` ```md :whitespace=trailing <!-- render trailing whitespace on each line --> A text with trailing spaces indented text ``` ```md :no-whitespace <!-- disable whitespace rendering --> A text with trailing spaces indented text ```Also see:
twoslash
Type:
boolean | ShikiTwoslashOptionsinterface ShikiTwoslashOptions extends TransformerTwoslashOptions { /** * Requires adding `twoslash` to the code block explicitly to run twoslash * @default true */ explicitTrigger?: RegExp | boolean /** * twoslash options */ twoslashOptions?: TransformerTwoslashOptions['twoslashOptions'] & VueSpecificOptions /** * The options for caching resolved types * @default true */ typesCache?: TwoslashTypesCache | boolean }Default:
falseDetails: Whether to enable twoslash.
Tips
For size optimization, the plugin doesn't include the
@vuepress/shiki-twoslashpackage by default. You need to install it manually to use this feature.Also see:
Example:
consta= 1 constb= 23console.log(a+b)Demo```ts twoslash const a = 1 const b = 23 console.log(a + b) ```Warning
For code blocks with
twoslashenabled:Don't add the
:v-premarker to code blocks, as this will preventtwoslashfrom running properly.To avoid layout conflicts, line numbers will not be displayed for these code blocks.
Advanced Options
defaultLang
- Type:
string - Default:
'plain' - Details: Fallback language to use when the specified language is not available.
logLevel
- Type:
'warn' | 'debug' | 'silent' - Default:
'warn' - Details: Log level for Shiki language detection.
warn: warn about each unknown language once (default)debug: log every unknown code block with its file path (default when--debugflag is set)silent: no warnings
preWrapper
Type:
booleanDefault:
trueDetails: Whether to add an extra wrapper outside the
<pre>tag.This wrapper is required for
lineNumbersandcollapsedLinesfeatures. If you disablepreWrapper, line numbers and collapsed lines will also be disabled.
shikiSetup
- Type:
(shiki: Highlighter) => void | Promise<void> - Details: A hook function to customize the Shiki highlighter instance.
transformers
Type:
ShikiTransformer[]Details: Shiki transformers.
This option will be passed to the
codeToHtml()method of Shiki.Also see:
