English
highlighter-helper
Shared utilities for the syntax highlighter plugins.
A highlighted code block has two parts: the code produced by the highlighter, and the decorations around it. This package owns the decorations, i.e. the line numbers, the collapsed lines, the title bar and the whitespace markers, together with their styles. Every highlighter therefore renders the same code block, and a new highlighter plugin does not have to reimplement the decorations.
It is used by @vuepress/plugin-shiki, @vuepress/plugin-prismjs and @vuepress/plugin-markdown-file-tree.
Usage
npm i @vuepress/highlighter-helper@nextThe highlighter plugins of this repository depend on the package, so it is installed together with them. Add it explicitly only when you write your own highlighter plugin.
The decorators rewrite the fence rule of markdown-it and decorate the HTML that the rule produces, so they must be registered after the fence rule of the highlighter:
import {
codeBlockTitle,
collapsedLines,
lineNumbers,
} from '@vuepress/highlighter-helper'
import type { Plugin } from 'vuepress/core'
export const myHighlighterPlugin = (): Plugin => ({
name: 'my-highlighter-plugin',
extendsMarkdown: (md) => {
// the fence rule of the highlighter
md.renderer.rules.fence = myFenceRenderer
md.use(lineNumbers, { lineNumbers: true })
md.use(collapsedLines, { collapsedLines: 15 })
md.use(codeBlockTitle, { codeBlockTitle: true })
},
})The client side needs the styles of the enabled features, so import them from the client config:
import '@vuepress/highlighter-helper/styles/base.css'
import '@vuepress/highlighter-helper/styles/line-numbers.css'
import '@vuepress/highlighter-helper/styles/collapsed-lines.css'
import '@vuepress/highlighter-helper/styles/code-block-title.css'Tips
base.css holds the layout variables and the container styles shared by every feature, while the other files only style their own feature.
Guide
Line numbers
The decorator adds a .line-numbers element next to the code and switches the code block to the line-numbers-mode layout, which is styled by styles/line-numbers.css.
lineNumbers: true enables the numbers for every code block, and lineNumbers: <number> enables them only for the code blocks whose line count is not less than the number. A single code block can override the global option with its info string:
:line-numbersenables the numbers.:line-numbers=<number>enables the numbers and starts the numbering at the number, e.g.:line-numbers=10numbers the first line as 10.:no-line-numbersdisables the numbers.
A highlighter that cannot render the numbers for some code blocks, e.g. because another transformer already owns them, can resolve the markers itself through the resolveLineNumbers option:
md.use(lineNumbers, {
lineNumbers: true,
resolveLineNumbers: (info) => (info.includes('twoslash') ? false : undefined),
})Returning undefined falls back to the built-in resolution.
Collapsed lines
The decorator adds a .collapsed-lines element to the code block and switches it to the has-collapsed-lines collapsed layout, with the number of visible lines stored in the --vp-collapsed-lines CSS variable. The styles are in styles/collapsed-lines.css, and the block is expanded by setupCollapsedLines() on the client.
The feature is off by default: collapsedLines: true collapses every code block from the 15th line, and collapsedLines: <number> from the given line. A single code block can override the global option with its info string:
:collapsed-linescollapses the block.:collapsed-lines=<number>collapses the block from the given line.:no-collapsed-lineskeeps the block expanded.
Code block title
The title attribute of the info string is rendered in a title bar above the code:
```ts title="src/index.ts"
export const foo = 'bar'
```The default render escapes the title and wraps the block into .code-block-with-title and .code-block-title-bar elements, styled by styles/code-block-title.css. Passing a function replaces the default render. The function receives the raw, unescaped title, so escape it before inserting it into markup:
export const myHighlighterPlugin = (): Plugin => ({
extendsMarkdown: (md) => {
md.use(codeBlockTitle, {
codeBlockTitle: (title, code) =>
`<div class="my-code-block"><div class="my-code-block-title">${md.utils.escapeHtml(title)}</div>${code}</div>`,
})
},
})Whitespace
The package resolves the whitespace markers of a code block, while the highlighter renders the whitespace itself, e.g. through a transformer. The position is all, boundary, leading or trailing, and resolveWhitespacePosition() returns:
falsewhen the block has the:no-whitespacemarker.- The position of the
:whitespace=<position>marker. - The global option when the block has a bare
:whitespacemarker, falling back toallwhen the option is not a position. - The global option when the block has no marker, which is
falsewhen the option istrue, i.e. enabled without rendering the whitespace by default.
Node API
lineNumbers
lineNumbers
boolean | number | 'disable'Default
trueWhether to show the line numbers for the code blocks. A number enables them only for the blocks with at least that many lines, and 'disable' turns the feature off completely, including the markers.
removeLastLine
booleanDefault
falseWhether to remove the last line of the code before counting the lines.
resolveLineNumbers
(info: string) => boolean | undefinedA custom resolver for a single code block, which receives the info string of the block and returns whether to show its line numbers. Returning undefined falls back to the built-in resolution.
It rewrites md.renderer.rules.fence and returns immediately when lineNumbers is 'disable'.
resolveLineNumbers
It parses the line number markers out of a code block info string and returns true for :line-numbers, the start number for :line-numbers=<number>, false for :no-line-numbers, and null when the info string has no marker.
collapsedLines
collapsedLines
boolean | number | 'disable'Default
'disable'Whether to collapse the code blocks that exceed a certain number of lines. true collapses from the 15th line, a number from that line, and 'disable' turns the feature off completely, including the markers.
removeLastLine
booleanDefault
falseWhether to remove the last line of the code before counting the lines.
It rewrites md.renderer.rules.fence and returns immediately when collapsedLines is 'disable'.
resolveCollapsedLines
It parses the collapsed lines markers out of a code block info string and returns true for :collapsed-lines, the start line for :collapsed-lines=<number>, false for :no-collapsed-lines, and null when the info string has no marker.
codeBlockTitle
codeBlockTitle
CodeBlockTitleRender | booleanDefault
trueWhether to render the title of the code blocks. A function replaces the default render, and receives the raw title and the rendered HTML of the code block as (title: string, code: string) => string.
It rewrites md.renderer.rules.fence and only decorates the blocks whose info string has a title attribute.
resolveAttr
It returns the value of an attribute of a code block info string, e.g. resolveAttr('ts title="src/index.ts"', 'title') returns 'src/index.ts', and null when the attribute is absent. The value can be wrapped in single or double quotes.
resolveWhitespacePosition
infoRequired
stringThe info string of the code block.
globalOptionRequired
WhitespacePosition | trueThe whitespace option of the plugin. true is resolved to 'all'.
It returns the WhitespacePosition to render, i.e. 'all', 'boundary', 'leading' or 'trailing', or false when the block must be rendered without whitespace.
WHITESPACE_REGEXP and NO_WHITESPACE_REGEXP
The regular expressions that match the :whitespace and :no-whitespace markers of an info string, together with their named groups, so that a highlighter can share the same marker syntax.
Client API
setupCollapsedLines
selector
stringDefault
'div[class*="language-"].has-collapsed-lines > .collapsed-lines'The selector of the element that expands a collapsed code block.
It registers a click listener that toggles the collapsed class of the code block and scrolls it into the center of the viewport, so it must be called once in the client config of the plugin.
Styles
Each feature ships a compiled CSS file, resolved as @vuepress/highlighter-helper/styles/<name>.css:
base.css: the layout variables and the container styles shared by every feature.code-block-title.css: the title bar.collapsed-lines.css: the collapsed lines and their fade.line-numbers.css: the line numbers.notation-diff.css,notation-error-level.css,notation-focus.css,notation-highlight.css,notation-word-highlight.css: the marks that the highlighters add to a line or a word, i.e. diff, error level, focus, highlight and word highlight.whitespace.css: the rendered whitespace characters.
@vuepress/highlighter-helper/scss/code-title-icon provides a style() mixin that adds an icon to the title bar of the code blocks matching a title, which the documentation site uses to show the icon of a file type:
@use '@vuepress/highlighter-helper/scss/code-title-icon';
@include code-title-icon.style(
(
title: '.vuepress/config.ts',
match: 'equals',
icon: '/images/vuepress.png',
),
(
title: '.scss',
match: 'endsWith',
icon: '/images/sass.svg',
)
);match accepts equals, startsWith, endsWith and contains; any other value is reported as a warning and ignored.
