@riebeckite/plugin-code-annotations 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +137 -0
- package/README_ja.md +136 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +449 -0
- package/dist/src/annotations.d.ts +48 -0
- package/dist/src/hast.d.ts +12 -0
- package/dist/src/options.d.ts +10 -0
- package/dist/src/rehype.d.ts +8 -0
- package/dist/src/remark.d.ts +16 -0
- package/dist/src/types.d.ts +56 -0
- package/package.json +55 -0
- package/style.css +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright Rerurate_514
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# @riebeckite/plugin-code-annotations
|
|
2
|
+
|
|
3
|
+
VitePress/Docusaurus-style code block annotations: line highlighting, focus,
|
|
4
|
+
and diff markers that work on plain `<pre><code>` blocks and on the line
|
|
5
|
+
wrappers produced by `@riebeckite/plugin-code-enhance`.
|
|
6
|
+
|
|
7
|
+
[日本語](./README_ja.md)
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { defineConfig } from "@riebeckite/core";
|
|
13
|
+
import { codeAnnotations } from "@riebeckite/plugin-code-annotations";
|
|
14
|
+
|
|
15
|
+
export default defineConfig({
|
|
16
|
+
// ...
|
|
17
|
+
plugins: [codeAnnotations()],
|
|
18
|
+
});
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The plugin registers its own `style.css`. It adds no client entry and no
|
|
22
|
+
runtime JavaScript.
|
|
23
|
+
|
|
24
|
+
## Syntax
|
|
25
|
+
|
|
26
|
+
### Line highlighting (fence meta)
|
|
27
|
+
|
|
28
|
+
Add a brace range after the language, exactly like Docusaurus and VitePress.
|
|
29
|
+
|
|
30
|
+
````md
|
|
31
|
+
```js {2,4-5}
|
|
32
|
+
const a = 1;
|
|
33
|
+
const b = 2;
|
|
34
|
+
const c = 3;
|
|
35
|
+
const d = 4;
|
|
36
|
+
const e = 5;
|
|
37
|
+
```
|
|
38
|
+
````
|
|
39
|
+
|
|
40
|
+
Lines `2`, `4`, and `5` receive `rb-code__line--highlighted`.
|
|
41
|
+
|
|
42
|
+
### Focus
|
|
43
|
+
|
|
44
|
+
Use the `focus` meta group, or an inline `[!code focus]` marker. An optional
|
|
45
|
+
count focuses the current line and the following lines.
|
|
46
|
+
|
|
47
|
+
````md
|
|
48
|
+
```js focus:{2}
|
|
49
|
+
const a = 1;
|
|
50
|
+
const b = 2;
|
|
51
|
+
```
|
|
52
|
+
````
|
|
53
|
+
|
|
54
|
+
````md
|
|
55
|
+
```js
|
|
56
|
+
const a = 1; // [!code focus]
|
|
57
|
+
const b = 2;
|
|
58
|
+
```
|
|
59
|
+
````
|
|
60
|
+
|
|
61
|
+
`.rb-code__line--focused` is applied. `[!code focus:3]` focuses three lines
|
|
62
|
+
starting at the marker.
|
|
63
|
+
|
|
64
|
+
### Diff
|
|
65
|
+
|
|
66
|
+
````md
|
|
67
|
+
```js
|
|
68
|
+
const kept = true;
|
|
69
|
+
const added = true; // [!code ++]
|
|
70
|
+
const removed = false; // [!code --]
|
|
71
|
+
```
|
|
72
|
+
````
|
|
73
|
+
|
|
74
|
+
The marker comment is removed from the rendered text, and the line receives
|
|
75
|
+
`rb-code__line--added` or `rb-code__line--removed`.
|
|
76
|
+
|
|
77
|
+
### Explicit highlight marker
|
|
78
|
+
|
|
79
|
+
````md
|
|
80
|
+
```js
|
|
81
|
+
const value = 1; // [!code highlight]
|
|
82
|
+
```
|
|
83
|
+
````
|
|
84
|
+
|
|
85
|
+
Marker comments are recognized with the `//`, `#`, `--`, and `<!-- -->`
|
|
86
|
+
comment prefixes, so the same syntax works for JavaScript, shell, SQL, Lua,
|
|
87
|
+
HTML, and other languages.
|
|
88
|
+
|
|
89
|
+
## Options
|
|
90
|
+
|
|
91
|
+
| Option | Type | Default | Description |
|
|
92
|
+
| ------ | ---- | ------- | ----------- |
|
|
93
|
+
| `className` | `string` | `"rb-code"` | Class on the block root (`<pre>` or `rehype-pretty-code` `<figure>`) |
|
|
94
|
+
| `lineClassName` | `string` | `"rb-code__line"` | Class on generated line wrappers |
|
|
95
|
+
| `highlightClassName` | `string` | `"rb-code__line--highlighted"` | Class for highlighted lines |
|
|
96
|
+
| `addedClassName` | `string` | `"rb-code__line--added"` | Class for `[!code ++]` lines |
|
|
97
|
+
| `removedClassName` | `string` | `"rb-code__line--removed"` | Class for `[!code --]` lines |
|
|
98
|
+
| `focusClassName` | `string` | `"rb-code__line--focused"` | Class for focused lines |
|
|
99
|
+
| `language` | `string` | unset | Only annotate blocks of this language |
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
codeAnnotations({ highlightClassName: "is-highlighted" });
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Using with code-enhance
|
|
106
|
+
|
|
107
|
+
`@riebeckite/plugin-code-annotations` does not import or depend on
|
|
108
|
+
`@riebeckite/plugin-code-enhance`. It detects both raw `<pre><code>` text and
|
|
109
|
+
the `.line` wrappers emitted by `rehype-pretty-code`:
|
|
110
|
+
|
|
111
|
+
- If line wrappers already exist, their classes are extended in place and the
|
|
112
|
+
existing `data-line` attribute is kept.
|
|
113
|
+
- Otherwise the plugin wraps the raw code text into
|
|
114
|
+
`<span class="rb-code__line" data-line="N">` elements.
|
|
115
|
+
|
|
116
|
+
Because `rehype-pretty-code` substitutes the `<code>` element, the plan is also
|
|
117
|
+
mirrored into the preserved fence meta, so annotations still apply after
|
|
118
|
+
code-enhance runs. Place `codeAnnotations()` after `codeEnhance()` in the
|
|
119
|
+
plugins array; the plugin uses `order: 10` to run after code-enhance's
|
|
120
|
+
highlighting regardless.
|
|
121
|
+
|
|
122
|
+
## Exports
|
|
123
|
+
|
|
124
|
+
- `codeAnnotations(options?)` / `codeAnnotationsPlugin(options?)` — plugin factory
|
|
125
|
+
- `remarkCodeAnnotations(options?)` — remark transform
|
|
126
|
+
- `rehypeCodeAnnotations(options?)` — rehype transform
|
|
127
|
+
- `parseCodeAnnotations(meta)` — parse fence meta into a plan
|
|
128
|
+
- `parseLineRanges(spec)` — parse `1,3-5` into line numbers
|
|
129
|
+
- `collectCodeAnnotations(meta, code)` — parse meta plus inline markers
|
|
130
|
+
- `resolveCodeAnnotationsOptions(options?)` — fill in defaults
|
|
131
|
+
- Types: `CodeAnnotationsOptions`, `ResolvedCodeAnnotationsOptions`,
|
|
132
|
+
`CodeAnnotationPlan`, `CodeAnnotationKind`
|
|
133
|
+
|
|
134
|
+
## See also
|
|
135
|
+
|
|
136
|
+
- [Plugin guide](../../docs/plugins_en.md)
|
|
137
|
+
- [`@riebeckite/plugin-code-enhance`](../code-enhance/README.md)
|
package/README_ja.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# @riebeckite/plugin-code-annotations
|
|
2
|
+
|
|
3
|
+
VitePress / Docusaurus 風のコードブロック装飾を加えるプラグインです。フェンスの
|
|
4
|
+
メタ情報による行の強調と、`[!code ...]` のインラインマーカーによるフォーカス・
|
|
5
|
+
差分表示に対応します。素の `<pre><code>` と
|
|
6
|
+
`@riebeckite/plugin-code-enhance` の行ラッパーの両方で動作します。
|
|
7
|
+
|
|
8
|
+
[English](./README.md)
|
|
9
|
+
|
|
10
|
+
## 設定
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { defineConfig } from "@riebeckite/core";
|
|
14
|
+
import { codeAnnotations } from "@riebeckite/plugin-code-annotations";
|
|
15
|
+
|
|
16
|
+
export default defineConfig({
|
|
17
|
+
// ...
|
|
18
|
+
plugins: [codeAnnotations()],
|
|
19
|
+
});
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
プラグインは `style.css` のみを登録します。クライアントエントリーはなく、
|
|
23
|
+
実行時 JavaScript は追加しません。
|
|
24
|
+
|
|
25
|
+
## 書き方
|
|
26
|
+
|
|
27
|
+
### フェンスメタによる行の強調
|
|
28
|
+
|
|
29
|
+
言語の後ろに波括弧で行範囲を書きます。Docusaurus や VitePress と同じ記法です。
|
|
30
|
+
|
|
31
|
+
````md
|
|
32
|
+
```js {2,4-5}
|
|
33
|
+
const a = 1;
|
|
34
|
+
const b = 2;
|
|
35
|
+
const c = 3;
|
|
36
|
+
const d = 4;
|
|
37
|
+
const e = 5;
|
|
38
|
+
```
|
|
39
|
+
````
|
|
40
|
+
|
|
41
|
+
2、4、5 行目に `rb-code__line--highlighted` が付きます。
|
|
42
|
+
|
|
43
|
+
### フォーカス
|
|
44
|
+
|
|
45
|
+
`focus` メタ、またはインラインの `[!code focus]` を使います。行数を指定すると
|
|
46
|
+
その行から続く行までをまとめてフォーカスします。
|
|
47
|
+
|
|
48
|
+
````md
|
|
49
|
+
```js focus:{2}
|
|
50
|
+
const a = 1;
|
|
51
|
+
const b = 2;
|
|
52
|
+
```
|
|
53
|
+
````
|
|
54
|
+
|
|
55
|
+
````md
|
|
56
|
+
```js
|
|
57
|
+
const a = 1; // [!code focus]
|
|
58
|
+
const b = 2;
|
|
59
|
+
```
|
|
60
|
+
````
|
|
61
|
+
|
|
62
|
+
`rb-code__line--focused` が付きます。`[!code focus:3]` なら 3 行分です。
|
|
63
|
+
|
|
64
|
+
### 差分
|
|
65
|
+
|
|
66
|
+
````md
|
|
67
|
+
```js
|
|
68
|
+
const kept = true;
|
|
69
|
+
const added = true; // [!code ++]
|
|
70
|
+
const removed = false; // [!code --]
|
|
71
|
+
```
|
|
72
|
+
````
|
|
73
|
+
|
|
74
|
+
マーカーのコメントは表示テキストから取り除かれ、行には
|
|
75
|
+
`rb-code__line--added` または `rb-code__line--removed` が付きます。
|
|
76
|
+
|
|
77
|
+
### 明示的な強調マーカー
|
|
78
|
+
|
|
79
|
+
````md
|
|
80
|
+
```js
|
|
81
|
+
const value = 1; // [!code highlight]
|
|
82
|
+
```
|
|
83
|
+
````
|
|
84
|
+
|
|
85
|
+
マーカーは `//`、`#`、`--`、`<!-- -->` のコメント記法に対応します。JavaScript、
|
|
86
|
+
シェル、SQL、Lua、HTML などで同じ書き方が使えます。
|
|
87
|
+
|
|
88
|
+
## オプション
|
|
89
|
+
|
|
90
|
+
| オプション | 既定値 | 内容 |
|
|
91
|
+
| --- | --- | --- |
|
|
92
|
+
| `className` | `"rb-code"` | ブロック直下(`<pre>` または `rehype-pretty-code` の `<figure>`)に付くクラス |
|
|
93
|
+
| `lineClassName` | `"rb-code__line"` | 生成する行ラッパーのクラス |
|
|
94
|
+
| `highlightClassName` | `"rb-code__line--highlighted"` | 強調行のクラス |
|
|
95
|
+
| `addedClassName` | `"rb-code__line--added"` | `[!code ++]` 行のクラス |
|
|
96
|
+
| `removedClassName` | `"rb-code__line--removed"` | `[!code --]` 行のクラス |
|
|
97
|
+
| `focusClassName` | `"rb-code__line--focused"` | フォーカス行のクラス |
|
|
98
|
+
| `language` | 未設定 | 指定した言語のブロックだけを対象にする |
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
codeAnnotations({ highlightClassName: "is-highlighted" });
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## code-enhance との併用
|
|
105
|
+
|
|
106
|
+
`@riebeckite/plugin-code-annotations` は
|
|
107
|
+
`@riebeckite/plugin-code-enhance` を import せず、依存もしません。素の
|
|
108
|
+
`<pre><code>` のテキストと、`rehype-pretty-code` が出力する `.line` ラッパーの
|
|
109
|
+
両方を検出します。
|
|
110
|
+
|
|
111
|
+
- 行ラッパーがすでにある場合は、既存のクラスに追記し、既存の `data-line` を
|
|
112
|
+
そのまま使います。
|
|
113
|
+
- ない場合は、コード本文を
|
|
114
|
+
`<span class="rb-code__line" data-line="N">` に分割します。
|
|
115
|
+
|
|
116
|
+
`rehype-pretty-code` は `<code>` 要素を差し替えるため、注釈プランは保持される
|
|
117
|
+
フェンスメタにも複製します。これにより code-enhance の後でも注釈が適用されます。
|
|
118
|
+
プラグイン配列では `codeEnhance()` の後に `codeAnnotations()` を置いてください。
|
|
119
|
+
`order: 10` により、順序に関わらず強調処理の後に実行されます。
|
|
120
|
+
|
|
121
|
+
## 公開 API
|
|
122
|
+
|
|
123
|
+
- `codeAnnotations(options?)` / `codeAnnotationsPlugin(options?)` — プラグインファクトリ
|
|
124
|
+
- `remarkCodeAnnotations(options?)` — remark 変換
|
|
125
|
+
- `rehypeCodeAnnotations(options?)` — rehype 変換
|
|
126
|
+
- `parseCodeAnnotations(meta)` — フェンスメタをプランに変換
|
|
127
|
+
- `parseLineRanges(spec)` — `1,3-5` を行番号に変換
|
|
128
|
+
- `collectCodeAnnotations(meta, code)` — メタとインラインマーカーをまとめて解析
|
|
129
|
+
- `resolveCodeAnnotationsOptions(options?)` — 既定値を補完
|
|
130
|
+
- 型: `CodeAnnotationsOptions`、`ResolvedCodeAnnotationsOptions`、
|
|
131
|
+
`CodeAnnotationPlan`、`CodeAnnotationKind`
|
|
132
|
+
|
|
133
|
+
## 関連資料
|
|
134
|
+
|
|
135
|
+
- [プラグインシステム](../../../docs/ja/plugin-system.md)
|
|
136
|
+
- [`@riebeckite/plugin-code-enhance`](../code-enhance/README_ja.md)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { CodeAnnotationsOptions } from "./src/types.js";
|
|
2
|
+
export { ANNOTATIONS_META_PREFIX, collectCodeAnnotations, createEmptyPlan, deserializeAnnotations, encodeAnnotationsMeta, extractAnnotationsMeta, hasAnnotations, parseCodeAnnotations, parseLineRanges, serializeAnnotations, stripInlineCodeAnnotation, } from "./src/annotations.js";
|
|
3
|
+
export type { CollectedCodeAnnotations, InlineCodeAnnotation, } from "./src/annotations.js";
|
|
4
|
+
export { rehypeCodeAnnotations } from "./src/rehype.js";
|
|
5
|
+
export { remarkCodeAnnotations } from "./src/remark.js";
|
|
6
|
+
export { DEFAULT_CODE_ANNOTATIONS_OPTIONS, resolveCodeAnnotationsOptions, } from "./src/options.js";
|
|
7
|
+
export type { CodeAnnotationKind, CodeAnnotationPlan, CodeAnnotationsOptions, ResolvedCodeAnnotationsOptions, } from "./src/types.js";
|
|
8
|
+
/**
|
|
9
|
+
* VitePress/Docusaurus-style code block annotations: line highlighting from
|
|
10
|
+
* fence meta and inline `[!code ...]` marker comments for focus, highlight,
|
|
11
|
+
* and diff lines.
|
|
12
|
+
*
|
|
13
|
+
* Runs after `@riebeckite/plugin-code-enhance` (via `order`) so it can annotate
|
|
14
|
+
* the line wrappers produced by `rehype-pretty-code` instead of re-wrapping
|
|
15
|
+
* them. It never imports or depends on code-enhance.
|
|
16
|
+
*/
|
|
17
|
+
export declare function codeAnnotations(options?: CodeAnnotationsOptions): import("@riebeckite/core").RiebeckitePlugin<import("./index.js").ResolvedCodeAnnotationsOptions>;
|
|
18
|
+
export declare const codeAnnotationsPlugin: typeof codeAnnotations;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
import { createStyleAsset, definePlugin } from "@riebeckite/core";
|
|
3
|
+
|
|
4
|
+
// src/annotations.ts
|
|
5
|
+
var ANNOTATIONS_META_PREFIX = "rb-annotations=";
|
|
6
|
+
var ANNOTATION_KEYS = [
|
|
7
|
+
"highlight",
|
|
8
|
+
"added",
|
|
9
|
+
"removed",
|
|
10
|
+
"focus"
|
|
11
|
+
];
|
|
12
|
+
var FOCUS_META_PATTERN = /(?:^|\s)focus[:=]\{([^}]*)\}/g;
|
|
13
|
+
var HIGHLIGHT_META_PATTERN = /\{([^{}]*)\}/g;
|
|
14
|
+
var INLINE_ANNOTATION_PATTERN = /([ \t]*)(?:(?:\/\/|#|--)[ \t]*\[!code[ \t]+(focus|highlight|\+\+|--)(?::(\d+))?\]|<!--[ \t]*\[!code[ \t]+(focus|highlight|\+\+|--)(?::(\d+))?[ \t]*-->)[ \t]*$/;
|
|
15
|
+
function createEmptyPlan() {
|
|
16
|
+
return { highlight: [], added: [], removed: [], focus: [] };
|
|
17
|
+
}
|
|
18
|
+
function parseCodeAnnotations(meta) {
|
|
19
|
+
const plan = createEmptyPlan();
|
|
20
|
+
if (typeof meta !== "string" || meta.length === 0) return plan;
|
|
21
|
+
const rest = meta.replace(FOCUS_META_PATTERN, (_match, body) => {
|
|
22
|
+
addRanges(plan.focus, parseLineRanges(body));
|
|
23
|
+
return " ";
|
|
24
|
+
});
|
|
25
|
+
for (const match of rest.matchAll(HIGHLIGHT_META_PATTERN)) {
|
|
26
|
+
addRanges(plan.highlight, parseLineRanges(match[1] ?? ""));
|
|
27
|
+
}
|
|
28
|
+
return normalizePlan(plan);
|
|
29
|
+
}
|
|
30
|
+
function parseLineRanges(spec) {
|
|
31
|
+
const lines = [];
|
|
32
|
+
for (const part of spec.split(",")) {
|
|
33
|
+
const trimmed = part.trim();
|
|
34
|
+
if (trimmed === "") continue;
|
|
35
|
+
const range = /^(\d+)\s*-\s*(\d+)$/.exec(trimmed);
|
|
36
|
+
if (range) {
|
|
37
|
+
const first = Number(range[1]);
|
|
38
|
+
const second = Number(range[2]);
|
|
39
|
+
const from = Math.min(first, second);
|
|
40
|
+
const to = Math.max(first, second);
|
|
41
|
+
for (let line = from; line <= to; line += 1) lines.push(line);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (/^\d+$/.test(trimmed)) lines.push(Number(trimmed));
|
|
45
|
+
}
|
|
46
|
+
return lines;
|
|
47
|
+
}
|
|
48
|
+
function stripInlineCodeAnnotation(line) {
|
|
49
|
+
const match = INLINE_ANNOTATION_PATTERN.exec(line);
|
|
50
|
+
if (!match) return { line, annotation: null };
|
|
51
|
+
const kind = normalizeKind(match[2] ?? match[4]);
|
|
52
|
+
if (!kind) return { line, annotation: null };
|
|
53
|
+
const rawCount = match[3] ?? match[5];
|
|
54
|
+
const parsedCount = rawCount === void 0 ? 1 : Number(rawCount);
|
|
55
|
+
const lineCount = Number.isFinite(parsedCount) ? Math.max(1, parsedCount) : 1;
|
|
56
|
+
const index = match.index ?? line.length;
|
|
57
|
+
const stripped = line.slice(0, index).replace(/[ \t]+$/, "");
|
|
58
|
+
return { line: stripped, annotation: { kind, lineCount } };
|
|
59
|
+
}
|
|
60
|
+
function collectCodeAnnotations(meta, code) {
|
|
61
|
+
const plan = parseCodeAnnotations(meta);
|
|
62
|
+
const strippedLines = [];
|
|
63
|
+
code.split("\n").forEach((rawLine, index) => {
|
|
64
|
+
const { line, annotation } = stripInlineCodeAnnotation(rawLine);
|
|
65
|
+
strippedLines.push(line);
|
|
66
|
+
if (!annotation) return;
|
|
67
|
+
const lineNumber = index + 1;
|
|
68
|
+
if (annotation.kind === "focus") {
|
|
69
|
+
for (let focused = lineNumber; focused < lineNumber + annotation.lineCount; focused += 1) {
|
|
70
|
+
plan.focus.push(focused);
|
|
71
|
+
}
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
plan[annotation.kind].push(lineNumber);
|
|
75
|
+
});
|
|
76
|
+
const hasAnnotations2 = ANNOTATION_KEYS.some((key) => plan[key].length > 0);
|
|
77
|
+
return {
|
|
78
|
+
plan: hasAnnotations2 ? normalizePlan(plan) : createEmptyPlan(),
|
|
79
|
+
code: strippedLines.join("\n"),
|
|
80
|
+
hasAnnotations: hasAnnotations2
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function normalizePlan(plan) {
|
|
84
|
+
return {
|
|
85
|
+
highlight: normalizeLines(plan.highlight),
|
|
86
|
+
added: normalizeLines(plan.added),
|
|
87
|
+
removed: normalizeLines(plan.removed),
|
|
88
|
+
focus: normalizeLines(plan.focus)
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function hasAnnotations(plan) {
|
|
92
|
+
if (!plan) return false;
|
|
93
|
+
return ANNOTATION_KEYS.some((key) => plan[key].length > 0);
|
|
94
|
+
}
|
|
95
|
+
function serializeAnnotations(plan) {
|
|
96
|
+
return JSON.stringify(normalizePlan(plan));
|
|
97
|
+
}
|
|
98
|
+
function deserializeAnnotations(value) {
|
|
99
|
+
if (typeof value !== "string" || value === "") return null;
|
|
100
|
+
try {
|
|
101
|
+
return coercePlan(JSON.parse(value));
|
|
102
|
+
} catch {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function encodeAnnotationsMeta(plan) {
|
|
107
|
+
return `${ANNOTATIONS_META_PREFIX}${encodeURIComponent(
|
|
108
|
+
serializeAnnotations(plan)
|
|
109
|
+
)}`;
|
|
110
|
+
}
|
|
111
|
+
function appendAnnotationsMeta(meta, plan) {
|
|
112
|
+
const token = encodeAnnotationsMeta(plan);
|
|
113
|
+
const base = typeof meta === "string" ? meta.trim() : "";
|
|
114
|
+
return base === "" ? token : `${base} ${token}`;
|
|
115
|
+
}
|
|
116
|
+
function extractAnnotationsMeta(meta) {
|
|
117
|
+
if (typeof meta !== "string") return null;
|
|
118
|
+
const match = meta.match(/(?:^|\s)rb-annotations=([^\s]+)/);
|
|
119
|
+
if (!match) return null;
|
|
120
|
+
try {
|
|
121
|
+
return coercePlan(JSON.parse(decodeURIComponent(match[1])));
|
|
122
|
+
} catch {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function removeAnnotationsMeta(meta) {
|
|
127
|
+
if (typeof meta !== "string") return "";
|
|
128
|
+
return meta.replace(/(?:^|\s)rb-annotations=[^\s]+/g, " ").trim().replace(/\s+/g, " ");
|
|
129
|
+
}
|
|
130
|
+
function coercePlan(value) {
|
|
131
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
const record = value;
|
|
135
|
+
return {
|
|
136
|
+
highlight: coerceLines(record.highlight),
|
|
137
|
+
added: coerceLines(record.added),
|
|
138
|
+
removed: coerceLines(record.removed),
|
|
139
|
+
focus: coerceLines(record.focus)
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
function coerceLines(value) {
|
|
143
|
+
if (!Array.isArray(value)) return [];
|
|
144
|
+
return value.filter(
|
|
145
|
+
(entry) => typeof entry === "number" && Number.isFinite(entry) && entry > 0
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
function addRanges(target, ranges) {
|
|
149
|
+
target.push(...ranges);
|
|
150
|
+
}
|
|
151
|
+
function normalizeLines(lines) {
|
|
152
|
+
return [...new Set(lines.filter((line) => line > 0))].sort(
|
|
153
|
+
(left, right) => left - right
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
function normalizeKind(kind) {
|
|
157
|
+
if (kind === "++") return "added";
|
|
158
|
+
if (kind === "--") return "removed";
|
|
159
|
+
if (kind === "focus") return "focus";
|
|
160
|
+
if (kind === "highlight") return "highlight";
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// src/hast.ts
|
|
165
|
+
function isElementNode(node) {
|
|
166
|
+
return node.type === "element";
|
|
167
|
+
}
|
|
168
|
+
function text(value) {
|
|
169
|
+
return { type: "text", value };
|
|
170
|
+
}
|
|
171
|
+
function element(tagName, properties = {}, children = []) {
|
|
172
|
+
return { type: "element", tagName, properties, children };
|
|
173
|
+
}
|
|
174
|
+
function visitElementTree(node, visitor, ancestors = []) {
|
|
175
|
+
if (isElementNode(node)) {
|
|
176
|
+
visitor(node, ancestors);
|
|
177
|
+
}
|
|
178
|
+
const nextAncestors = isElementNode(node) ? [...ancestors, node] : ancestors;
|
|
179
|
+
for (const child of getChildren(node)) {
|
|
180
|
+
visitElementTree(child, visitor, nextAncestors);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
function getStringProperty(node, key) {
|
|
184
|
+
const value = getProperty(node, key);
|
|
185
|
+
if (typeof value === "string") return value;
|
|
186
|
+
if (Array.isArray(value)) return value.join(" ");
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
function getBooleanProperty(node, key) {
|
|
190
|
+
const value = getProperty(node, key);
|
|
191
|
+
return value === true || value === "true" || value === "";
|
|
192
|
+
}
|
|
193
|
+
function hasProperty(node, key) {
|
|
194
|
+
return getProperty(node, key) !== void 0;
|
|
195
|
+
}
|
|
196
|
+
function getClassNameList(node) {
|
|
197
|
+
const value = node.properties?.className;
|
|
198
|
+
if (Array.isArray(value)) {
|
|
199
|
+
return value.filter((entry) => typeof entry === "string");
|
|
200
|
+
}
|
|
201
|
+
if (typeof value === "string") {
|
|
202
|
+
return value.split(/\s+/).filter(Boolean);
|
|
203
|
+
}
|
|
204
|
+
return [];
|
|
205
|
+
}
|
|
206
|
+
function mergeClassName(current, next) {
|
|
207
|
+
const currentClass = Array.isArray(current) ? current.join(" ") : typeof current === "string" ? current : "";
|
|
208
|
+
if (next === "") return currentClass;
|
|
209
|
+
return currentClass ? `${currentClass} ${next}` : next;
|
|
210
|
+
}
|
|
211
|
+
function getDataMeta(node) {
|
|
212
|
+
if (node === null || node === void 0) return null;
|
|
213
|
+
const data = node.data;
|
|
214
|
+
if (data === null || typeof data !== "object") return null;
|
|
215
|
+
const meta = data.meta;
|
|
216
|
+
return typeof meta === "string" ? meta : null;
|
|
217
|
+
}
|
|
218
|
+
function getTextContent(node) {
|
|
219
|
+
if (node.type === "text" && "value" in node && typeof node.value === "string") {
|
|
220
|
+
return node.value;
|
|
221
|
+
}
|
|
222
|
+
return getChildren(node).map(getTextContent).join("");
|
|
223
|
+
}
|
|
224
|
+
function getProperty(node, key) {
|
|
225
|
+
const value = node.properties?.[key];
|
|
226
|
+
if (value !== void 0) return value;
|
|
227
|
+
const hyphenated = key.replace(
|
|
228
|
+
/[A-Z]/g,
|
|
229
|
+
(match) => `-${match.toLowerCase()}`
|
|
230
|
+
);
|
|
231
|
+
if (hyphenated !== key) return node.properties?.[hyphenated];
|
|
232
|
+
return void 0;
|
|
233
|
+
}
|
|
234
|
+
function getChildren(node) {
|
|
235
|
+
const children = node.children;
|
|
236
|
+
return Array.isArray(children) ? children : [];
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// src/options.ts
|
|
240
|
+
var DEFAULT_CODE_ANNOTATIONS_OPTIONS = {
|
|
241
|
+
className: "rb-code",
|
|
242
|
+
lineClassName: "rb-code__line",
|
|
243
|
+
highlightClassName: "rb-code__line--highlighted",
|
|
244
|
+
addedClassName: "rb-code__line--added",
|
|
245
|
+
removedClassName: "rb-code__line--removed",
|
|
246
|
+
focusClassName: "rb-code__line--focused"
|
|
247
|
+
};
|
|
248
|
+
function resolveCodeAnnotationsOptions(options = {}) {
|
|
249
|
+
const resolved = {
|
|
250
|
+
className: options.className ?? DEFAULT_CODE_ANNOTATIONS_OPTIONS.className,
|
|
251
|
+
lineClassName: options.lineClassName ?? DEFAULT_CODE_ANNOTATIONS_OPTIONS.lineClassName,
|
|
252
|
+
highlightClassName: options.highlightClassName ?? DEFAULT_CODE_ANNOTATIONS_OPTIONS.highlightClassName,
|
|
253
|
+
addedClassName: options.addedClassName ?? DEFAULT_CODE_ANNOTATIONS_OPTIONS.addedClassName,
|
|
254
|
+
removedClassName: options.removedClassName ?? DEFAULT_CODE_ANNOTATIONS_OPTIONS.removedClassName,
|
|
255
|
+
focusClassName: options.focusClassName ?? DEFAULT_CODE_ANNOTATIONS_OPTIONS.focusClassName
|
|
256
|
+
};
|
|
257
|
+
const language = options.language?.trim();
|
|
258
|
+
if (language) resolved.language = language;
|
|
259
|
+
return resolved;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// src/rehype.ts
|
|
263
|
+
function rehypeCodeAnnotations(options = resolveCodeAnnotationsOptions()) {
|
|
264
|
+
return (tree) => {
|
|
265
|
+
visitElementTree(tree, (node, ancestors) => {
|
|
266
|
+
if (node.tagName !== "code") return;
|
|
267
|
+
const parent = ancestors[ancestors.length - 1];
|
|
268
|
+
if (parent?.tagName !== "pre") return;
|
|
269
|
+
const pre = parent;
|
|
270
|
+
const figure = findFigure(ancestors);
|
|
271
|
+
const plan = readPlan(node, pre, figure);
|
|
272
|
+
stripAnnotationsFromProperties(node);
|
|
273
|
+
if (plan === null || !hasAnnotations(plan)) return;
|
|
274
|
+
if (!languageMatches(node, options.language)) return;
|
|
275
|
+
applyPlan(node, pre, figure, plan, options);
|
|
276
|
+
});
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
function findFigure(ancestors) {
|
|
280
|
+
for (let index = ancestors.length - 1; index >= 0; index -= 1) {
|
|
281
|
+
if (getBooleanProperty(ancestors[index], "dataRehypePrettyCodeFigure")) {
|
|
282
|
+
return ancestors[index];
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return null;
|
|
286
|
+
}
|
|
287
|
+
function readPlan(code, pre, figure) {
|
|
288
|
+
const fromAttribute = deserializeAnnotations(
|
|
289
|
+
getStringProperty(code, "dataRbCodeAnnotations") ?? getStringProperty(code, "data-rb-code-annotations")
|
|
290
|
+
);
|
|
291
|
+
if (fromAttribute) return fromAttribute;
|
|
292
|
+
const metas = [getDataMeta(figure), getDataMeta(pre), getDataMeta(code)];
|
|
293
|
+
for (const meta of metas) {
|
|
294
|
+
const plan = extractAnnotationsMeta(meta);
|
|
295
|
+
if (plan) return plan;
|
|
296
|
+
}
|
|
297
|
+
return null;
|
|
298
|
+
}
|
|
299
|
+
function stripAnnotationsFromProperties(code) {
|
|
300
|
+
if (!code.properties) return;
|
|
301
|
+
delete code.properties["data-rb-code-annotations"];
|
|
302
|
+
delete code.properties.dataRbCodeAnnotations;
|
|
303
|
+
for (const key of ["data-meta", "dataMeta"]) {
|
|
304
|
+
const value = code.properties[key];
|
|
305
|
+
if (typeof value !== "string") continue;
|
|
306
|
+
const stripped = removeAnnotationsMeta(value);
|
|
307
|
+
if (stripped !== "") code.properties[key] = stripped;
|
|
308
|
+
else delete code.properties[key];
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
function applyPlan(code, pre, figure, plan, options) {
|
|
312
|
+
const root = figure && getBooleanProperty(figure, "dataRehypePrettyCodeFigure") ? figure : pre;
|
|
313
|
+
root.properties = {
|
|
314
|
+
...root.properties,
|
|
315
|
+
className: mergeClassName(root.properties?.className, options.className)
|
|
316
|
+
};
|
|
317
|
+
const lines = getLineElements(code);
|
|
318
|
+
if (lines.length > 0) {
|
|
319
|
+
lines.forEach((line, index) => {
|
|
320
|
+
applyLineClasses(line, index + 1, plan, options);
|
|
321
|
+
});
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
const rawLines = getTextContent(code).replace(/\n$/, "").split("\n");
|
|
325
|
+
const children = [];
|
|
326
|
+
rawLines.forEach((lineText, index) => {
|
|
327
|
+
const classNames = [
|
|
328
|
+
options.lineClassName,
|
|
329
|
+
...annotationClassNames(index + 1, plan, options)
|
|
330
|
+
];
|
|
331
|
+
children.push(
|
|
332
|
+
element(
|
|
333
|
+
"span",
|
|
334
|
+
{
|
|
335
|
+
className: classNames.join(" "),
|
|
336
|
+
dataLine: String(index + 1)
|
|
337
|
+
},
|
|
338
|
+
[text(lineText)]
|
|
339
|
+
)
|
|
340
|
+
);
|
|
341
|
+
if (index < rawLines.length - 1) children.push(text("\n"));
|
|
342
|
+
});
|
|
343
|
+
code.children = children;
|
|
344
|
+
}
|
|
345
|
+
function applyLineClasses(line, lineNumber, plan, options) {
|
|
346
|
+
for (const name of annotationClassNames(lineNumber, plan, options)) {
|
|
347
|
+
line.properties = {
|
|
348
|
+
...line.properties,
|
|
349
|
+
className: mergeClassName(line.properties?.className, name)
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
function annotationClassNames(lineNumber, plan, options) {
|
|
354
|
+
const names = [];
|
|
355
|
+
if (plan.highlight.includes(lineNumber)) {
|
|
356
|
+
names.push(options.highlightClassName);
|
|
357
|
+
}
|
|
358
|
+
if (plan.added.includes(lineNumber)) names.push(options.addedClassName);
|
|
359
|
+
if (plan.removed.includes(lineNumber)) names.push(options.removedClassName);
|
|
360
|
+
if (plan.focus.includes(lineNumber)) names.push(options.focusClassName);
|
|
361
|
+
return names;
|
|
362
|
+
}
|
|
363
|
+
function getLineElements(code) {
|
|
364
|
+
return (code.children ?? []).filter(isElementNode).filter((child) => {
|
|
365
|
+
if (child.tagName !== "span") return false;
|
|
366
|
+
if (hasProperty(child, "dataLine")) return true;
|
|
367
|
+
const classNames = getClassNameList(child);
|
|
368
|
+
return classNames.includes("line") || classNames.some((name) => name.endsWith("__line"));
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
function languageMatches(code, language) {
|
|
372
|
+
if (!language) return true;
|
|
373
|
+
return detectLanguage(code) === language;
|
|
374
|
+
}
|
|
375
|
+
function detectLanguage(code) {
|
|
376
|
+
const explicit = getStringProperty(code, "dataLanguage") ?? getStringProperty(code, "data-language");
|
|
377
|
+
if (explicit) return explicit;
|
|
378
|
+
for (const name of getClassNameList(code)) {
|
|
379
|
+
if (name.startsWith("language-")) return name.slice("language-".length);
|
|
380
|
+
}
|
|
381
|
+
return null;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// src/remark.ts
|
|
385
|
+
function remarkCodeAnnotations(options = resolveCodeAnnotationsOptions()) {
|
|
386
|
+
return (tree) => {
|
|
387
|
+
visitMdast(tree, (node) => {
|
|
388
|
+
if (node.type !== "code" || typeof node.value !== "string") return;
|
|
389
|
+
if (options.language !== void 0 && node.lang !== options.language) {
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
const meta = typeof node.meta === "string" ? node.meta : null;
|
|
393
|
+
const { plan, code, hasAnnotations: hasAnnotations2 } = collectCodeAnnotations(
|
|
394
|
+
meta,
|
|
395
|
+
node.value
|
|
396
|
+
);
|
|
397
|
+
if (!hasAnnotations2) return;
|
|
398
|
+
node.value = code;
|
|
399
|
+
const hProperties = {
|
|
400
|
+
...node.data?.hProperties
|
|
401
|
+
};
|
|
402
|
+
hProperties["data-rb-code-annotations"] = serializeAnnotations(plan);
|
|
403
|
+
const existingMeta = hProperties["data-meta"] ?? hProperties.dataMeta;
|
|
404
|
+
hProperties["data-meta"] = typeof existingMeta === "string" && existingMeta !== "" ? appendAnnotationsMeta(existingMeta, plan) : encodeAnnotationsMeta(plan);
|
|
405
|
+
node.data = { ...node.data ?? {}, hProperties };
|
|
406
|
+
});
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
function visitMdast(node, visitor) {
|
|
410
|
+
visitor(node);
|
|
411
|
+
for (const child of node.children ?? []) visitMdast(child, visitor);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// index.ts
|
|
415
|
+
function codeAnnotations(options = {}) {
|
|
416
|
+
const resolved = resolveCodeAnnotationsOptions(options);
|
|
417
|
+
return definePlugin({
|
|
418
|
+
name: "code-annotations",
|
|
419
|
+
order: 10,
|
|
420
|
+
options: resolved,
|
|
421
|
+
extendMarkdownPipeline: (pipeline) => {
|
|
422
|
+
pipeline.use(remarkCodeAnnotations, resolved);
|
|
423
|
+
},
|
|
424
|
+
extendHtmlPipeline: (pipeline) => {
|
|
425
|
+
pipeline.use(rehypeCodeAnnotations, resolved);
|
|
426
|
+
},
|
|
427
|
+
assets: [createStyleAsset("code-annotations")]
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
var codeAnnotationsPlugin = codeAnnotations;
|
|
431
|
+
export {
|
|
432
|
+
ANNOTATIONS_META_PREFIX,
|
|
433
|
+
DEFAULT_CODE_ANNOTATIONS_OPTIONS,
|
|
434
|
+
codeAnnotations,
|
|
435
|
+
codeAnnotationsPlugin,
|
|
436
|
+
collectCodeAnnotations,
|
|
437
|
+
createEmptyPlan,
|
|
438
|
+
deserializeAnnotations,
|
|
439
|
+
encodeAnnotationsMeta,
|
|
440
|
+
extractAnnotationsMeta,
|
|
441
|
+
hasAnnotations,
|
|
442
|
+
parseCodeAnnotations,
|
|
443
|
+
parseLineRanges,
|
|
444
|
+
rehypeCodeAnnotations,
|
|
445
|
+
remarkCodeAnnotations,
|
|
446
|
+
resolveCodeAnnotationsOptions,
|
|
447
|
+
serializeAnnotations,
|
|
448
|
+
stripInlineCodeAnnotation
|
|
449
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { CodeAnnotationKind, CodeAnnotationPlan } from "./types.js";
|
|
2
|
+
/** Prefix used to smuggle a plan through the preserved fence meta channel. */
|
|
3
|
+
export declare const ANNOTATIONS_META_PREFIX = "rb-annotations=";
|
|
4
|
+
export declare function createEmptyPlan(): CodeAnnotationPlan;
|
|
5
|
+
/**
|
|
6
|
+
* Parse the highlight/focus specifiers of a code fence meta string.
|
|
7
|
+
*
|
|
8
|
+
* Docusaurus-style `{1,3-5}` groups become `highlight` lines. An optional
|
|
9
|
+
* `focus:{1,2}` (or `focus={1,2}`) group becomes `focus` lines. Inline marker
|
|
10
|
+
* comments are not part of the meta string and are handled by
|
|
11
|
+
* {@link collectCodeAnnotations}.
|
|
12
|
+
*/
|
|
13
|
+
export declare function parseCodeAnnotations(meta: string | null | undefined): CodeAnnotationPlan;
|
|
14
|
+
/**
|
|
15
|
+
* Parse a numeric range list such as `1,3-5` into sorted 1-based line numbers.
|
|
16
|
+
*/
|
|
17
|
+
export declare function parseLineRanges(spec: string): number[];
|
|
18
|
+
export type InlineCodeAnnotation = {
|
|
19
|
+
kind: CodeAnnotationKind;
|
|
20
|
+
lineCount: number;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Remove a trailing `[!code ...]` marker comment from a line and report what
|
|
24
|
+
* it annotated. The comment prefix may be `//`, `#`, `--`, or an HTML comment.
|
|
25
|
+
*/
|
|
26
|
+
export declare function stripInlineCodeAnnotation(line: string): {
|
|
27
|
+
line: string;
|
|
28
|
+
annotation: InlineCodeAnnotation | null;
|
|
29
|
+
};
|
|
30
|
+
export type CollectedCodeAnnotations = {
|
|
31
|
+
plan: CodeAnnotationPlan;
|
|
32
|
+
code: string;
|
|
33
|
+
hasAnnotations: boolean;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Parse fence meta and inline markers from a code block, returning the
|
|
37
|
+
* annotation plan and the code text with marker comments removed.
|
|
38
|
+
*/
|
|
39
|
+
export declare function collectCodeAnnotations(meta: string | null | undefined, code: string): CollectedCodeAnnotations;
|
|
40
|
+
export declare function normalizePlan(plan: CodeAnnotationPlan): CodeAnnotationPlan;
|
|
41
|
+
export declare function hasAnnotations(plan: CodeAnnotationPlan | null): boolean;
|
|
42
|
+
export declare function serializeAnnotations(plan: CodeAnnotationPlan): string;
|
|
43
|
+
export declare function deserializeAnnotations(value: unknown): CodeAnnotationPlan | null;
|
|
44
|
+
export declare function encodeAnnotationsMeta(plan: CodeAnnotationPlan): string;
|
|
45
|
+
export declare function appendAnnotationsMeta(meta: string | null | undefined, plan: CodeAnnotationPlan): string;
|
|
46
|
+
export declare function extractAnnotationsMeta(meta: string | null | undefined): CodeAnnotationPlan | null;
|
|
47
|
+
/** Remove an embedded plan token, keeping any surrounding fence meta. */
|
|
48
|
+
export declare function removeAnnotationsMeta(meta: string | null | undefined): string;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ElementNode, HastNode, TextNode } from "./types.js";
|
|
2
|
+
export declare function isElementNode(node: HastNode): node is ElementNode;
|
|
3
|
+
export declare function text(value: string): TextNode;
|
|
4
|
+
export declare function element(tagName: string, properties?: Record<string, unknown>, children?: HastNode[]): ElementNode;
|
|
5
|
+
export declare function visitElementTree(node: HastNode, visitor: (node: ElementNode, ancestors: ElementNode[]) => void, ancestors?: ElementNode[]): void;
|
|
6
|
+
export declare function getStringProperty(node: ElementNode, key: string): string | null;
|
|
7
|
+
export declare function getBooleanProperty(node: ElementNode, key: string): boolean;
|
|
8
|
+
export declare function hasProperty(node: ElementNode, key: string): boolean;
|
|
9
|
+
export declare function getClassNameList(node: ElementNode): string[];
|
|
10
|
+
export declare function mergeClassName(current: unknown, next: string): string;
|
|
11
|
+
export declare function getDataMeta(node: ElementNode | null | undefined): string | null;
|
|
12
|
+
export declare function getTextContent(node: HastNode): string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { CodeAnnotationsOptions, ResolvedCodeAnnotationsOptions } from "./types.js";
|
|
2
|
+
export declare const DEFAULT_CODE_ANNOTATIONS_OPTIONS: {
|
|
3
|
+
readonly className: "rb-code";
|
|
4
|
+
readonly lineClassName: "rb-code__line";
|
|
5
|
+
readonly highlightClassName: "rb-code__line--highlighted";
|
|
6
|
+
readonly addedClassName: "rb-code__line--added";
|
|
7
|
+
readonly removedClassName: "rb-code__line--removed";
|
|
8
|
+
readonly focusClassName: "rb-code__line--focused";
|
|
9
|
+
};
|
|
10
|
+
export declare function resolveCodeAnnotationsOptions(options?: CodeAnnotationsOptions): ResolvedCodeAnnotationsOptions;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { HastNode, ResolvedCodeAnnotationsOptions } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Materializes annotation plans recorded by {@link remarkCodeAnnotations}.
|
|
4
|
+
* Reuses existing per-line wrappers (for example the ones emitted by
|
|
5
|
+
* `rehype-pretty-code` through `@riebeckite/plugin-code-enhance`) and falls
|
|
6
|
+
* back to wrapping raw `<code>` text into `rb-code__line` spans.
|
|
7
|
+
*/
|
|
8
|
+
export declare function rehypeCodeAnnotations(options?: ResolvedCodeAnnotationsOptions): (tree: HastNode) => void;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ResolvedCodeAnnotationsOptions } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Reads the fence meta and inline `[!code ...]` markers of every code block,
|
|
4
|
+
* strips the marker comments from the code text, and records the resulting
|
|
5
|
+
* plan for the rehype stage.
|
|
6
|
+
*
|
|
7
|
+
* The plan travels through two properties:
|
|
8
|
+
*
|
|
9
|
+
* - `data-rb-code-annotations` is the primary channel and is read directly for
|
|
10
|
+
* plain `<pre><code>` blocks.
|
|
11
|
+
* - `data-meta` carries an encoded copy because
|
|
12
|
+
* `rehype-pretty-code` (used by `@riebeckite/plugin-code-enhance`) replaces
|
|
13
|
+
* the `<code>` element and drops its properties. code-enhance already reads
|
|
14
|
+
* `data-meta`, so the copy survives on the generated `<figure>`.
|
|
15
|
+
*/
|
|
16
|
+
export declare function remarkCodeAnnotations(options?: ResolvedCodeAnnotationsOptions): (tree: unknown) => void;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export type CodeAnnotationsOptions = {
|
|
2
|
+
/**
|
|
3
|
+
* Class applied to the code block root (`<pre>` or the
|
|
4
|
+
* `rehype-pretty-code` `<figure>`). Defaults to `"rb-code"`.
|
|
5
|
+
*/
|
|
6
|
+
className?: string;
|
|
7
|
+
/**
|
|
8
|
+
* Class applied to each generated line wrapper. Defaults to
|
|
9
|
+
* `"rb-code__line"`. Existing line wrappers are not re-classed.
|
|
10
|
+
*/
|
|
11
|
+
lineClassName?: string;
|
|
12
|
+
/** Class applied to highlighted lines. Defaults to `"rb-code__line--highlighted"`. */
|
|
13
|
+
highlightClassName?: string;
|
|
14
|
+
/** Class applied to `++` lines. Defaults to `"rb-code__line--added"`. */
|
|
15
|
+
addedClassName?: string;
|
|
16
|
+
/** Class applied to `--` lines. Defaults to `"rb-code__line--removed"`. */
|
|
17
|
+
removedClassName?: string;
|
|
18
|
+
/** Class applied to focused lines. Defaults to `"rb-code__line--focused"`. */
|
|
19
|
+
focusClassName?: string;
|
|
20
|
+
/**
|
|
21
|
+
* When set, annotations are only applied to code blocks whose detected
|
|
22
|
+
* language matches this value. Unset by default.
|
|
23
|
+
*/
|
|
24
|
+
language?: string;
|
|
25
|
+
};
|
|
26
|
+
export type ResolvedCodeAnnotationsOptions = {
|
|
27
|
+
className: string;
|
|
28
|
+
lineClassName: string;
|
|
29
|
+
highlightClassName: string;
|
|
30
|
+
addedClassName: string;
|
|
31
|
+
removedClassName: string;
|
|
32
|
+
focusClassName: string;
|
|
33
|
+
language?: string;
|
|
34
|
+
};
|
|
35
|
+
export type CodeAnnotationPlan = {
|
|
36
|
+
highlight: number[];
|
|
37
|
+
added: number[];
|
|
38
|
+
removed: number[];
|
|
39
|
+
focus: number[];
|
|
40
|
+
};
|
|
41
|
+
export type CodeAnnotationKind = "highlight" | "added" | "removed" | "focus";
|
|
42
|
+
export type ElementNode = {
|
|
43
|
+
type: string;
|
|
44
|
+
tagName?: string;
|
|
45
|
+
properties?: Record<string, unknown>;
|
|
46
|
+
data?: Record<string, unknown>;
|
|
47
|
+
children?: HastNode[];
|
|
48
|
+
};
|
|
49
|
+
export type TextNode = {
|
|
50
|
+
type: "text";
|
|
51
|
+
value: string;
|
|
52
|
+
};
|
|
53
|
+
export type HastNode = ElementNode | TextNode | {
|
|
54
|
+
type: string;
|
|
55
|
+
[key: string]: unknown;
|
|
56
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@riebeckite/plugin-code-annotations",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"source": "./index.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./style.css": "./style.css"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@riebeckite/core": "0.0.1"
|
|
19
|
+
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"LICENSE",
|
|
25
|
+
"README.md",
|
|
26
|
+
"README_ja.md",
|
|
27
|
+
"dist",
|
|
28
|
+
"style.css"
|
|
29
|
+
],
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/Rerurate514/riebeckite.git",
|
|
33
|
+
"directory": "packages/plugins/code-annotations"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/Rerurate514/riebeckite#readme",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/Rerurate514/riebeckite/issues"
|
|
38
|
+
},
|
|
39
|
+
"description": "Code block annotations, highlights, and diff markers for Riebeckite.",
|
|
40
|
+
"keywords": [
|
|
41
|
+
"riebeckite",
|
|
42
|
+
"plugin",
|
|
43
|
+
"code-blocks",
|
|
44
|
+
"syntax-highlighting"
|
|
45
|
+
],
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
48
|
+
},
|
|
49
|
+
"sideEffects": [
|
|
50
|
+
"./style.css"
|
|
51
|
+
],
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "node ../../../scripts/build_package.mjs"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/style.css
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
.rb-code {
|
|
2
|
+
--rb-code-annotation-accent: var(--rb-color-accent, #686868);
|
|
3
|
+
position: relative;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
.rb-code__line {
|
|
7
|
+
display: block;
|
|
8
|
+
min-width: 100%;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
.rb-code__line--highlighted {
|
|
12
|
+
background: color-mix(
|
|
13
|
+
in srgb,
|
|
14
|
+
var(--rb-code-annotation-accent) 14%,
|
|
15
|
+
transparent
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.rb-code__line--added {
|
|
20
|
+
background: color-mix(in srgb, #1f883d 14%, transparent);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
.rb-code__line--removed {
|
|
24
|
+
background: color-mix(in srgb, #cf222e 14%, transparent);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
.rb-code__line--focused {
|
|
28
|
+
background: color-mix(
|
|
29
|
+
in srgb,
|
|
30
|
+
var(--rb-code-annotation-accent) 10%,
|
|
31
|
+
transparent
|
|
32
|
+
);
|
|
33
|
+
outline: 1px solid
|
|
34
|
+
color-mix(in srgb, var(--rb-code-annotation-accent) 45%, transparent);
|
|
35
|
+
outline-offset: -1px;
|
|
36
|
+
}
|