$ cnpm install refractor
Lightweight, robust, elegant virtual syntax highlighting using Prism. Useful for virtual DOMs and non-HTML things. Perfect for React, VDOM, and others.
refractor
is built to work with all syntaxes supported by Prism,
that’s 209 languages (as of prism@1.20.0) and all
themes.
Want to use highlight.js
instead?
Try lowlight
!
npm:
npm install refractor
var refractor = require('refractor')
var nodes = refractor.highlight('"use strict";', 'js')
console.log(nodes)
Yields:
[ { type: 'element',
tagName: 'span',
properties: { className: [ 'token', 'string' ] },
children: [ { type: 'text', value: '"use strict"' } ] },
{ type: 'element',
tagName: 'span',
properties: { className: [ 'token', 'punctuation' ] },
children: [ { type: 'text', value: ';' } ] } ]
Or, serialized with rehype:
var rehype = require('rehype')
var html = rehype()
.stringify({type: 'root', children: nodes})
.toString()
console.log(html)
Yields:
<span class="token string">"use strict"</span><span class="token punctuation">;</span>
Tip: Use
hast-to-hyperscript
to transform to other virtual DOMs, or DIY.
refractor.register(syntax)
Register a syntax.
Needed if you’re using refractor/core.js
.
var refractor = require('refractor/core.js')
var markdown = require('refractor/lang/markdown.js')
refractor.register(markdown)
console.log(refractor.highlight('*Emphasis*', 'markdown'))
Yields:
[ { type: 'element',
tagName: 'span',
properties: [Object],
children: [Array] } ]
refractor.alias(name[, alias])
Register a new alias
for the name
language.
alias(name, alias|list)
alias(aliases)
name
(string
) — Name of a registered languagealias
(string
) — New alias for the registered languagelist
(Array.<alias>
) — List of aliasesaliases
(Object.<alias|list>
) — Map where each key is a name
and each
value an alias
or a list
var refractor = require('./core')
var markdown = require('./lang/markdown')
refractor.register(markdown)
// refractor.highlight('*Emphasis*', 'mdown')
// ^ would throw: Error: Unknown language: `mdown` is not registered
refractor.alias({markdown: ['mdown', 'mkdn', 'mdwn', 'ron']})
refractor.highlight('*Emphasis*', 'mdown')
// ^ Works!
refractor.highlight(value, language)
Parse value
(string
) according to the language
(name or alias)
syntax.
Virtual nodes representing the highlighted value (Array.<Node>
).
var refractor = require('refractor/core.js')
console.log(refractor.highlight('em { color: red }', 'css'))
Yields:
[ { type: 'element',
tagName: 'span',
properties: [Object],
children: [Array] },
{ type: 'text', value: ' ' },
// ...
{ type: 'text', value: ' red ' },
{ type: 'element',
tagName: 'span',
properties: [Object],
children: [Array] } ]
refractor.registered(language)
Check if a language
(name or alias) is registered.
var refractor = require('refractor/core.js')
var markdown = require('refractor/lang/markdown.js')
console.log(refractor.registered('markdown'))
refractor.register(markdown)
console.log(refractor.registered('markdown'))
Yields:
false
true
refractor.listLanguages()
List all registered languages (names and aliases).
Array.<string>
.
var refractor = require('refractor/core.js')
var markdown = require('refractor/lang/markdown.js')
console.log(refractor.listLanguages())
refractor.register(markdown)
console.log(refractor.listLanguages())
Yields:
[ 'markup',
'xml',
'html',
'mathml',
'svg',
'css',
'clike',
'javascript',
'js' ]
[ 'markup',
'xml',
'html',
'mathml',
'svg',
'css',
'clike',
'javascript',
'js',
'markdown' ]
I do not suggest using the pre-bundled files or requiring
refractor
itself in the browser as that would include a 376kb (139kb GZipped)
of code.
Instead require refractor/core.js
and include only the needed syntaxes.
For example:
var refractor = require('refractor/core.js')
refractor.register(require('refractor/lang/jsx.js'))
console.log(refractor.highlight('<Dropdown primary />', 'jsx'))
Yields:
[ { type: 'element',
tagName: 'span',
properties: { className: [ 'token', 'tag' ] },
children:
[ { type: 'element',
tagName: 'span',
properties: { className: [ 'token', 'tag' ] },
children:
[ { type: 'element',
tagName: 'span',
properties: { className: [ 'token', 'punctuation' ] },
children: [ { type: 'text', value: '<' } ] },
{ type: 'text', value: 'Dropdown' } ] },
{ type: 'text', value: ' ' },
{ type: 'element',
tagName: 'span',
properties: { className: [ 'token', 'attr-name' ] },
children: [ { type: 'text', value: 'primary' } ] },
{ type: 'text', value: ' ' },
{ type: 'element',
tagName: 'span',
properties: { className: [ 'token', 'punctuation' ] },
children: [ { type: 'text', value: '/>' } ] } ] } ]
…When using browserify and minifying with tinyify this results in just 65kb of code (23kb with GZip).
refractor
does not support Prism plugins:
require
just what you
needAll syntaxes are included if you require('refractor')
.
If you’re using refractor/core.js
, checked syntaxes are always included, but
unchecked syntaxes are not and must be require
d and register
ed.
Unlike in Prism, cssExtras
and phpExtras
are camel-cased instead of
dash-cased.
Only these custom built syntaxes will work with refractor
because Prism’s own
syntaxes are made to work with global variables and are not requirable.
clike
css
javascript
— alias: js
markup
— alias: xml
, html
, mathml
, svg
abap
abnf
actionscript
ada
antlr4
— alias: g4
apacheconf
apl
applescript
aql
arduino
arff
asciidoc
— alias: adoc
asm6502
aspnet
autohotkey
autoit
bash
— alias: shell
basic
batch
bbcode
— alias: shortcode
bison
bnf
— alias: rbnf
brainfuck
brightscript
bro
c
cil
clojure
cmake
coffeescript
— alias: coffee
concurnas
— alias: conc
cpp
crystal
csharp
— alias: dotnet
, cs
csp
cssExtras
d
dart
dax
diff
django
— alias: jinja2
dnsZoneFile
docker
— alias: dockerfile
ebnf
eiffel
ejs
elixir
elm
erb
erlang
etlua
excelFormula
factor
firestoreSecurityRules
flow
fortran
fsharp
ftl
gcode
gdscript
gedcom
gherkin
git
glsl
gml
go
graphql
groovy
haml
handlebars
haskell
— alias: hs
haxe
hcl
hpkp
hsts
http
ichigojam
icon
inform7
ini
io
j
java
javadoc
javadoclike
javastacktrace
jolie
jq
jsExtras
jsTemplates
jsdoc
json
json5
jsonp
jsx
julia
keyman
kotlin
latex
— alias: tex
, context
latte
less
lilypond
liquid
lisp
livescript
llvm
lolcode
lua
makefile
markdown
— alias: md
markupTemplating
matlab
mel
mizar
monkey
moonscript
— alias: moon
n1ql
n4js
nand2tetrisHdl
nasm
neon
nginx
nim
nix
nsis
objectivec
ocaml
opencl
oz
parigp
parser
pascal
— alias: objectpascal
pascaligo
pcaxis
— alias: px
perl
phpExtras
php
phpdoc
plsql
powerquery
powershell
processing
prolog
properties
protobuf
pug
puppet
pure
python
— alias: py
q
qml
qore
r
reason
regex
renpy
rest
rip
roboconf
robotframework
ruby
— alias: rb
rust
sas
sass
scala
scheme
scss
shellSession
smalltalk
smarty
solidity
solutionFile
soy
sparql
— alias: rq
splunkSpl
sqf
sql
stylus
swift
t4Cs
t4Templating
t4Vb
tap
tcl
textile
toml
tsx
tt2
turtle
twig
typescript
— alias: ts
vala
vbnet
velocity
verilog
vhdl
vim
visualBasic
wasm
wiki
xeora
— alias: xeoracube
xojo
xquery
yaml
— alias: yml
zig
lowlight
— Same, but based on highlight.js
react-syntax-highlighter
— React component for syntax highlightingrehype-prism
— Syntax highlighting in rehypereact-refractor
— Syntax highlighter for ReactCopyright 2013 - present © cnpmjs.org