What is refractor?
Refractor is a lightweight, robust, and extensible syntax highlighter that works in both Node.js and the browser. It is built on top of the Prism syntax highlighter and provides a way to highlight code syntax in various programming languages.
What are refractor's main functionalities?
Syntax Highlighting
This feature allows you to highlight code syntax for a given programming language. The example highlights a simple JavaScript code snippet.
const refractor = require('refractor');
const html = refractor.highlight('const x = 42;', 'javascript');
console.log(html);
Registering Languages
You can register additional languages for syntax highlighting. This example registers the Python language and highlights a Python code snippet.
const refractor = require('refractor');
const python = require('refractor/lang/python');
refractor.register(python);
const html = refractor.highlight('print("Hello, World!")', 'python');
console.log(html);
Syntax Highlighting with Plugins
Refractor supports plugins to extend its functionality. This example uses the line-numbers plugin to add line numbers to the highlighted code.
const refractor = require('refractor');
const lineNumbers = require('refractor/plugins/line-numbers');
refractor.use(lineNumbers);
const html = refractor.highlight('const x = 42;', 'javascript');
console.log(html);
Other packages similar to refractor
highlight.js
Highlight.js is a popular syntax highlighter written in JavaScript. It automatically detects the language of the code and applies syntax highlighting. Compared to Refractor, Highlight.js is more automatic in language detection but may not be as customizable.
prismjs
Prism is a lightweight, extensible syntax highlighter, built with modern web standards in mind. Refractor is built on top of Prism, so they share many similarities. However, Refractor provides a more modular approach and additional features like plugins.
codemirror
CodeMirror is a versatile text editor implemented in JavaScript for the browser. It is specialized for editing code and comes with syntax highlighting capabilities. While CodeMirror is more of a full-fledged code editor, Refractor focuses solely on syntax highlighting.
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 120 languages (as of prism@1.6.0) (and all
7 themes).
Want to use highlight.js
instead? Try lowlight
!
Table of Contents
Installation
npm:
npm install
Usage in the browser »
Usage
var refractor = require('refractor');
var result = refractor.highlight('"use strict";', 'js');
console.dir(result, {depth: null});
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, stringified with rehype:
var rehype = require('rehype');
var html = rehype().stringify({type: 'root', children: result}).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.
API
refractor.register(syntax)
Register a syntax. Needed in the browser.
refractor.highlight(value, language)
Parse value
(string
) according to the language
(name or alias)
grammar.
Returns
Virtual nodes representing the highlighted value (Array.<Node>
).
Browser
I do not suggest using the pre-built files or requiring refractor
in
the browser as that would include a 250kb (90kb GZipped) file.
Instead, require refractor/core.js
, and include only the used
highlighters. For example:
var refractor = require('refractor/core.js');
var jsx = require('refractor/lang/jsx.js');
refractor.register(jsx);
var result = refractor.highlight('<Dropdown primary />', 'jsx');
console.dir(result, {depth: null});
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 the result, this results in just 19kb of
code (7.62kb with GZip).
Plugins
refractor
does not support Prism plugins. Prism is built using global
variables instead of a module format. All the syntaxes below are custom made
to work with CommonJS so you can require
just what you want to make things
work. Additionally, Prism plugins often deal with the real DOM instead of the
tokens created by Prism, making them hard to get to work in virtual DOMs.
Syntaxes
All syntaxes are included if you require('refractor')
. Checked items are
always included in the core (core.js
), but unchecked items are not and must
be require
d and register
ed if core.js
is used.
Unlike in Prism, cssExtras
and phpExtras
are camel-cased instead of
dash-cased.
Prism syntaxes are built to work with global variables and without module
format. All below syntaxes are custom made to work with CommonJS. Only
these custom built syntaxes will work with refractor
.
Related
License
MIT © Titus Wormer