7
7
[ ![ Downloads] [ badge-downloads-image ]] [ badge-downloads-url ]
8
8
[ ![ Size] [ badge-size-image ]] [ badge-size-url ]
9
9
10
- Lightweight, robust, elegant virtual syntax highlighting using
11
- [ Prism] [ github-prism ] .
10
+ Lightweight,
11
+ robust,
12
+ and elegant virtual syntax highlighting using [ Prism] [ github-prism ] .
12
13
13
14
## Contents
14
15
@@ -40,33 +41,46 @@ Lightweight, robust, elegant virtual syntax highlighting using
40
41
This package wraps [ Prism] [ github-prism ] to output objects (ASTs) instead of a
41
42
string of HTML.
42
43
43
- Prism, through refractor, supports 270+ programming languages.
44
+ Prism,
45
+ through refractor,
46
+ supports 290+ programming languages.
44
47
Supporting all of them requires a lot of code.
45
48
That’s why there are three entry points for refractor:
46
49
47
50
<!-- count start-->
48
51
49
- * ` lib/core.js ` — 0 languages
50
- * ` lib/common.js ` (default) — 36 languages
51
52
* ` lib/all.js ` — 297 languages
53
+ * ` lib/common.js ` (default) — 36 languages
54
+ * ` lib/core.js ` — 0 languages
52
55
53
56
<!-- count end-->
54
57
55
- Bundled, minified, and gzipped, those are roughly 12.7 kB, 40 kB, and 211 kB.
58
+ Bundled,
59
+ minified,
60
+ and gzipped,
61
+ those are roughly 12.7 kB,
62
+ 40 kB,
63
+ and 211 kB.
56
64
57
65
## When should I use this?
58
66
59
67
This package is useful when you want to perform syntax highlighting in a place
60
68
where serialized HTML wouldn’t work or wouldn’t work well.
61
- For example, you can use refractor when you want to show code in a CLI by
62
- rendering to ANSI sequences, when you’re using virtual DOM frameworks (such as
63
- React or Preact) so that diffing can be performant, or when you’re working with
64
- ASTs (rehype).
65
-
66
- A different package, [ ` lowlight ` ] [ github-lowlight ] , does the same as refractor
67
- but uses [ ` highlight.js ` ] [ github-highlightjs ] instead.
68
- If you’re looking for a * really good* (but rather heavy) highlighter, try
69
- [ ` starry-night ` ] [ github-starry-night ] .
69
+ For example,
70
+ you can use refractor when you want to show code in a CLI by rendering to ANSI
71
+ sequences,
72
+ when you’re using virtual DOM frameworks
73
+ (such as React or Preact)
74
+ so that diffing can be performant,
75
+ or when you’re working with ASTs
76
+ (rehype).
77
+
78
+ A different package,
79
+ [ ` lowlight ` ] [ github-lowlight ] ,
80
+ does the same as refractor but uses [ ` highlight.js ` ] [ github-highlightjs ]
81
+ instead.
82
+ If you’re looking for a * really good* but rather heavy highlighter,
83
+ try [ ` starry-night ` ] [ github-starry-night ] .
70
84
71
85
<!-- Old name of the following section:-->
72
86
@@ -80,7 +94,8 @@ You can play with refractor on the
80
94
## Install
81
95
82
96
This package is [ ESM only] [ github-gist-esm ] .
83
- In Node.js (version 14+), install with [ npm] [ npmjs-install ] :
97
+ In Node.js (version 14+),
98
+ install with [ npm] [ npmjs-install ] :
84
99
85
100
``` sh
86
101
npm install refractor
@@ -146,7 +161,9 @@ Highlight `value` (code) as `language` (programming language).
146
161
* ` value ` (` string ` )
147
162
— code to highlight
148
163
* ` language ` (` string ` or ` Grammar ` )
149
- — programming language name, alias, or grammar.
164
+ — programming language name,
165
+ alias,
166
+ or grammar
150
167
151
168
###### Returns
152
169
@@ -184,8 +201,9 @@ Register a syntax.
184
201
###### Parameters
185
202
186
203
* ` syntax ` (` Function ` )
187
- — language function custom made for refractor, as in, the files in
188
- ` refractor/lang/*.js `
204
+ — language function custom made for refractor,
205
+ as in,
206
+ the files in ` refractor/lang/*.js `
189
207
190
208
###### Example
191
209
@@ -355,22 +373,27 @@ Yields:
355
373
## Types
356
374
357
375
This package is fully typed with [ TypeScript] [ ] .
358
- It exports the additional types ` Root ` , ` Grammar ` , and ` Syntax ` .
376
+ It exports the additional types
377
+ ` Grammar ` ,
378
+ ` Root ` ,
379
+ and ` Syntax ` .
359
380
360
381
<!-- Old name of the following section:-->
361
382
362
383
<a name =" syntaxes " ></a >
363
384
364
385
## Data
365
386
366
- If you’re using ` refractor/lib/core.js ` , no syntaxes are included.
367
- Checked syntaxes are included if you import ` refractor ` (or explicitly
368
- ` refractor/lib/common.js ` ).
387
+ If you’re using ` refractor/lib/core.js ` ,
388
+ no syntaxes are included.
389
+ Checked syntaxes are included if you import ` refractor `
390
+ (or explicitly ` refractor/lib/common.js ` ).
369
391
Unchecked syntaxes are available through ` refractor/lib/all.js ` .
370
392
You can import ` core ` or ` common ` and manually add more languages as you please.
371
393
372
- Prism operates as a singleton: once you register a language in one place, it’ll
373
- be available everywhere.
394
+ Prism operates as a singleton:
395
+ once you register a language in one place,
396
+ it’ll be available everywhere.
374
397
375
398
Only these custom built syntaxes will work with ` refractor ` because Prism’s own
376
399
syntaxes are made to work with global variables and are not importable.
@@ -679,13 +702,16 @@ syntaxes are made to work with global variables and are not importable.
679
702
680
703
## CSS
681
704
682
- ` refractor ` does not inject CSS for the syntax highlighted code (because well,
683
- refractor doesn’t have to be turned into HTML and might not run in a browser!).
684
- If you are in a browser, you can use any Prism theme.
685
- For example, to get Prism Dark from cdnjs:
705
+ ` refractor ` does not inject CSS for the syntax highlighted code.
706
+ It does not make sense: refractor doesn’t have to be turned into HTML and might
707
+ not run in a browser!
708
+ If you are in a browser,
709
+ you can use any Prism theme.
710
+ For example,
711
+ to get Prism Dark from [ ` esm.sh ` ] [ esmsh ] :
686
712
687
713
``` html
688
- <link rel =" stylesheet" href =" https://cdnjs.cloudflare.com/ajax/libs/prism/1.27 .0/themes/prism-dark.min .css" >
714
+ <link rel =" stylesheet" href =" https://esm.sh/prismjs@1.30 .0/themes/prism-dark.css" >
689
715
```
690
716
691
717
<!-- Old name of the following section:-->
@@ -695,15 +721,17 @@ For example, to get Prism Dark from cdnjs:
695
721
## Compatibility
696
722
697
723
This package is at least compatible with all maintained versions of Node.js.
698
- As of now, that is Node.js 14.14+ and 16.0+.
724
+ As of now,
725
+ that is Node.js 14+.
699
726
It also works in Deno and modern browsers.
700
727
701
728
Only the custom built syntaxes in ` refractor/lang/*.js ` will work with
702
729
` refractor ` as Prism’s own syntaxes are made to work with global variables and
703
730
are not importable.
704
731
705
- refractor also does not support Prism plugins, due to the same limitations, and
706
- that they almost exclusively deal with the DOM.
732
+ refractor also does not support Prism plugins,
733
+ due to the same limitations,
734
+ and that they almost exclusively deal with the DOM.
707
735
708
736
## Security
709
737
0 commit comments