Color mode

Code Annotations

VitePress/Docusaurus-style code block annotations: line highlighting, focus, and diff markers that work on plain <pre><code> blocks and on the line wrappers produced by @riebeckite/plugin-code-enhance.

日本語

Installation

ts
import { defineConfig } from "@riebeckite/core";
import { codeAnnotations } from "@riebeckite/plugin-code-annotations";
 
export default defineConfig({
  // ...
  plugins: [codeAnnotations()],
});

The plugin registers its own style.css. It adds no client entry and no runtime JavaScript.

Syntax

Line highlighting (fence meta)

Add a brace range after the language, exactly like Docusaurus and VitePress.

md
```js {2,4-5}
const a = 1;
const b = 2;
const c = 3;
const d = 4;
const e = 5;
```

Lines 2, 4, and 5 receive rb-code__line--highlighted.

Focus

Use the focus meta group, or an inline [!code focus] marker. An optional count focuses the current line and the following lines.

md
```js focus:{2}
const a = 1;
const b = 2;
```
md
```js
const a = 1; // [!code focus]
const b = 2;
```

.rb-code__line--focused is applied. [!code focus:3] focuses three lines starting at the marker.

Diff

md
```js
const kept = true;
const added = true;    // [!code ++]
const removed = false; // [!code --]
```

The marker comment is removed from the rendered text, and the line receives rb-code__line--added or rb-code__line--removed.

Explicit highlight marker

md
```js
const value = 1; // [!code highlight]
```

Marker comments are recognized with the //, #, --, and <!-- --> comment prefixes, so the same syntax works for JavaScript, shell, SQL, Lua, HTML, and other languages.

Options

Option Type Default Description
className string "rb-code" Class on the block root (<pre> or rehype-pretty-code <figure>)
lineClassName string "rb-code__line" Class on generated line wrappers
highlightClassName string "rb-code__line--highlighted" Class for highlighted lines
addedClassName string "rb-code__line--added" Class for [!code ++] lines
removedClassName string "rb-code__line--removed" Class for [!code --] lines
focusClassName string "rb-code__line--focused" Class for focused lines
language string unset Only annotate blocks of this language
ts
codeAnnotations({ highlightClassName: "is-highlighted" });

Using with code-enhance

@riebeckite/plugin-code-annotations does not import or depend on @riebeckite/plugin-code-enhance. It detects both raw <pre><code> text and the .line wrappers emitted by rehype-pretty-code:

  • If line wrappers already exist, their classes are extended in place and the existing data-line attribute is kept.
  • Otherwise the plugin wraps the raw code text into <span class="rb-code__line" data-line="N"> elements.

Because rehype-pretty-code substitutes the <code> element, the plan is also mirrored into the preserved fence meta, so annotations still apply after code-enhance runs. Place codeAnnotations() after codeEnhance() in the plugins array; the plugin uses order: 10 to run after code-enhance's highlighting regardless.

Exports

  • codeAnnotations(options?) / codeAnnotationsPlugin(options?) — plugin factory
  • remarkCodeAnnotations(options?) — remark transform
  • rehypeCodeAnnotations(options?) — rehype transform
  • parseCodeAnnotations(meta) — parse fence meta into a plan
  • parseLineRanges(spec) — parse 1,3-5 into line numbers
  • collectCodeAnnotations(meta, code) — parse meta plus inline markers
  • resolveCodeAnnotationsOptions(options?) — fill in defaults
  • Types: CodeAnnotationsOptions, ResolvedCodeAnnotationsOptions, CodeAnnotationPlan, CodeAnnotationKind

See also

History

