--- icon: hammer --- # 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 ```bash npm i @vuepress/highlighter-helper@next ``` The 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: ```ts title="src/node/myHighlighterPlugin.ts" 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: ```ts title="src/client/config.ts" 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' ``` ::: tip `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: ` 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-numbers` enables the numbers. - `:line-numbers=` enables the numbers and starts the numbering at the number, e.g. `:line-numbers=10` numbers the first line as 10. - `:no-line-numbers` disables 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: ```ts 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: ` from the given line. A single code block can override the global option with its info string: - `:collapsed-lines` collapses the block. - `:collapsed-lines=` collapses the block from the given line. - `:no-collapsed-lines` keeps the block expanded. ### Code block title The `title` attribute of the info string is rendered in a title bar above the code: ````md ```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: ```ts title="src/node/myHighlighterPlugin.ts" export const myHighlighterPlugin = (): Plugin => ({ extendsMarkdown: (md) => { md.use(codeBlockTitle, { codeBlockTitle: (title, code) => `
${md.utils.escapeHtml(title)}
${code}
`, }) }, }) ``` ### 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: - `false` when the block has the `:no-whitespace` marker. - The position of the `:whitespace=` marker. - The global option when the block has a bare `:whitespace` marker, falling back to `all` when the option is not a position. - The global option when the block has no marker, which is `false` when the option is `true`, i.e. enabled without rendering the whitespace by default. ## Node API ### lineNumbers ::: fields @`lineNumbers` type=`boolean | number | 'disable'` default=`true` Whether 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` type=boolean default=`false` Whether to remove the last line of the code before counting the lines. @`resolveLineNumbers` type=`(info: string) => boolean | undefined` A 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=`, `false` for `:no-line-numbers`, and `null` when the info string has no marker. ### collapsedLines ::: fields @`collapsedLines` type=`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` type=boolean default=`false` Whether 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=`, `false` for `:no-collapsed-lines`, and `null` when the info string has no marker. ### codeBlockTitle ::: fields @`codeBlockTitle` type=`CodeBlockTitleRender | boolean` default=`true` Whether 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 ::: fields @`info` type=string required The info string of the code block. @`globalOption` type=`WhitespacePosition | true` required The 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 ::: fields @`selector` type=string default=`'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/.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: ```scss title=".vuepress/styles/index.scss" @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.