Skip to content

Commit 67eeb3a

Browse files
wooormtalatkuyuk
andcommitted
Add example, links to rehype-highlight-code-lines
Closes GH-29. Closes GH-32. Co-authored-by: Talat Küyük <[email protected]>
1 parent 3c2d9e0 commit 67eeb3a

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

readme.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* [Example: registering](#example-registering)
2626
* [Example: aliases](#example-aliases)
2727
* [Example: sanitation](#example-sanitation)
28+
* [Example: line numbering and highlighting](#example-line-numbering-and-highlighting)
2829
* [Types](#types)
2930
* [Compatibility](#compatibility)
3031
* [Security](#security)
@@ -349,6 +350,43 @@ const file = await unified()
349350
console.log(String(file))
350351
```
351352
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+
352390
## Types
353391
354392
This package is fully typed with [TypeScript][].
@@ -381,6 +419,8 @@ When in doubt, use [`rehype-sanitize`][rehype-sanitize].
381419
— add metadata to the head of a document
382420
* [`rehype-document`](https://github.com/rehypejs/rehype-document)
383421
— wrap a fragment in a document
422+
* [`rehype-highlight-code-lines`][rehype-highlight-code-lines]
423+
— add line numbers and highlight lines
384424
385425
## Contribute
386426
@@ -458,6 +498,8 @@ abide by its terms.
458498
459499
[rehype-starry-night]: https://github.com/rehypejs/rehype-starry-night
460500
501+
[rehype-highlight-code-lines]: https://github.com/ipikuka/rehype-highlight-code-lines
502+
461503
[typescript]: https://www.typescriptlang.org
462504
463505
[unified]: https://github.com/unifiedjs/unified

0 commit comments

Comments
 (0)