UNPKG

37.5 kBMarkdownView Raw
1<!--lint disable no-html-->
2
3# refractor
4
5[![Build][build-badge]][build]
6[![Coverage][coverage-badge]][coverage]
7[![Downloads][downloads-badge]][downloads]
8[![Size][size-badge]][size]
9
10Lightweight, robust, elegant virtual syntax highlighting using [Prism][].
11
12## Contents
13
14* [What is this?](#what-is-this)
15* [When should I use this?](#when-should-i-use-this)
16* [Playground](#playground)
17* [Install](#install)
18* [Use](#use)
19* [API](#api)
20 * [`refractor.highlight(value, language)`](#refractorhighlightvalue-language)
21 * [`refractor.register(syntax)`](#refractorregistersyntax)
22 * [`refractor.alias(name[, alias])`](#refractoraliasname-alias)
23 * [`refractor.registered(aliasOrlanguage)`](#refractorregisteredaliasorlanguage)
24 * [`refractor.listLanguages()`](#refractorlistlanguages)
25* [Examples](#examples)
26 * [Example: serializing hast as html](#example-serializing-hast-as-html)
27 * [Example: turning hast into react nodes](#example-turning-hast-into-react-nodes)
28* [Types](#types)
29* [Data](#data)
30* [CSS](#css)
31* [Compatibility](#compatibility)
32* [Security](#security)
33* [Related](#related)
34* [Projects](#projects)
35* [Contribute](#contribute)
36
37## What is this?
38
39This package wraps [Prism][] to output objects (ASTs) instead of a string of
40HTML.
41
42Prism, through refractor, supports 270+ programming languages.
43Supporting all of them requires a lot of code.
44That’s why there are three entry points for refractor:
45
46<!--count start-->
47
48* `lib/core.js` — 0 languages
49* `lib/common.js` (default) — 36 languages
50* `lib/all.js` — 297 languages
51
52<!--count end-->
53
54Bundled, minified, and gzipped, those are roughly 12.7 kB, 40 kB, and 211 kB.
55
56## When should I use this?
57
58This package is useful when you want to perform syntax highlighting in a place
59where serialized HTML wouldn’t work or wouldn’t work well.
60For example, you can use refractor when you want to show code in a CLI by
61rendering to ANSI sequences, when you’re using virtual DOM frameworks (such as
62React or Preact) so that diffing can be performant, or when you’re working with
63ASTs (rehype).
64
65A different package, [`lowlight`][lowlight], does the same as refractor but
66uses [`highlight.js`][hljs] instead.
67If you’re looking for a *really good* (but rather heavy) highlighter, try
68[`starry-night`][starry-night].
69
70<!--Old name of the following section:-->
71
72<a name="demo"></a>
73
74## Playground
75
76You can play with refractor on the
77[interactive demo (Replit)](https://replit.com/@karlhorky/official-refractor-demo#index.js).
78
79## Install
80
81This package is [ESM only][esm].
82In Node.js (version 14.14+, 16.0+), install with [npm][]:
83
84```sh
85npm install refractor
86```
87
88In Deno with [`esm.sh`][esmsh]:
89
90```js
91import {refractor} from 'https://esm.sh/refractor@4'
92```
93
94In browsers with [`esm.sh`][esmsh]:
95
96```html
97<script type="module">
98 import {refractor} from 'https://esm.sh/refractor@4?bundle'
99</script>
100```
101
102## Use
103
104```js
105import {refractor} from 'refractor'
106
107const tree = refractor.highlight('"use strict";', 'js')
108
109console.log(tree)
110```
111
112Yields:
113
114```js
115{
116 type: 'root',
117 children: [
118 {
119 type: 'element',
120 tagName: 'span',
121 properties: {className: ['token', 'string']},
122 children: [{type: 'text', value: '"use strict"'}]
123 },
124 {
125 type: 'element',
126 tagName: 'span',
127 properties: {className: ['token', 'punctuation']},
128 children: [{type: 'text', value: ';'}]
129 }
130 ]
131}
132```
133
134## API
135
136This package exports the identifier `refractor`.
137There is no default export.
138
139### `refractor.highlight(value, language)`
140
141Highlight `value` (code) as `language` (programming language).
142
143###### Parameters
144
145* `value` (`string`)
146 — code to highlight
147* `language` (`string` or `Grammar`)
148 — programming language name, alias, or grammar.
149
150###### Returns
151
152Node representing highlighted code ([`Root`][root]).
153
154###### Example
155
156```js
157import {refractor} from 'refractor/lib/core.js'
158import css from 'refractor/lang/css.js'
159
160refractor.register(css)
161console.log(refractor.highlight('em { color: red }', 'css'))
162```
163
164Yields:
165
166```js
167{
168 type: 'root',
169 children: [
170 {type: 'element', tagName: 'span', properties: [Object], children: [Array]},
171 {type: 'text', value: ' '},
172 // …
173 {type: 'text', value: ' red '},
174 {type: 'element', tagName: 'span', properties: [Object], children: [Array]}
175 ]
176}
177```
178
179### `refractor.register(syntax)`
180
181Register a syntax.
182
183###### Parameters
184
185* `syntax` (`Function`)
186 — language function custom made for refractor, as in, the files in
187 `refractor/lang/*.js`
188
189###### Example
190
191```js
192import {refractor} from 'refractor/lib/core.js'
193import markdown from 'refractor/lang/markdown.js'
194
195refractor.register(markdown)
196
197console.log(refractor.highlight('*Emphasis*', 'markdown'))
198```
199
200Yields:
201
202```js
203{
204 type: 'root',
205 children: [
206 {type: 'element', tagName: 'span', properties: [Object], children: [Array]}
207 ]
208}
209```
210
211### `refractor.alias(name[, alias])`
212
213Register aliases for already registered languages.
214
215###### Signatures
216
217* `alias(name, alias|list)`
218* `alias(aliases)`
219
220###### Parameters
221
222* `language` (`string`)
223 — programming language [name][names]
224* `alias` (`string`)
225 — new aliases for the programming language
226* `list` (`Array<string>`)
227 — list of aliases
228* `aliases` (`Record<language, alias|list>`)
229 — map of `language`s to `alias`es or `list`s
230
231###### Example
232
233```js
234import {refractor} from 'refractor/lib/core.js'
235import markdown from 'refractor/lang/markdown.js'
236
237refractor.register(markdown)
238
239// refractor.highlight('*Emphasis*', 'mdown')
240// ^ would throw: Error: Unknown language: `mdown` is not registered
241
242refractor.alias({markdown: ['mdown', 'mkdn', 'mdwn', 'ron']})
243refractor.highlight('*Emphasis*', 'mdown')
244// ^ Works!
245```
246
247### `refractor.registered(aliasOrlanguage)`
248
249Check whether an `alias` or `language` is registered.
250
251###### Parameters
252
253* `aliasOrlanguage` (`string`)
254 — programming language name or alias
255
256###### Example
257
258```js
259import {refractor} from 'refractor/lib/core.js'
260import markdown from 'refractor/lang/markdown.js'
261
262console.log(refractor.registered('markdown')) //=> false
263
264refractor.register(markdown)
265
266console.log(refractor.registered('markdown')) //=> true
267```
268
269### `refractor.listLanguages()`
270
271List all registered languages (names and aliases).
272
273###### Returns
274
275`Array<string>`.
276
277###### Example
278
279```js
280import {refractor} from 'refractor/lib/core.js'
281import markdown from 'refractor/lang/markdown.js'
282
283console.log(refractor.listLanguages()) //=> []
284
285refractor.register(markdown)
286
287console.log(refractor.listLanguages())
288```
289
290Yields:
291
292```js
293[
294 'markup', // Note that `markup` (a lot of xml based languages) is a dep of markdown.
295 'html',
296 // …
297 'markdown',
298 'md'
299]
300```
301
302## Examples
303
304### Example: serializing hast as html
305
306hast trees as returned by refractor can be serialized with
307[`hast-util-to-html`][hast-util-to-html]:
308
309```js
310import {refractor} from 'refractor'
311import {toHtml} from 'hast-util-to-html'
312
313const tree = refractor.highlight('"use strict";', 'js')
314
315console.log(toHtml(tree))
316```
317
318Yields:
319
320```html
321<span class="token string">"use strict"</span><span class="token punctuation">;</span>
322```
323
324### Example: turning hast into react nodes
325
326hast trees as returned by refractor can be turned into React (or Preact) with
327[`hast-to-hyperscript`][hast-to-hyperscript]:
328
329```js
330import {refractor} from 'refractor'
331import {toH} from 'hast-to-hyperscript'
332import React from 'react'
333
334const tree = refractor.highlight('"use strict";', 'js')
335const react = toH(React.createElement, tree)
336
337console.log(react)
338```
339
340Yields:
341
342```js
343{
344 '$$typeof': Symbol(react.element),
345 type: 'div',
346 key: 'h-1',
347 ref: null,
348 props: { children: [ [Object], [Object] ] },
349 _owner: null,
350 _store: {}
351}
352```
353
354## Types
355
356This package is fully typed with [TypeScript][].
357It exports the additional types `Root`, `Grammar`, and `Syntax`.
358
359<!--Old name of the following section:-->
360
361<a name="syntaxes"></a>
362
363## Data
364
365If you’re using `refractor/lib/core.js`, no syntaxes are included.
366Checked syntaxes are included if you import `refractor` (or explicitly
367`refractor/lib/common.js`).
368Unchecked syntaxes are available through `refractor/lib/all.js`.
369You can import `core` or `common` and manually add more languages as you please.
370
371Prism operates as a singleton: once you register a language in one place, it’ll
372be available everywhere.
373
374Only these custom built syntaxes will work with `refractor` because Prism’s own
375syntaxes are made to work with global variables and are not importable.
376
377<!--support start-->
378
379* [x] [`arduino`](https://github.com/wooorm/refractor/blob/main/lang/arduino.js) — alias: `ino`
380* [x] [`bash`](https://github.com/wooorm/refractor/blob/main/lang/bash.js) — alias: `sh`, `shell`
381* [x] [`basic`](https://github.com/wooorm/refractor/blob/main/lang/basic.js)
382* [x] [`c`](https://github.com/wooorm/refractor/blob/main/lang/c.js)
383* [x] [`clike`](https://github.com/wooorm/refractor/blob/main/lang/clike.js)
384* [x] [`cpp`](https://github.com/wooorm/refractor/blob/main/lang/cpp.js)
385* [x] [`csharp`](https://github.com/wooorm/refractor/blob/main/lang/csharp.js) — alias: `cs`, `dotnet`
386* [x] [`css`](https://github.com/wooorm/refractor/blob/main/lang/css.js)
387* [x] [`diff`](https://github.com/wooorm/refractor/blob/main/lang/diff.js)
388* [x] [`go`](https://github.com/wooorm/refractor/blob/main/lang/go.js)
389* [x] [`ini`](https://github.com/wooorm/refractor/blob/main/lang/ini.js)
390* [x] [`java`](https://github.com/wooorm/refractor/blob/main/lang/java.js)
391* [x] [`javascript`](https://github.com/wooorm/refractor/blob/main/lang/javascript.js) — alias: `js`
392* [x] [`json`](https://github.com/wooorm/refractor/blob/main/lang/json.js) — alias: `webmanifest`
393* [x] [`kotlin`](https://github.com/wooorm/refractor/blob/main/lang/kotlin.js) — alias: `kt`, `kts`
394* [x] [`less`](https://github.com/wooorm/refractor/blob/main/lang/less.js)
395* [x] [`lua`](https://github.com/wooorm/refractor/blob/main/lang/lua.js)
396* [x] [`makefile`](https://github.com/wooorm/refractor/blob/main/lang/makefile.js)
397* [x] [`markdown`](https://github.com/wooorm/refractor/blob/main/lang/markdown.js) — alias: `md`
398* [x] [`markup`](https://github.com/wooorm/refractor/blob/main/lang/markup.js) — alias: `atom`, `html`, `mathml`, `rss`, `ssml`, `svg`, `xml`
399* [x] [`markup-templating`](https://github.com/wooorm/refractor/blob/main/lang/markup-templating.js)
400* [x] [`objectivec`](https://github.com/wooorm/refractor/blob/main/lang/objectivec.js) — alias: `objc`
401* [x] [`perl`](https://github.com/wooorm/refractor/blob/main/lang/perl.js)
402* [x] [`php`](https://github.com/wooorm/refractor/blob/main/lang/php.js)
403* [x] [`python`](https://github.com/wooorm/refractor/blob/main/lang/python.js) — alias: `py`
404* [x] [`r`](https://github.com/wooorm/refractor/blob/main/lang/r.js)
405* [x] [`regex`](https://github.com/wooorm/refractor/blob/main/lang/regex.js)
406* [x] [`ruby`](https://github.com/wooorm/refractor/blob/main/lang/ruby.js) — alias: `rb`
407* [x] [`rust`](https://github.com/wooorm/refractor/blob/main/lang/rust.js)
408* [x] [`sass`](https://github.com/wooorm/refractor/blob/main/lang/sass.js)
409* [x] [`scss`](https://github.com/wooorm/refractor/blob/main/lang/scss.js)
410* [x] [`sql`](https://github.com/wooorm/refractor/blob/main/lang/sql.js)
411* [x] [`swift`](https://github.com/wooorm/refractor/blob/main/lang/swift.js)
412* [x] [`typescript`](https://github.com/wooorm/refractor/blob/main/lang/typescript.js) — alias: `ts`
413* [x] [`vbnet`](https://github.com/wooorm/refractor/blob/main/lang/vbnet.js)
414* [x] [`yaml`](https://github.com/wooorm/refractor/blob/main/lang/yaml.js) — alias: `yml`
415* [ ] [`abap`](https://github.com/wooorm/refractor/blob/main/lang/abap.js)
416* [ ] [`abnf`](https://github.com/wooorm/refractor/blob/main/lang/abnf.js)
417* [ ] [`actionscript`](https://github.com/wooorm/refractor/blob/main/lang/actionscript.js)
418* [ ] [`ada`](https://github.com/wooorm/refractor/blob/main/lang/ada.js)
419* [ ] [`agda`](https://github.com/wooorm/refractor/blob/main/lang/agda.js)
420* [ ] [`al`](https://github.com/wooorm/refractor/blob/main/lang/al.js)
421* [ ] [`antlr4`](https://github.com/wooorm/refractor/blob/main/lang/antlr4.js) — alias: `g4`
422* [ ] [`apacheconf`](https://github.com/wooorm/refractor/blob/main/lang/apacheconf.js)
423* [ ] [`apex`](https://github.com/wooorm/refractor/blob/main/lang/apex.js)
424* [ ] [`apl`](https://github.com/wooorm/refractor/blob/main/lang/apl.js)
425* [ ] [`applescript`](https://github.com/wooorm/refractor/blob/main/lang/applescript.js)
426* [ ] [`aql`](https://github.com/wooorm/refractor/blob/main/lang/aql.js)
427* [ ] [`arff`](https://github.com/wooorm/refractor/blob/main/lang/arff.js)
428* [ ] [`armasm`](https://github.com/wooorm/refractor/blob/main/lang/armasm.js) — alias: `arm-asm`
429* [ ] [`arturo`](https://github.com/wooorm/refractor/blob/main/lang/arturo.js) — alias: `art`
430* [ ] [`asciidoc`](https://github.com/wooorm/refractor/blob/main/lang/asciidoc.js) — alias: `adoc`
431* [ ] [`asm6502`](https://github.com/wooorm/refractor/blob/main/lang/asm6502.js)
432* [ ] [`asmatmel`](https://github.com/wooorm/refractor/blob/main/lang/asmatmel.js)
433* [ ] [`aspnet`](https://github.com/wooorm/refractor/blob/main/lang/aspnet.js)
434* [ ] [`autohotkey`](https://github.com/wooorm/refractor/blob/main/lang/autohotkey.js)
435* [ ] [`autoit`](https://github.com/wooorm/refractor/blob/main/lang/autoit.js)
436* [ ] [`avisynth`](https://github.com/wooorm/refractor/blob/main/lang/avisynth.js) — alias: `avs`
437* [ ] [`avro-idl`](https://github.com/wooorm/refractor/blob/main/lang/avro-idl.js) — alias: `avdl`
438* [ ] [`awk`](https://github.com/wooorm/refractor/blob/main/lang/awk.js) — alias: `gawk`
439* [ ] [`batch`](https://github.com/wooorm/refractor/blob/main/lang/batch.js)
440* [ ] [`bbcode`](https://github.com/wooorm/refractor/blob/main/lang/bbcode.js) — alias: `shortcode`
441* [ ] [`bbj`](https://github.com/wooorm/refractor/blob/main/lang/bbj.js)
442* [ ] [`bicep`](https://github.com/wooorm/refractor/blob/main/lang/bicep.js)
443* [ ] [`birb`](https://github.com/wooorm/refractor/blob/main/lang/birb.js)
444* [ ] [`bison`](https://github.com/wooorm/refractor/blob/main/lang/bison.js)
445* [ ] [`bnf`](https://github.com/wooorm/refractor/blob/main/lang/bnf.js) — alias: `rbnf`
446* [ ] [`bqn`](https://github.com/wooorm/refractor/blob/main/lang/bqn.js)
447* [ ] [`brainfuck`](https://github.com/wooorm/refractor/blob/main/lang/brainfuck.js)
448* [ ] [`brightscript`](https://github.com/wooorm/refractor/blob/main/lang/brightscript.js)
449* [ ] [`bro`](https://github.com/wooorm/refractor/blob/main/lang/bro.js)
450* [ ] [`bsl`](https://github.com/wooorm/refractor/blob/main/lang/bsl.js) — alias: `oscript`
451* [ ] [`cfscript`](https://github.com/wooorm/refractor/blob/main/lang/cfscript.js) — alias: `cfc`
452* [ ] [`chaiscript`](https://github.com/wooorm/refractor/blob/main/lang/chaiscript.js)
453* [ ] [`cil`](https://github.com/wooorm/refractor/blob/main/lang/cil.js)
454* [ ] [`cilkc`](https://github.com/wooorm/refractor/blob/main/lang/cilkc.js) — alias: `cilk-c`
455* [ ] [`cilkcpp`](https://github.com/wooorm/refractor/blob/main/lang/cilkcpp.js) — alias: `cilk`, `cilk-cpp`
456* [ ] [`clojure`](https://github.com/wooorm/refractor/blob/main/lang/clojure.js)
457* [ ] [`cmake`](https://github.com/wooorm/refractor/blob/main/lang/cmake.js)
458* [ ] [`cobol`](https://github.com/wooorm/refractor/blob/main/lang/cobol.js)
459* [ ] [`coffeescript`](https://github.com/wooorm/refractor/blob/main/lang/coffeescript.js) — alias: `coffee`
460* [ ] [`concurnas`](https://github.com/wooorm/refractor/blob/main/lang/concurnas.js) — alias: `conc`
461* [ ] [`cooklang`](https://github.com/wooorm/refractor/blob/main/lang/cooklang.js)
462* [ ] [`coq`](https://github.com/wooorm/refractor/blob/main/lang/coq.js)
463* [ ] [`crystal`](https://github.com/wooorm/refractor/blob/main/lang/crystal.js)
464* [ ] [`cshtml`](https://github.com/wooorm/refractor/blob/main/lang/cshtml.js) — alias: `razor`
465* [ ] [`csp`](https://github.com/wooorm/refractor/blob/main/lang/csp.js)
466* [ ] [`css-extras`](https://github.com/wooorm/refractor/blob/main/lang/css-extras.js)
467* [ ] [`csv`](https://github.com/wooorm/refractor/blob/main/lang/csv.js)
468* [ ] [`cue`](https://github.com/wooorm/refractor/blob/main/lang/cue.js)
469* [ ] [`cypher`](https://github.com/wooorm/refractor/blob/main/lang/cypher.js)
470* [ ] [`d`](https://github.com/wooorm/refractor/blob/main/lang/d.js)
471* [ ] [`dart`](https://github.com/wooorm/refractor/blob/main/lang/dart.js)
472* [ ] [`dataweave`](https://github.com/wooorm/refractor/blob/main/lang/dataweave.js)
473* [ ] [`dax`](https://github.com/wooorm/refractor/blob/main/lang/dax.js)
474* [ ] [`dhall`](https://github.com/wooorm/refractor/blob/main/lang/dhall.js)
475* [ ] [`django`](https://github.com/wooorm/refractor/blob/main/lang/django.js) — alias: `jinja2`
476* [ ] [`dns-zone-file`](https://github.com/wooorm/refractor/blob/main/lang/dns-zone-file.js) — alias: `dns-zone`
477* [ ] [`docker`](https://github.com/wooorm/refractor/blob/main/lang/docker.js) — alias: `dockerfile`
478* [ ] [`dot`](https://github.com/wooorm/refractor/blob/main/lang/dot.js) — alias: `gv`
479* [ ] [`ebnf`](https://github.com/wooorm/refractor/blob/main/lang/ebnf.js)
480* [ ] [`editorconfig`](https://github.com/wooorm/refractor/blob/main/lang/editorconfig.js)
481* [ ] [`eiffel`](https://github.com/wooorm/refractor/blob/main/lang/eiffel.js)
482* [ ] [`ejs`](https://github.com/wooorm/refractor/blob/main/lang/ejs.js) — alias: `eta`
483* [ ] [`elixir`](https://github.com/wooorm/refractor/blob/main/lang/elixir.js)
484* [ ] [`elm`](https://github.com/wooorm/refractor/blob/main/lang/elm.js)
485* [ ] [`erb`](https://github.com/wooorm/refractor/blob/main/lang/erb.js)
486* [ ] [`erlang`](https://github.com/wooorm/refractor/blob/main/lang/erlang.js)
487* [ ] [`etlua`](https://github.com/wooorm/refractor/blob/main/lang/etlua.js)
488* [ ] [`excel-formula`](https://github.com/wooorm/refractor/blob/main/lang/excel-formula.js) — alias: `xls`, `xlsx`
489* [ ] [`factor`](https://github.com/wooorm/refractor/blob/main/lang/factor.js)
490* [ ] [`false`](https://github.com/wooorm/refractor/blob/main/lang/false.js)
491* [ ] [`firestore-security-rules`](https://github.com/wooorm/refractor/blob/main/lang/firestore-security-rules.js)
492* [ ] [`flow`](https://github.com/wooorm/refractor/blob/main/lang/flow.js)
493* [ ] [`fortran`](https://github.com/wooorm/refractor/blob/main/lang/fortran.js)
494* [ ] [`fsharp`](https://github.com/wooorm/refractor/blob/main/lang/fsharp.js)
495* [ ] [`ftl`](https://github.com/wooorm/refractor/blob/main/lang/ftl.js)
496* [ ] [`gap`](https://github.com/wooorm/refractor/blob/main/lang/gap.js)
497* [ ] [`gcode`](https://github.com/wooorm/refractor/blob/main/lang/gcode.js)
498* [ ] [`gdscript`](https://github.com/wooorm/refractor/blob/main/lang/gdscript.js)
499* [ ] [`gedcom`](https://github.com/wooorm/refractor/blob/main/lang/gedcom.js)
500* [ ] [`gettext`](https://github.com/wooorm/refractor/blob/main/lang/gettext.js) — alias: `po`
501* [ ] [`gherkin`](https://github.com/wooorm/refractor/blob/main/lang/gherkin.js)
502* [ ] [`git`](https://github.com/wooorm/refractor/blob/main/lang/git.js)
503* [ ] [`glsl`](https://github.com/wooorm/refractor/blob/main/lang/glsl.js)
504* [ ] [`gml`](https://github.com/wooorm/refractor/blob/main/lang/gml.js) — alias: `gamemakerlanguage`
505* [ ] [`gn`](https://github.com/wooorm/refractor/blob/main/lang/gn.js) — alias: `gni`
506* [ ] [`go-module`](https://github.com/wooorm/refractor/blob/main/lang/go-module.js) — alias: `go-mod`
507* [ ] [`gradle`](https://github.com/wooorm/refractor/blob/main/lang/gradle.js)
508* [ ] [`graphql`](https://github.com/wooorm/refractor/blob/main/lang/graphql.js)
509* [ ] [`groovy`](https://github.com/wooorm/refractor/blob/main/lang/groovy.js)
510* [ ] [`haml`](https://github.com/wooorm/refractor/blob/main/lang/haml.js)
511* [ ] [`handlebars`](https://github.com/wooorm/refractor/blob/main/lang/handlebars.js) — alias: `hbs`, `mustache`
512* [ ] [`haskell`](https://github.com/wooorm/refractor/blob/main/lang/haskell.js) — alias: `hs`
513* [ ] [`haxe`](https://github.com/wooorm/refractor/blob/main/lang/haxe.js)
514* [ ] [`hcl`](https://github.com/wooorm/refractor/blob/main/lang/hcl.js)
515* [ ] [`hlsl`](https://github.com/wooorm/refractor/blob/main/lang/hlsl.js)
516* [ ] [`hoon`](https://github.com/wooorm/refractor/blob/main/lang/hoon.js)
517* [ ] [`hpkp`](https://github.com/wooorm/refractor/blob/main/lang/hpkp.js)
518* [ ] [`hsts`](https://github.com/wooorm/refractor/blob/main/lang/hsts.js)
519* [ ] [`http`](https://github.com/wooorm/refractor/blob/main/lang/http.js)
520* [ ] [`ichigojam`](https://github.com/wooorm/refractor/blob/main/lang/ichigojam.js)
521* [ ] [`icon`](https://github.com/wooorm/refractor/blob/main/lang/icon.js)
522* [ ] [`icu-message-format`](https://github.com/wooorm/refractor/blob/main/lang/icu-message-format.js)
523* [ ] [`idris`](https://github.com/wooorm/refractor/blob/main/lang/idris.js) — alias: `idr`
524* [ ] [`iecst`](https://github.com/wooorm/refractor/blob/main/lang/iecst.js)
525* [ ] [`ignore`](https://github.com/wooorm/refractor/blob/main/lang/ignore.js) — alias: `gitignore`, `hgignore`, `npmignore`
526* [ ] [`inform7`](https://github.com/wooorm/refractor/blob/main/lang/inform7.js)
527* [ ] [`io`](https://github.com/wooorm/refractor/blob/main/lang/io.js)
528* [ ] [`j`](https://github.com/wooorm/refractor/blob/main/lang/j.js)
529* [ ] [`javadoc`](https://github.com/wooorm/refractor/blob/main/lang/javadoc.js)
530* [ ] [`javadoclike`](https://github.com/wooorm/refractor/blob/main/lang/javadoclike.js)
531* [ ] [`javastacktrace`](https://github.com/wooorm/refractor/blob/main/lang/javastacktrace.js)
532* [ ] [`jexl`](https://github.com/wooorm/refractor/blob/main/lang/jexl.js)
533* [ ] [`jolie`](https://github.com/wooorm/refractor/blob/main/lang/jolie.js)
534* [ ] [`jq`](https://github.com/wooorm/refractor/blob/main/lang/jq.js)
535* [ ] [`js-extras`](https://github.com/wooorm/refractor/blob/main/lang/js-extras.js)
536* [ ] [`js-templates`](https://github.com/wooorm/refractor/blob/main/lang/js-templates.js)
537* [ ] [`jsdoc`](https://github.com/wooorm/refractor/blob/main/lang/jsdoc.js)
538* [ ] [`json5`](https://github.com/wooorm/refractor/blob/main/lang/json5.js)
539* [ ] [`jsonp`](https://github.com/wooorm/refractor/blob/main/lang/jsonp.js)
540* [ ] [`jsstacktrace`](https://github.com/wooorm/refractor/blob/main/lang/jsstacktrace.js)
541* [ ] [`jsx`](https://github.com/wooorm/refractor/blob/main/lang/jsx.js)
542* [ ] [`julia`](https://github.com/wooorm/refractor/blob/main/lang/julia.js)
543* [ ] [`keepalived`](https://github.com/wooorm/refractor/blob/main/lang/keepalived.js)
544* [ ] [`keyman`](https://github.com/wooorm/refractor/blob/main/lang/keyman.js)
545* [ ] [`kumir`](https://github.com/wooorm/refractor/blob/main/lang/kumir.js) — alias: `kum`
546* [ ] [`kusto`](https://github.com/wooorm/refractor/blob/main/lang/kusto.js)
547* [ ] [`latex`](https://github.com/wooorm/refractor/blob/main/lang/latex.js) — alias: `context`, `tex`
548* [ ] [`latte`](https://github.com/wooorm/refractor/blob/main/lang/latte.js)
549* [ ] [`lilypond`](https://github.com/wooorm/refractor/blob/main/lang/lilypond.js) — alias: `ly`
550* [ ] [`linker-script`](https://github.com/wooorm/refractor/blob/main/lang/linker-script.js) — alias: `ld`
551* [ ] [`liquid`](https://github.com/wooorm/refractor/blob/main/lang/liquid.js)
552* [ ] [`lisp`](https://github.com/wooorm/refractor/blob/main/lang/lisp.js) — alias: `elisp`, `emacs`, `emacs-lisp`
553* [ ] [`livescript`](https://github.com/wooorm/refractor/blob/main/lang/livescript.js)
554* [ ] [`llvm`](https://github.com/wooorm/refractor/blob/main/lang/llvm.js)
555* [ ] [`log`](https://github.com/wooorm/refractor/blob/main/lang/log.js)
556* [ ] [`lolcode`](https://github.com/wooorm/refractor/blob/main/lang/lolcode.js)
557* [ ] [`magma`](https://github.com/wooorm/refractor/blob/main/lang/magma.js)
558* [ ] [`mata`](https://github.com/wooorm/refractor/blob/main/lang/mata.js)
559* [ ] [`matlab`](https://github.com/wooorm/refractor/blob/main/lang/matlab.js)
560* [ ] [`maxscript`](https://github.com/wooorm/refractor/blob/main/lang/maxscript.js)
561* [ ] [`mel`](https://github.com/wooorm/refractor/blob/main/lang/mel.js)
562* [ ] [`mermaid`](https://github.com/wooorm/refractor/blob/main/lang/mermaid.js)
563* [ ] [`metafont`](https://github.com/wooorm/refractor/blob/main/lang/metafont.js)
564* [ ] [`mizar`](https://github.com/wooorm/refractor/blob/main/lang/mizar.js)
565* [ ] [`mongodb`](https://github.com/wooorm/refractor/blob/main/lang/mongodb.js)
566* [ ] [`monkey`](https://github.com/wooorm/refractor/blob/main/lang/monkey.js)
567* [ ] [`moonscript`](https://github.com/wooorm/refractor/blob/main/lang/moonscript.js) — alias: `moon`
568* [ ] [`n1ql`](https://github.com/wooorm/refractor/blob/main/lang/n1ql.js)
569* [ ] [`n4js`](https://github.com/wooorm/refractor/blob/main/lang/n4js.js) — alias: `n4jsd`
570* [ ] [`nand2tetris-hdl`](https://github.com/wooorm/refractor/blob/main/lang/nand2tetris-hdl.js)
571* [ ] [`naniscript`](https://github.com/wooorm/refractor/blob/main/lang/naniscript.js) — alias: `nani`
572* [ ] [`nasm`](https://github.com/wooorm/refractor/blob/main/lang/nasm.js)
573* [ ] [`neon`](https://github.com/wooorm/refractor/blob/main/lang/neon.js)
574* [ ] [`nevod`](https://github.com/wooorm/refractor/blob/main/lang/nevod.js)
575* [ ] [`nginx`](https://github.com/wooorm/refractor/blob/main/lang/nginx.js)
576* [ ] [`nim`](https://github.com/wooorm/refractor/blob/main/lang/nim.js)
577* [ ] [`nix`](https://github.com/wooorm/refractor/blob/main/lang/nix.js)
578* [ ] [`nsis`](https://github.com/wooorm/refractor/blob/main/lang/nsis.js)
579* [ ] [`ocaml`](https://github.com/wooorm/refractor/blob/main/lang/ocaml.js)
580* [ ] [`odin`](https://github.com/wooorm/refractor/blob/main/lang/odin.js)
581* [ ] [`opencl`](https://github.com/wooorm/refractor/blob/main/lang/opencl.js)
582* [ ] [`openqasm`](https://github.com/wooorm/refractor/blob/main/lang/openqasm.js) — alias: `qasm`
583* [ ] [`oz`](https://github.com/wooorm/refractor/blob/main/lang/oz.js)
584* [ ] [`parigp`](https://github.com/wooorm/refractor/blob/main/lang/parigp.js)
585* [ ] [`parser`](https://github.com/wooorm/refractor/blob/main/lang/parser.js)
586* [ ] [`pascal`](https://github.com/wooorm/refractor/blob/main/lang/pascal.js) — alias: `objectpascal`
587* [ ] [`pascaligo`](https://github.com/wooorm/refractor/blob/main/lang/pascaligo.js)
588* [ ] [`pcaxis`](https://github.com/wooorm/refractor/blob/main/lang/pcaxis.js) — alias: `px`
589* [ ] [`peoplecode`](https://github.com/wooorm/refractor/blob/main/lang/peoplecode.js) — alias: `pcode`
590* [ ] [`php-extras`](https://github.com/wooorm/refractor/blob/main/lang/php-extras.js)
591* [ ] [`phpdoc`](https://github.com/wooorm/refractor/blob/main/lang/phpdoc.js)
592* [ ] [`plant-uml`](https://github.com/wooorm/refractor/blob/main/lang/plant-uml.js) — alias: `plantuml`
593* [ ] [`plsql`](https://github.com/wooorm/refractor/blob/main/lang/plsql.js)
594* [ ] [`powerquery`](https://github.com/wooorm/refractor/blob/main/lang/powerquery.js) — alias: `mscript`, `pq`
595* [ ] [`powershell`](https://github.com/wooorm/refractor/blob/main/lang/powershell.js)
596* [ ] [`processing`](https://github.com/wooorm/refractor/blob/main/lang/processing.js)
597* [ ] [`prolog`](https://github.com/wooorm/refractor/blob/main/lang/prolog.js)
598* [ ] [`promql`](https://github.com/wooorm/refractor/blob/main/lang/promql.js)
599* [ ] [`properties`](https://github.com/wooorm/refractor/blob/main/lang/properties.js)
600* [ ] [`protobuf`](https://github.com/wooorm/refractor/blob/main/lang/protobuf.js)
601* [ ] [`psl`](https://github.com/wooorm/refractor/blob/main/lang/psl.js)
602* [ ] [`pug`](https://github.com/wooorm/refractor/blob/main/lang/pug.js)
603* [ ] [`puppet`](https://github.com/wooorm/refractor/blob/main/lang/puppet.js)
604* [ ] [`pure`](https://github.com/wooorm/refractor/blob/main/lang/pure.js)
605* [ ] [`purebasic`](https://github.com/wooorm/refractor/blob/main/lang/purebasic.js) — alias: `pbfasm`
606* [ ] [`purescript`](https://github.com/wooorm/refractor/blob/main/lang/purescript.js) — alias: `purs`
607* [ ] [`q`](https://github.com/wooorm/refractor/blob/main/lang/q.js)
608* [ ] [`qml`](https://github.com/wooorm/refractor/blob/main/lang/qml.js)
609* [ ] [`qore`](https://github.com/wooorm/refractor/blob/main/lang/qore.js)
610* [ ] [`qsharp`](https://github.com/wooorm/refractor/blob/main/lang/qsharp.js) — alias: `qs`
611* [ ] [`racket`](https://github.com/wooorm/refractor/blob/main/lang/racket.js) — alias: `rkt`
612* [ ] [`reason`](https://github.com/wooorm/refractor/blob/main/lang/reason.js)
613* [ ] [`rego`](https://github.com/wooorm/refractor/blob/main/lang/rego.js)
614* [ ] [`renpy`](https://github.com/wooorm/refractor/blob/main/lang/renpy.js) — alias: `rpy`
615* [ ] [`rescript`](https://github.com/wooorm/refractor/blob/main/lang/rescript.js) — alias: `res`
616* [ ] [`rest`](https://github.com/wooorm/refractor/blob/main/lang/rest.js)
617* [ ] [`rip`](https://github.com/wooorm/refractor/blob/main/lang/rip.js)
618* [ ] [`roboconf`](https://github.com/wooorm/refractor/blob/main/lang/roboconf.js)
619* [ ] [`robotframework`](https://github.com/wooorm/refractor/blob/main/lang/robotframework.js) — alias: `robot`
620* [ ] [`sas`](https://github.com/wooorm/refractor/blob/main/lang/sas.js)
621* [ ] [`scala`](https://github.com/wooorm/refractor/blob/main/lang/scala.js)
622* [ ] [`scheme`](https://github.com/wooorm/refractor/blob/main/lang/scheme.js)
623* [ ] [`shell-session`](https://github.com/wooorm/refractor/blob/main/lang/shell-session.js) — alias: `sh-session`, `shellsession`
624* [ ] [`smali`](https://github.com/wooorm/refractor/blob/main/lang/smali.js)
625* [ ] [`smalltalk`](https://github.com/wooorm/refractor/blob/main/lang/smalltalk.js)
626* [ ] [`smarty`](https://github.com/wooorm/refractor/blob/main/lang/smarty.js)
627* [ ] [`sml`](https://github.com/wooorm/refractor/blob/main/lang/sml.js) — alias: `smlnj`
628* [ ] [`solidity`](https://github.com/wooorm/refractor/blob/main/lang/solidity.js) — alias: `sol`
629* [ ] [`solution-file`](https://github.com/wooorm/refractor/blob/main/lang/solution-file.js) — alias: `sln`
630* [ ] [`soy`](https://github.com/wooorm/refractor/blob/main/lang/soy.js)
631* [ ] [`sparql`](https://github.com/wooorm/refractor/blob/main/lang/sparql.js) — alias: `rq`
632* [ ] [`splunk-spl`](https://github.com/wooorm/refractor/blob/main/lang/splunk-spl.js)
633* [ ] [`sqf`](https://github.com/wooorm/refractor/blob/main/lang/sqf.js)
634* [ ] [`squirrel`](https://github.com/wooorm/refractor/blob/main/lang/squirrel.js)
635* [ ] [`stan`](https://github.com/wooorm/refractor/blob/main/lang/stan.js)
636* [ ] [`stata`](https://github.com/wooorm/refractor/blob/main/lang/stata.js)
637* [ ] [`stylus`](https://github.com/wooorm/refractor/blob/main/lang/stylus.js)
638* [ ] [`supercollider`](https://github.com/wooorm/refractor/blob/main/lang/supercollider.js) — alias: `sclang`
639* [ ] [`systemd`](https://github.com/wooorm/refractor/blob/main/lang/systemd.js)
640* [ ] [`t4-cs`](https://github.com/wooorm/refractor/blob/main/lang/t4-cs.js) — alias: `t4`
641* [ ] [`t4-templating`](https://github.com/wooorm/refractor/blob/main/lang/t4-templating.js)
642* [ ] [`t4-vb`](https://github.com/wooorm/refractor/blob/main/lang/t4-vb.js)
643* [ ] [`tap`](https://github.com/wooorm/refractor/blob/main/lang/tap.js)
644* [ ] [`tcl`](https://github.com/wooorm/refractor/blob/main/lang/tcl.js)
645* [ ] [`textile`](https://github.com/wooorm/refractor/blob/main/lang/textile.js)
646* [ ] [`toml`](https://github.com/wooorm/refractor/blob/main/lang/toml.js)
647* [ ] [`tremor`](https://github.com/wooorm/refractor/blob/main/lang/tremor.js) — alias: `trickle`, `troy`
648* [ ] [`tsx`](https://github.com/wooorm/refractor/blob/main/lang/tsx.js)
649* [ ] [`tt2`](https://github.com/wooorm/refractor/blob/main/lang/tt2.js)
650* [ ] [`turtle`](https://github.com/wooorm/refractor/blob/main/lang/turtle.js) — alias: `trig`
651* [ ] [`twig`](https://github.com/wooorm/refractor/blob/main/lang/twig.js)
652* [ ] [`typoscript`](https://github.com/wooorm/refractor/blob/main/lang/typoscript.js) — alias: `tsconfig`
653* [ ] [`unrealscript`](https://github.com/wooorm/refractor/blob/main/lang/unrealscript.js) — alias: `uc`, `uscript`
654* [ ] [`uorazor`](https://github.com/wooorm/refractor/blob/main/lang/uorazor.js)
655* [ ] [`uri`](https://github.com/wooorm/refractor/blob/main/lang/uri.js) — alias: `url`
656* [ ] [`v`](https://github.com/wooorm/refractor/blob/main/lang/v.js)
657* [ ] [`vala`](https://github.com/wooorm/refractor/blob/main/lang/vala.js)
658* [ ] [`velocity`](https://github.com/wooorm/refractor/blob/main/lang/velocity.js)
659* [ ] [`verilog`](https://github.com/wooorm/refractor/blob/main/lang/verilog.js)
660* [ ] [`vhdl`](https://github.com/wooorm/refractor/blob/main/lang/vhdl.js)
661* [ ] [`vim`](https://github.com/wooorm/refractor/blob/main/lang/vim.js)
662* [ ] [`visual-basic`](https://github.com/wooorm/refractor/blob/main/lang/visual-basic.js) — alias: `vb`, `vba`
663* [ ] [`warpscript`](https://github.com/wooorm/refractor/blob/main/lang/warpscript.js)
664* [ ] [`wasm`](https://github.com/wooorm/refractor/blob/main/lang/wasm.js)
665* [ ] [`web-idl`](https://github.com/wooorm/refractor/blob/main/lang/web-idl.js) — alias: `webidl`
666* [ ] [`wgsl`](https://github.com/wooorm/refractor/blob/main/lang/wgsl.js)
667* [ ] [`wiki`](https://github.com/wooorm/refractor/blob/main/lang/wiki.js)
668* [ ] [`wolfram`](https://github.com/wooorm/refractor/blob/main/lang/wolfram.js) — alias: `mathematica`, `nb`, `wl`
669* [ ] [`wren`](https://github.com/wooorm/refractor/blob/main/lang/wren.js)
670* [ ] [`xeora`](https://github.com/wooorm/refractor/blob/main/lang/xeora.js) — alias: `xeoracube`
671* [ ] [`xml-doc`](https://github.com/wooorm/refractor/blob/main/lang/xml-doc.js)
672* [ ] [`xojo`](https://github.com/wooorm/refractor/blob/main/lang/xojo.js)
673* [ ] [`xquery`](https://github.com/wooorm/refractor/blob/main/lang/xquery.js)
674* [ ] [`yang`](https://github.com/wooorm/refractor/blob/main/lang/yang.js)
675* [ ] [`zig`](https://github.com/wooorm/refractor/blob/main/lang/zig.js)
676
677<!--support end-->
678
679## CSS
680
681`refractor` does not inject CSS for the syntax highlighted code (because well,
682refractor doesn’t have to be turned into HTML and might not run in a browser!).
683If you are in a browser, you can use any Prism theme.
684For example, to get Prism Dark from cdnjs:
685
686```html
687<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.27.0/themes/prism-dark.min.css">
688```
689
690<!--Old name of the following section:-->
691
692<a name="plugins"></a>
693
694## Compatibility
695
696This package is at least compatible with all maintained versions of Node.js.
697As of now, that is Node.js 14.14+ and 16.0+.
698It also works in Deno and modern browsers.
699
700Only the custom built syntaxes in `refractor/lang/*.js` will work with
701`refractor` as Prism’s own syntaxes are made to work with global variables and
702are not importable.
703
704refractor also does not support Prism plugins, due to the same limitations, and
705that they almost exclusively deal with the DOM.
706
707## Security
708
709This package is safe.
710
711## Related
712
713* [`lowlight`][lowlight]
714 — the same as refractor but with [`highlight.js`][hljs]
715* [`starry-night`][starry-night]
716 — similar but like GitHub and really good
717
718## Projects
719
720* [`react-syntax-highlighter`](https://github.com/react-syntax-highlighter/react-syntax-highlighter)
721 — [React][] component for syntax highlighting
722* [`@mapbox/rehype-prism`](https://github.com/mapbox/rehype-prism)
723 — [**rehype**][rehype] plugin to highlight code
724 blocks
725* [`react-refractor`](https://github.com/rexxars/react-refractor)
726 — syntax highlighter for [React][]
727
728## Contribute
729
730Yes please!
731See [How to Contribute to Open Source][contribute].
732
733## License
734
735[MIT][license] © [Titus Wormer][author]
736
737<!-- Definitions -->
738
739[build-badge]: https://github.com/wooorm/refractor/workflows/main/badge.svg
740
741[build]: https://github.com/wooorm/refractor/actions
742
743[coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/refractor.svg
744
745[coverage]: https://codecov.io/github/wooorm/refractor
746
747[downloads-badge]: https://img.shields.io/npm/dm/refractor.svg
748
749[downloads]: https://www.npmjs.com/package/refractor
750
751[size-badge]: https://img.shields.io/bundlephobia/minzip/refractor.svg
752
753[size]: https://bundlephobia.com/result?p=refractor
754
755[npm]: https://docs.npmjs.com/cli/install
756
757[esmsh]: https://esm.sh
758
759[license]: license
760
761[author]: https://wooorm.com
762
763[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
764
765[typescript]: https://www.typescriptlang.org
766
767[contribute]: https://opensource.guide/how-to-contribute/
768
769[rehype]: https://github.com/rehypejs/rehype
770
771[names]: https://prismjs.com/#languages-list
772
773[react]: https://facebook.github.io/react/
774
775[prism]: https://github.com/PrismJS/prism
776
777[lowlight]: https://github.com/wooorm/lowlight
778
779[hljs]: https://github.com/highlightjs/highlight.js
780
781[starry-night]: https://github.com/wooorm/starry-night
782
783[root]: https://github.com/syntax-tree/hast#root
784
785[hast-util-to-html]: https://github.com/syntax-tree/hast-util-to-html
786
787[hast-to-hyperscript]: https://github.com/syntax-tree/hast-to-hyperscript