prismjs
This plugin enables syntax highlighting for markdown code fences with Prism.js.
This plugin has been integrated into the default theme.
Usage
npm i -D @vuepress/plugin-prismjs@nextimport { prismjsPlugin } from '@vuepress/plugin-prismjs'
export default {
plugins: [
prismjsPlugin({
// options
}),
],
}Options
theme
Type:
PrismjsThemeDefault:
'nord'Details: Prism.js theme that will be applied to code blocks.
themes
Type:
{ light: PrismjsTheme; dark: PrismjsTheme }Details:
Apply light/dark dual themes.
Note: To use this feature, your theme must set the
data-theme="dark"attribute on the<html>tag when dark mode is enabled.
Available Prism.js Light themes
- ateliersulphurpool-light
- coldark-cold
- coy
- duotone-light
- ghcolors
- gruvbox-light
- material-light
- one-light
- vs
Available Prism.js Dark themes
- atom-dark
- cb
- coldark-dark
- dark
- dracula
- duotone-dark
- duotone-earth
- duotone-forest
- duotone-sea
- duotone-space
- gruvbox-dark
- holi
- hopscotch
- lucario
- material-dark
- material-oceanic
- night-owl
- nord
- one-dark
- pojoaque
- shades-of-purple
- solarized-dark-atom
- tomorrow
- vsc-dark-plus
- xonokai
- z-touch
lineNumbers
Type:
boolean | number | 'disable'Default:
trueDetails:
number: The minimum number of lines to enable line numbers.
For example, if you set it to 4, line numbers will only be enabled when your code block has at least 4 lines of code.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 in your fenced code blocks to override the value set in config, and customize the beginning number by adding=after:line-numbers. For example,:line-numbers=2means the line numbers in code blocks will start from2.
// line-numbers is enabled
const line2 = 'This is line 2'
const line3 = 'This is line 3'// line-numbers is disabled
const line2 = 'This is line 2'
const line3 = 'This is line 3'// line-numbers is enabled and starts from 2
const line3 = 'This is line 3'
const line4 = 'This is line 4'```ts:line-numbers
// line-numbers is enabled
const line2 = 'This is line 2'
const line3 = 'This is line 3'
```
```ts :no-line-numbers
// line-numbers is disabled
const line2 = 'This is line 2'
const line3 = 'This is line 3'
```
```ts :line-numbers=2
// line-numbers is enabled and starts from 2
const line3 = 'This is line 3'
const line4 = 'This is line 4'
```highlightLines
Type:
booleanDefault:
trueDetails:
Whether to enable code line highlighting. You can highlight specified lines of your code blocks by adding line range markers in 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-linesmarkers 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
CodeBlockTitleRenderto customize the title rendering.Example:
console.log('hello')Demo```ts title="foo/baz.js" console.log('hello') ```
Tips
In the new version, some functionalities similar to shiki have been implemented, allowing you to style code blocks using the same syntax.
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 highlight must be written on a separate line.
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 on 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 (Space and Tab).
true: Enable whitespace, but not render any whitespace by default.false: Completely disable whitespace rendering;:whitespacewill not take effect.'all': Render all whitespace.'boundary': Render leading and trailing whitespace of the line.'trailing': Render trailing whitespace of the line.
You can add
:whitespace/:no-whitespacemarkers in your fenced code blocks to override the value set in config, and customize the render type by adding=after:whitespace. For example,:whitespace=boundarywill render leading and trailing whitespace of the line.Example:
<!-- render all whitespace --> A text with trailing spaces indented text<!-- render leading and trailing whitespace of the line --> A text with trailing spaces indented text<!-- render trailing whitespace of the line --> A text with trailing spaces indented text<!-- disable render whitespace --> 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 of the line --> A text with trailing spaces indented text ``` ```md :whitespace=trailing <!-- render trailing whitespace of the line --> A text with trailing spaces indented text ``` ```md :no-whitespace <!-- disable render whitespace --> A text with trailing spaces indented text ```Also see:
preloadLanguages
Type:
string[]Default:
['markdown', 'jsdoc', 'yaml']Details:
Languages to preload.
By default, languages will be loaded on demand when parsing markdown files.
However, Prism.js has some potential issues about loading languages dynamically. To avoid them, you can preload languages via this option.
preWrapper
Type:
booleanDefault:
trueDetails:
Whether to add an extra wrapper outside the
<pre>tag.The wrapper is required by
lineNumbersandcollapsedLines. This means if you disablepreWrapper, the line numbers and collapsed lines will also be disabled.Tips
You can disable it if you want to implement them on the client side. For example, Prismjs Line Highlight or Prismjs Line Numbers.
