rehype-highlight-code-lines 1.0.4 → 1.0.7
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/README.md +36 -77
- package/dist/esm/index.d.ts +10 -1
- package/dist/esm/index.js +234 -118
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -0
- package/package.json +31 -22
- package/src/index.ts +303 -157
package/README.md
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
# rehype-highlight-code-lines
|
|
2
2
|
|
|
3
|
-
[![
|
|
4
|
-
[![
|
|
5
|
-
[![
|
|
6
|
-
[![
|
|
7
|
-
[![type-coverage]
|
|
8
|
-
[![typescript][badge-typescript]][typescript
|
|
9
|
-
[![
|
|
3
|
+
[![npm version][badge-npm-version]][url-npm-package]
|
|
4
|
+
[![npm downloads][badge-npm-download]][url-npm-package]
|
|
5
|
+
[![publish to npm][badge-publish-to-npm]][url-publish-github-actions]
|
|
6
|
+
[![code-coverage][badge-codecov]][url-codecov]
|
|
7
|
+
[![type-coverage][badge-type-coverage]][url-github-package]
|
|
8
|
+
[![typescript][badge-typescript]][url-typescript]
|
|
9
|
+
[![license][badge-license]][url-license]
|
|
10
10
|
|
|
11
|
-
This package is a [unified][unified] ([rehype][rehype]) plugin **
|
|
11
|
+
This package is a **[unified][unified]** (**[rehype][rehype]**) plugin that **wraps each line of code in a container, enabling code block numbering and line highlighting**.
|
|
12
12
|
|
|
13
|
-
**[unified][unified]** is a project that transforms content with abstract syntax trees (ASTs) using the new parser **[micromark][micromark]**. **[remark][remark]** adds support for markdown to unified. **[mdast][mdast]** is the Markdown Abstract Syntax Tree (AST) which is a specification for representing markdown in a syntax tree.
|
|
13
|
+
**[unified][unified]** is a project that transforms content with abstract syntax trees (ASTs) using the new parser **[micromark][micromark]**. **[remark][remark]** adds support for markdown to unified. **[mdast][mdast]** is the Markdown Abstract Syntax Tree (AST) which is a specification for representing markdown in a syntax tree. **[rehype][rehype]** is a tool that transforms HTML with plugins. **[hast][hast]** stands for HTML Abstract Syntax Tree (HAST) that rehype uses.
|
|
14
14
|
|
|
15
|
-
**This plugin
|
|
15
|
+
**This plugin enables line numbering for code blocks and highlights specific lines as needed.**
|
|
16
16
|
|
|
17
17
|
## When should I use this?
|
|
18
18
|
|
|
19
|
-
The `rehype-highlight-code-lines`
|
|
19
|
+
The `rehype-highlight-code-lines` is ideal for adding line numbers to code blocks and highlighting specific lines.
|
|
20
20
|
|
|
21
21
|
**The `rehype-highlight-code-lines` is NOT code highlighter and does NOT provide code highlighting!** You can use a code highlighter for example **[rehype-highlight][rehype-highlight]** to highlight the code, then use the `rehype-highlight-code-lines` **after**.
|
|
22
22
|
|
|
@@ -42,9 +42,9 @@ yarn add rehype-highlight-code-lines
|
|
|
42
42
|
|
|
43
43
|
## Usage
|
|
44
44
|
|
|
45
|
-
In a code fence,
|
|
46
|
-
+ specify a range of
|
|
47
|
-
+
|
|
45
|
+
In a code fence, right after the language of the code block:
|
|
46
|
+
+ Use curly braces `{}` to specify a range of line numbers to highlight specific lines.
|
|
47
|
+
+ Add `showLineNumbers` to enable line numbering.
|
|
48
48
|
|
|
49
49
|
**\`\`\`[language] {2,4-6} showLineNumbers**
|
|
50
50
|
|
|
@@ -54,7 +54,7 @@ In a code fence, just after the language of the code block,
|
|
|
54
54
|
|
|
55
55
|
**\`\`\`[language] showLineNumbers**
|
|
56
56
|
|
|
57
|
-
You can use the specifiers without a language
|
|
57
|
+
You can use the specifiers without a language:
|
|
58
58
|
|
|
59
59
|
**\`\`\`{5} showLineNumbers**
|
|
60
60
|
|
|
@@ -140,7 +140,6 @@ All options are **optional** and have **default values**.
|
|
|
140
140
|
```typescript
|
|
141
141
|
type HighlightLinesOptions = {
|
|
142
142
|
showLineNumbers?: boolean; // default is "false"
|
|
143
|
-
lineContainerTagName?: "div" | "span"; // default is "span"
|
|
144
143
|
};
|
|
145
144
|
|
|
146
145
|
use(rehypeHighlightLines, HighlightLinesOptions);
|
|
@@ -176,28 +175,27 @@ Sometimes you may want to start the line numbering from a specific number. In th
|
|
|
176
175
|
|
|
177
176
|
#### `lineContainerTagName`
|
|
178
177
|
|
|
179
|
-
It is a
|
|
178
|
+
**It is a deprecated option.** Marked as `@deprecated`, will be removed in the next versions.
|
|
180
179
|
|
|
181
|
-
|
|
180
|
+
It was a **union** type option which was **"div" | "span"** for providing custom HTML tag name for code lines.
|
|
181
|
+
|
|
182
|
+
By default, it is `span` which is **inline** level container. If you set it as `div`, the container will still be `span` after deprecation.
|
|
182
183
|
|
|
183
184
|
```javascript
|
|
184
185
|
use(rehypeHighlightLines, {
|
|
185
|
-
lineContainerTagName: "div",
|
|
186
|
+
lineContainerTagName: "div", // @deprecated, always "span"
|
|
186
187
|
});
|
|
187
188
|
```
|
|
188
189
|
|
|
189
|
-
Now, the code line container's tag name will be `div`.
|
|
190
|
-
|
|
191
190
|
### Examples:
|
|
192
191
|
|
|
193
192
|
```typescript
|
|
194
193
|
// line numbering will occur as per directive "showLineNumber" and code-line containers will be <span> inline element
|
|
195
194
|
use(rehypeHighlightLines);
|
|
196
195
|
|
|
197
|
-
// all code blocks will be numbered and code-line containers will be <
|
|
196
|
+
// all code blocks will be numbered by default and code-line containers will be <span> inline element
|
|
198
197
|
use(rehypeHighlightLines, {
|
|
199
198
|
showLineNumbers: true,
|
|
200
|
-
lineContainerTagName: "div",
|
|
201
199
|
});
|
|
202
200
|
```
|
|
203
201
|
|
|
@@ -210,32 +208,6 @@ use(rehypeHighlightLines, {
|
|
|
210
208
|
+ [demo blog application](https://next-mdx-remote-client-in-app-router.vercel.app/) using `next-mdx-remote-client` within `Next.js app router`
|
|
211
209
|
+ [demo blog application](https://next-mdx-remote-client-in-pages-router.vercel.app/) using `next-mdx-remote-client` within `Next.js pages router`
|
|
212
210
|
|
|
213
|
-
## Copying Code Block's Content Issue
|
|
214
|
-
|
|
215
|
-
When the line container is "div" block element, the end of line character (eol) at the end of line causes unwanted extra blank line. In order to fix the issue, I've **removed the eol when it is "div", but kept the eol when it is "span"**.
|
|
216
|
-
|
|
217
|
-
But, **the lack of the eol when "div" causes another issue.** If you implement a button for copying code block content, it doesn't work as expected since all code are printed in a single line. In order to work around the issue, you can implement an `onClick` of the `button` like below:
|
|
218
|
-
|
|
219
|
-
*I assume you use a `useRef` for `<pre>` element.*
|
|
220
|
-
|
|
221
|
-
```javascript
|
|
222
|
-
const onClick = () => {
|
|
223
|
-
if (!preRef.current) return;
|
|
224
|
-
|
|
225
|
-
// clone the <code> element in order not to cause any change in actual DOM
|
|
226
|
-
const code = preRef.current.getElementsByTagName("code")[0].cloneNode(true);
|
|
227
|
-
|
|
228
|
-
// add eol to each code-line since there is no eol at the end when they are div
|
|
229
|
-
Array.from((code as HTMLElement).querySelectorAll("div.code-line")).forEach(
|
|
230
|
-
(line) => {
|
|
231
|
-
line.innerHTML = line.innerHTML + "\r";
|
|
232
|
-
}
|
|
233
|
-
);
|
|
234
|
-
|
|
235
|
-
void navigator.clipboard.writeText(code.textContent ?? "");
|
|
236
|
-
};
|
|
237
|
-
```
|
|
238
|
-
|
|
239
211
|
## Styling
|
|
240
212
|
|
|
241
213
|
The following styles can be added for **line highlighting** and **line numbering** to work correctly:
|
|
@@ -275,29 +247,22 @@ pre > code {
|
|
|
275
247
|
}
|
|
276
248
|
|
|
277
249
|
.code-line {
|
|
250
|
+
min-width: 100%;
|
|
278
251
|
padding-left: 12px;
|
|
279
252
|
padding-right: 12px;
|
|
280
253
|
margin-left: -12px;
|
|
281
254
|
margin-right: -12px;
|
|
282
255
|
border-left: 4px solid transparent; /* prepare for highlighted code-lines */
|
|
283
|
-
}
|
|
284
256
|
|
|
285
|
-
div.code-line:empty {
|
|
286
|
-
/* it is necessary because there is no even eol character in div code-lines */
|
|
287
|
-
height: 15.5938px; /* calculated height */
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
span.code-line {
|
|
291
|
-
min-width: 100%;
|
|
292
257
|
display: inline-block;
|
|
293
258
|
}
|
|
294
259
|
|
|
295
260
|
.code-line.inserted {
|
|
296
|
-
background-color: var(--color-inserted-line); /* inserted code-line (+)
|
|
261
|
+
background-color: var(--color-inserted-line); /* inserted code-line (+) */
|
|
297
262
|
}
|
|
298
263
|
|
|
299
264
|
.code-line.deleted {
|
|
300
|
-
background-color: var(--color-deleted-line); /* deleted code-line (-)
|
|
265
|
+
background-color: var(--color-deleted-line); /* deleted code-line (-) */
|
|
301
266
|
}
|
|
302
267
|
|
|
303
268
|
.highlighted-code-line {
|
|
@@ -324,7 +289,7 @@ This plugin modifies the `hast` (HTML abstract syntax tree).
|
|
|
324
289
|
|
|
325
290
|
## Types
|
|
326
291
|
|
|
327
|
-
This package is fully typed with [TypeScript][typescript].
|
|
292
|
+
This package is fully typed with [TypeScript][url-typescript].
|
|
328
293
|
|
|
329
294
|
The plugin exports the type `HighlightLinesOptions`.
|
|
330
295
|
|
|
@@ -375,17 +340,6 @@ I like to contribute the Unified / Remark / MDX ecosystem, so I recommend you to
|
|
|
375
340
|
|
|
376
341
|
[MIT License](./LICENSE) © ipikuka
|
|
377
342
|
|
|
378
|
-
### Keywords
|
|
379
|
-
|
|
380
|
-
🟩 [unified][unifiednpm] 🟩 [rehype][rehypenpm] 🟩 [rehype plugin][rehypepluginnpm] 🟩 [hast][hastnpm] 🟩 [markdown][markdownnpm] 🟩 [rehype-highlight][rehypehighlightnpm]
|
|
381
|
-
|
|
382
|
-
[unifiednpm]: https://www.npmjs.com/search?q=keywords:unified
|
|
383
|
-
[rehypenpm]: https://www.npmjs.com/search?q=keywords:rehype
|
|
384
|
-
[rehypepluginnpm]: https://www.npmjs.com/search?q=keywords:rehype%20plugin
|
|
385
|
-
[hastnpm]: https://www.npmjs.com/search?q=keywords:hast
|
|
386
|
-
[markdownnpm]: https://www.npmjs.com/search?q=keywords:markdown
|
|
387
|
-
[rehypehighlightnpm]: https://www.npmjs.com/search?q=keywords:rehype-highlight
|
|
388
|
-
|
|
389
343
|
[unified]: https://github.com/unifiedjs/unified
|
|
390
344
|
[micromark]: https://github.com/micromark/micromark
|
|
391
345
|
[remark]: https://github.com/remarkjs/remark
|
|
@@ -394,18 +348,23 @@ I like to contribute the Unified / Remark / MDX ecosystem, so I recommend you to
|
|
|
394
348
|
[rehype]: https://github.com/rehypejs/rehype
|
|
395
349
|
[rehypeplugins]: https://github.com/rehypejs/rehype/blob/main/doc/plugins.md
|
|
396
350
|
[hast]: https://github.com/syntax-tree/hast
|
|
397
|
-
[typescript]: https://www.typescriptlang.org/
|
|
398
351
|
[rehype-highlight]: https://github.com/rehypejs/rehype-highlight
|
|
399
352
|
|
|
400
353
|
[badge-npm-version]: https://img.shields.io/npm/v/rehype-highlight-code-lines
|
|
401
354
|
[badge-npm-download]:https://img.shields.io/npm/dt/rehype-highlight-code-lines
|
|
402
|
-
[npm-package
|
|
355
|
+
[url-npm-package]: https://www.npmjs.com/package/rehype-highlight-code-lines
|
|
356
|
+
[url-github-package]: https://github.com/ipikuka/rehype-highlight-code-lines
|
|
403
357
|
|
|
404
358
|
[badge-license]: https://img.shields.io/github/license/ipikuka/rehype-highlight-code-lines
|
|
405
|
-
[
|
|
359
|
+
[url-license]: https://github.com/ipikuka/rehype-highlight-code-lines/blob/main/LICENSE
|
|
406
360
|
|
|
407
|
-
[badge-
|
|
408
|
-
[github-
|
|
361
|
+
[badge-publish-to-npm]: https://github.com/ipikuka/rehype-highlight-code-lines/actions/workflows/publish.yml/badge.svg
|
|
362
|
+
[url-publish-github-actions]: https://github.com/ipikuka/rehype-highlight-code-lines/actions/workflows/publish.yml
|
|
409
363
|
|
|
410
364
|
[badge-typescript]: https://img.shields.io/npm/types/rehype-highlight-code-lines
|
|
411
|
-
[typescript
|
|
365
|
+
[url-typescript]: https://www.typescriptlang.org
|
|
366
|
+
|
|
367
|
+
[badge-codecov]: https://codecov.io/gh/ipikuka/rehype-highlight-code-lines/graph/badge.svg?token=RKrZlvMHwq
|
|
368
|
+
[url-codecov]: https://codecov.io/gh/ipikuka/rehype-highlight-code-lines
|
|
369
|
+
|
|
370
|
+
[badge-type-coverage]: https://img.shields.io/badge/dynamic/json.svg?label=type-coverage&prefix=%E2%89%A5&suffix=%&query=$.typeCoverage.atLeast&uri=https%3A%2F%2Fraw.githubusercontent.com%2Fipikuka%2Frehype-highlight-code-lines%2Fmain%2Fpackage.json
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
import type { Plugin } from "unified";
|
|
2
2
|
import type { Root } from "hast";
|
|
3
|
+
declare module "hast" {
|
|
4
|
+
interface Data {
|
|
5
|
+
meta?: string | undefined;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
3
8
|
export type HighlightLinesOptions = {
|
|
4
9
|
showLineNumbers?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* @deprecated container tag name is always "span"
|
|
12
|
+
* will be removed in the next versions
|
|
13
|
+
*/
|
|
5
14
|
lineContainerTagName?: "div" | "span";
|
|
6
15
|
};
|
|
7
16
|
export declare function clsx(arr: (string | false | null | undefined | 0)[]): string[];
|
|
8
17
|
/**
|
|
9
18
|
*
|
|
10
|
-
*
|
|
19
|
+
* add line numbers to code blocks and allow highlighting of desired code lines
|
|
11
20
|
*
|
|
12
21
|
*/
|
|
13
22
|
declare const plugin: Plugin<[HighlightLinesOptions?], Root>;
|