Skip to content
Lightweight, robust, elegant virtual syntax highlighting using Prism
JavaScript
Branch: master
Clone or download

Latest commit

Latest commit 7372dcc Apr 6, 2020

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lang Update `prism` to `1.20.0` Apr 6, 2020
script Update dev-dependencies Apr 6, 2020
test Update `prism` to `1.18.0` Apr 6, 2020
.editorconfig Initial commit Aug 2, 2017
.gitignore Add `yarn.lock` to `.gitignore` Apr 29, 2018
.npmrc Add `.npmrc` Apr 29, 2018
.prettierignore Refactor code-style Apr 29, 2018
.travis.yml Update Travis May 15, 2019
changelog.md Add `changelog.md` Nov 3, 2018
core.js Add support for registering aliases Apr 19, 2019
funding.yml Add `funding.yml` Oct 15, 2019
index.js Update `prism` to `1.18.0` Apr 6, 2020
license Move `LICENSE` > `license` Nov 3, 2018
package.json 3.0.0 Apr 6, 2020
readme.md Update `prism` to `1.20.0` Apr 6, 2020

readme.md

refractor

Build Coverage Downloads Size

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!

Contents

Install

npm:

npm install refractor

Use in the browser »

Use

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.

API

refractor.register(syntax)

Register a syntax. Needed if you’re using refractor/core.js.

Example
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.

Signatures
  • alias(name, alias|list)
  • alias(aliases)
Parameters
  • name (string) — Name of a registered language
  • alias (string) — New alias for the registered language
  • list (Array.<alias>) — List of aliases
  • aliases (Object.<alias|list>) — Map where each key is a name and each value an alias or a list
Example
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.

Returns

Virtual nodes representing the highlighted value (Array.<Node>).

Example
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.

Example
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).

Returns

Array.<string>.

Example
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' ]

Browser

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).

Plugins

refractor does not support Prism plugins:

  1. Prism plugins often deal with the DOM, not Prism tokens
  2. Prism is made using global variables instead of a module format, so all syntaxes below are custom built to work so you can require just what you need

Syntaxes

All 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 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.

Related

Projects

License

MIT © Titus Wormer

You can’t perform that action at this time.