|
25 | 25 | * [Example: registering](#example-registering)
|
26 | 26 | * [Example: aliases](#example-aliases)
|
27 | 27 | * [Example: sanitation](#example-sanitation)
|
| 28 | + * [Example: line numbering and highlighting](#example-line-numbering-and-highlighting) |
28 | 29 | * [Types](#types)
|
29 | 30 | * [Compatibility](#compatibility)
|
30 | 31 | * [Security](#security)
|
@@ -349,6 +350,43 @@ const file = await unified()
|
349 | 350 | console.log(String(file))
|
350 | 351 | ```
|
351 | 352 |
|
| 353 | +### Example: line numbering and highlighting |
| 354 | +
|
| 355 | +You can add support for line numbers and line highlighting with a separate |
| 356 | +plugin, [`rehype-highlight-code-lines`][rehype-highlight-code-lines]. |
| 357 | +
|
| 358 | +For example, with `example.html`: |
| 359 | +
|
| 360 | +```html |
| 361 | +<pre><code class="language-js">console.log("Hi!")</code></pre> |
| 362 | +``` |
| 363 | +
|
| 364 | +…and `example.js`: |
| 365 | +
|
| 366 | +```js |
| 367 | +import {rehype} from 'rehype' |
| 368 | +import rehypeHighlight from 'rehype-highlight' |
| 369 | +import rehypeHighlightCodeLines from 'rehype-highlight-code-lines' |
| 370 | +import {read} from 'to-vfile' |
| 371 | + |
| 372 | +const file = await rehype() |
| 373 | + .data('settings', {fragment: true}) |
| 374 | + .use(rehypeHighlight) |
| 375 | + .use(rehypeHighlightCodeLines, { |
| 376 | + showLineNumbers: true, |
| 377 | + lineContainerTagName: 'div' |
| 378 | + }) |
| 379 | + .process(await read('example.html')) |
| 380 | + |
| 381 | +console.log(String(file)) |
| 382 | +``` |
| 383 | +
|
| 384 | +…then running that yields: |
| 385 | +
|
| 386 | +```html |
| 387 | +<pre><code class="hljs language-js"><div class="code-line numbered-code-line" data-line-number="1"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">"Hi!"</span>)</div></code></pre> |
| 388 | +``` |
| 389 | +
|
352 | 390 | ## Types
|
353 | 391 |
|
354 | 392 | This package is fully typed with [TypeScript][].
|
@@ -381,6 +419,8 @@ When in doubt, use [`rehype-sanitize`][rehype-sanitize].
|
381 | 419 | — add metadata to the head of a document
|
382 | 420 | * [`rehype-document`](https://github.com/rehypejs/rehype-document)
|
383 | 421 | — wrap a fragment in a document
|
| 422 | +* [`rehype-highlight-code-lines`][rehype-highlight-code-lines] |
| 423 | + — add line numbers and highlight lines |
384 | 424 |
|
385 | 425 | ## Contribute
|
386 | 426 |
|
@@ -458,6 +498,8 @@ abide by its terms.
|
458 | 498 |
|
459 | 499 | [rehype-starry-night]: https://github.com/rehypejs/rehype-starry-night
|
460 | 500 |
|
| 501 | +[rehype-highlight-code-lines]: https://github.com/ipikuka/rehype-highlight-code-lines |
| 502 | +
|
461 | 503 | [typescript]: https://www.typescriptlang.org
|
462 | 504 |
|
463 | 505 | [unified]: https://github.com/unifiedjs/unified
|
|
0 commit comments