This package wraps Prism to output objects (ASTs) instead of a
string of HTML.
Prism,
through refractor,
supports 290+ programming languages.
Supporting all of them requires a lot of code.
That’s why there are three entry points for refractor:
refractor/all — 297 languages
refractor/core — 0 languages
refractor (default) — 36 common languages
Bundled,
minified,
and gzipped,
those are roughly 12.7 kB (core),
40 kB (default),
and 211 kB (all).
When should I use this?
This package is useful when you want to perform syntax highlighting in a place
where serialized HTML wouldn’t work or wouldn’t work well.
For example,
you can use refractor when you want to show code in a CLI by rendering to ANSI
sequences,
when you’re using virtual DOM frameworks
(such as React or Preact)
so that diffing can be performant,
or when you’re working with ASTs
(rehype).
A different package,
lowlight,
does the same as refractor but uses highlight.js
instead.
If you’re looking for a really good but rather heavy highlighter,
try starry-night.
import css from 'refractor/css'import {refractor} from 'refractor/core'refractor.register(css)console.log(refractor.highlight('em { color: red }', 'css'))
syntax (Function)
— language function custom made for refractor,
as in,
the files in refractor/*
Example
import markdown from 'refractor/markdown'import {refractor} from 'refractor/core'refractor.register(markdown)console.log(refractor.highlight('*Emphasis*', 'markdown'))
alias (string)
— new aliases for the programming language
list (Array<string>)
— list of aliases
aliases (Record<language, alias | list>)
— map of languages to aliases or lists
Example
import markdown from 'refractor/markdown'import {refractor} from 'refractor/core'refractor.register(markdown)// refractor.highlight('*Emphasis*', 'mdown')// ^ would throw: Error: Unknown language: `mdown` is not registeredrefractor.alias({markdown: ['mdown', 'mkdn', 'mdwn', 'ron']})refractor.highlight('*Emphasis*', 'mdown')// ^ Works!
refractor.registered(aliasOrlanguage)
Check whether an alias or language is registered.
Parameters
aliasOrlanguage (string)
— programming language name or alias
Example
import markdown from 'refractor/markdown'import {refractor} from 'refractor/core'console.log(refractor.registered('markdown')) //=> falserefractor.register(markdown)console.log(refractor.registered('markdown')) //=> true
refractor.listLanguages()
List all registered languages (names and aliases).
Returns
Array<string>.
Example
import markdown from 'refractor/markdown'import {refractor} from 'refractor/core'console.log(refractor.listLanguages()) //=> []refractor.register(markdown)console.log(refractor.listLanguages())
Yields:
[ 'markup', // Note that `markup` (a lot of xml based languages) is a dep of markdown. 'html', // … 'markdown', 'md']
hast trees as returned by refractor can be serialized with
hast-util-to-html:
import {toHtml} from 'hast-util-to-html'import {refractor} from 'refractor'const tree = refractor.highlight('"use strict";', 'js')console.log(toHtml(tree))
If you’re using refractor/core,
no syntaxes are included.
Checked syntaxes are included if you import refractor.
Unchecked syntaxes are available through refractor/all.
You can import refractor/core or refractor and manually add more languages
as you please.
Prism operates as a singleton:
once you register a language in one place,
it’ll be available everywhere.
Only these custom built syntaxes will work with refractor because Prism’s own
syntaxes are made to work with global variables and are not importable.
refractor does not inject CSS for the syntax highlighted code.
It does not make sense: refractor doesn’t have to be turned into HTML and might
not run in a browser!
If you are in a browser,
you can use any Prism theme.
For example,
to get Prism Dark from esm.sh:
This package is at least compatible with all maintained versions of Node.js.
As of now,
that is Node.js 16+.
It also works in Deno and modern browsers.
Only the custom built syntaxes in refractor/* will work with
refractor as Prism’s own syntaxes are made to work with global variables and
are not importable.
refractor also does not support Prism plugins,
due to the same limitations,
and that they almost exclusively deal with the DOM.