1 changesCollapseExpand
1 + <!-- Generated from packages/plugins/code-annotations/README.md. Do not edit this page directly; edit the package README and run `pnpm docs:sync`. -->
2 +
3 + # Code Annotations
4 +
5 + VitePress/Docusaurus-style code block annotations: line highlighting, focus,
6 + and diff markers that work on plain `<pre><code>` blocks and on the line
7 + wrappers produced by `@riebeckite/plugin-code-enhance`.
8 +
9 + [日本語](./code-annotations.md)
10 +
11 + ## Installation
12 +
13 + ```ts
14 + import { defineConfig } from "@riebeckite/core";
15 + import { codeAnnotations } from "@riebeckite/plugin-code-annotations";
16 +
17 + export default defineConfig({
18 + // ...
19 + plugins: [codeAnnotations()],
20 + });
21 + ```
22 +
23 + The plugin registers its own `style.css`. It adds no client entry and no
24 + runtime JavaScript.
25 +
26 + ## Syntax
27 +
28 + ### Line highlighting (fence meta)
29 +
30 + Add a brace range after the language, exactly like Docusaurus and VitePress.
31 +
32 + ````md
33 + ```js {2,4-5}
34 + const a = 1;
35 + const b = 2;
36 + const c = 3;
37 + const d = 4;
38 + const e = 5;
39 + ```
40 + ````
41 +
42 + Lines `2`, `4`, and `5` receive `rb-code__line--highlighted`.
43 +
44 + ### Focus
45 +
46 + Use the `focus` meta group, or an inline `[!code focus]` marker. An optional
47 + count focuses the current line and the following lines.
48 +
49 + ````md
50 + ```js focus:{2}
51 + const a = 1;
52 + const b = 2;
53 + ```
54 + ````
55 +
56 + ````md
57 + ```js
58 + const a = 1; // [!code focus]
59 + const b = 2;
60 + ```
61 + ````
62 +
63 + `.rb-code__line--focused` is applied. `[!code focus:3]` focuses three lines
64 + starting at the marker.
65 +
66 + ### Diff
67 +
68 + ````md
69 + ```js
70 + const kept = true;
71 + const added = true; // [!code ++]
72 + const removed = false; // [!code --]
73 + ```
74 + ````
75 +
76 + The marker comment is removed from the rendered text, and the line receives
77 + `rb-code__line--added` or `rb-code__line--removed`.
78 +
79 + ### Explicit highlight marker
80 +
81 + ````md
82 + ```js
83 + const value = 1; // [!code highlight]
84 + ```
85 + ````
86 +
87 + Marker comments are recognized with the `//`, `#`, `--`, and `<!-- -->`
88 + comment prefixes, so the same syntax works for JavaScript, shell, SQL, Lua,
89 + HTML, and other languages.
90 +
91 + ## Options
92 +
93 + | Option | Type | Default | Description |
94 + | ------ | ---- | ------- | ----------- |
95 + | `className` | `string` | `"rb-code"` | Class on the block root (`<pre>` or `rehype-pretty-code` `<figure>`) |
96 + | `lineClassName` | `string` | `"rb-code__line"` | Class on generated line wrappers |
97 + | `highlightClassName` | `string` | `"rb-code__line--highlighted"` | Class for highlighted lines |
98 + | `addedClassName` | `string` | `"rb-code__line--added"` | Class for `[!code ++]` lines |
99 + | `removedClassName` | `string` | `"rb-code__line--removed"` | Class for `[!code --]` lines |
100 + | `focusClassName` | `string` | `"rb-code__line--focused"` | Class for focused lines |
101 + | `language` | `string` | unset | Only annotate blocks of this language |
102 +
103 + ```ts
104 + codeAnnotations({ highlightClassName: "is-highlighted" });
105 + ```
106 +
107 + ## Using with code-enhance
108 +
109 + `@riebeckite/plugin-code-annotations` does not import or depend on
110 + `@riebeckite/plugin-code-enhance`. It detects both raw `<pre><code>` text and
111 + the `.line` wrappers emitted by `rehype-pretty-code`:
112 +
113 + - If line wrappers already exist, their classes are extended in place and the
114 + existing `data-line` attribute is kept.
115 + - Otherwise the plugin wraps the raw code text into
116 + `<span class="rb-code__line" data-line="N">` elements.
117 +
118 + Because `rehype-pretty-code` substitutes the `<code>` element, the plan is also
119 + mirrored into the preserved fence meta, so annotations still apply after
120 + code-enhance runs. Place `codeAnnotations()` after `codeEnhance()` in the
121 + plugins array; the plugin uses `order: 10` to run after code-enhance's
122 + highlighting regardless.
123 +
124 + ## Exports
125 +
126 + - `codeAnnotations(options?)` / `codeAnnotationsPlugin(options?)` — plugin factory
127 + - `remarkCodeAnnotations(options?)` — remark transform
128 + - `rehypeCodeAnnotations(options?)` — rehype transform
129 + - `parseCodeAnnotations(meta)` — parse fence meta into a plan
130 + - `parseLineRanges(spec)` — parse `1,3-5` into line numbers
131 + - `collectCodeAnnotations(meta, code)` — parse meta plus inline markers
132 + - `resolveCodeAnnotationsOptions(options?)` — fill in defaults
133 + - Types: `CodeAnnotationsOptions`, `ResolvedCodeAnnotationsOptions`,
134 + `CodeAnnotationPlan`, `CodeAnnotationKind`
135 +
136 + ## See also
137 +
138 + - [Plugin guide](../reference/plugin-api.en.md)
139 + - [`@riebeckite/plugin-code-enhance`](./code-enhance.en.md)
140 +