$ npm 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 258 languages (as of prism@1.24.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: ';'}]
}
]
Which serialized with rehype or hast-util-to-html
yields (you may have to wrap it into a fragment like so: {type: 'root', children: nodes}):
<span class="token string">"use strict"</span><span class="token punctuation">;</span>
Tip: Use
hast-to-hyperscriptto transform to other virtual DOMs, or DIY.
refractor.register(syntax)Register a syntax.
Needed if you’re using refractor/core.
var refractor = require('refractor/core')
var markdown = require('refractor/lang/markdown')
refractor.register(markdown)
console.log(refractor.highlight('*Emphasis*', 'markdown'))
Yields:
[
{
type: 'element',
tagName: 'span',
properties: {className: [Array]},
children: [[Object], [Object], [Object]]
}
]
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 listvar refractor = require('refractor/core')
var markdown = require('refractor/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')
console.log(refractor.highlight('em { color: red }', 'css'))
Yields:
[
{
type: 'element',
tagName: 'span',
properties: {className: [Array]},
children: [[Object]]
},
{type: 'text', value: ' '},
// …
{type: 'text', value: ' red '},
{
type: 'element',
tagName: 'span',
properties: {className: [Array]},
children: [[Object]]
}
]
refractor.registered(language)Check if a language (name or alias) is registered.
var refractor = require('refractor/core')
var markdown = require('refractor/lang/markdown')
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')
var markdown = require('refractor/lang/markdown')
console.log(refractor.listLanguages())
refractor.register(markdown)
console.log(refractor.listLanguages())
Yields:
[
'markup',
'html',
// …
'javascript',
'js'
]
[
'markup',
'html',
// …
'javascript',
'js',
'markdown',
'md'
]
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 and include only the needed syntaxes.
For example:
var refractor = require('refractor/core')
refractor.register(require('refractor/lang/jsx'))
console.log(refractor.highlight('<Dropdown primary />', 'jsx'))
Yields:
[
{
type: 'element',
tagName: 'span',
properties: {className: ['token', 'tag']},
children: [
{type: 'element', tagName: 'span', properties: {className: [Array]}, children: [[Object], [Object]]},
{type: 'text', value: ' '},
{type: 'element', tagName: 'span', properties: {className: [Array]}, children: [[Object]]},
{type: 'text', value: ' '},
{type: 'element', tagName: 'span', properties: {className: [Array]}, children: [[Object]]}
]
}
]
…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, checked syntaxes are always included, but
unchecked syntaxes are not and must be required and registered.
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.
clikecssjavascript — alias: jsmarkup — alias: html, mathml, svg, xml, ssml, atom, rssabapabnfactionscriptadaagdaalantlr4 — alias: g4apacheconfapexaplapplescriptaqlarduinoarffasciidoc — alias: adocasm6502aspnetautohotkeyautoitbash — alias: shellbasicbatchbbcode — alias: shortcodebirbbisonbnf — alias: rbnfbrainfuckbrightscriptbrobslccfscriptchaiscriptcilclojurecmakecobolcoffeescript — alias: coffeeconcurnas — alias: conccoqcppcrystalcsharp — alias: dotnet, cscspcssExtrascsvcypherddartdataweavedaxdhalldiffdjango — alias: jinja2dnsZoneFiledocker — alias: dockerfiledot — alias: gvebnfeditorconfigeiffelejs — alias: etaelixirelmerberlangetluaexcelFormulafactor$falsefirestoreSecurityRulesflowfortranfsharpftlgcodegdscriptgedcomgherkingitglslgmlgographqlgroovyhamlhandlebars — alias: hbshaskell — alias: hshaxehclhlslhpkphstshttpichigojamiconicuMessageFormatidris — alias: idriecstignore — alias: gitignore, hgignore, npmignoreinform7iniiojjavajavadocjavadoclikejavastacktracejexljoliejqjsExtrasjsTemplatesjsdocjson — alias: webmanifestjson5jsonpjsstacktracejsxjuliakeymankotlin — alias: kt, ktskumir — alias: kumlatex — alias: tex, contextlattelesslilypondliquidlisplivescriptllvmloglolcodeluamakefilemarkdown — alias: mdmarkupTemplatingmatlabmelmizarmongodbmonkeymoonscript — alias: moonn1qln4js — alias: n4jsdnand2tetrisHdlnaniscriptnasmneonnevodnginxnimnixnsisobjectivec — alias: objcocamlopenclopenqasm — alias: qasmozparigpparserpascal — alias: objectpascalpascaligopcaxis — alias: pxpeoplecode — alias: pcodeperlphpExtrasphpphpdocplsqlpowerquerypowershellprocessingprologpromqlpropertiesprotobufpslpugpuppetpurepurebasicpurescript — alias: purspython — alias: pyqqmlqoreqsharp — alias: qsrracket — alias: rktreasonregexregorenpy — alias: rpyrestriproboconfrobotframeworkruby — alias: rbrustsassassscalaschemescssshellSessionsmalismalltalksmartysml — alias: smlnjsolidity — alias: solsolutionFilesoysparql — alias: rqsplunkSplsqfsqlsquirrelstanstylusswiftt4Cst4Templatingt4Vbtaptcltextiletomltsxtt2turtletwigtypescript — alias: tstyposcript — alias: tsconfigunrealscript — alias: uc, uscripturi — alias: urlvvalavbnetvelocityverilogvhdlvimvisualBasicwarpscriptwasmwikiwolfram — alias: mathematica, wl, nbxeora — alias: xeoracubexmlDocxojoxqueryyaml — alias: ymlyangziglowlight — Same, but based on highlight.jsreact-syntax-highlighter
— React component for syntax highlightingrehype-prism
— Syntax highlighting in rehypereact-refractor
— Syntax highlighter for React© 2010 - cnpmjs.org x YWFE | Home | YWFE