All Projects → wooorm → Refractor

wooorm / Refractor

Licence: mit
Lightweight, robust, elegant virtual syntax highlighting using Prism

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Refractor

Lowlight
Virtual syntax highlighting for virtual DOMs and non-HTML things
Stars: ✭ 310 (+6.53%)
Mutual labels:  syntax-highlighting, highlight, syntax, virtual-dom, vdom, virtual
Val
VirtualDOM abstraction layer - give yourself better integration and full control over the DOM with any virtual DOM library that uses a Hyperscript-like API such as React and Preact.
Stars: ✭ 181 (-37.8%)
Mutual labels:  virtual-dom, vdom, virtual
see-cli
A colorful 🌈 cat - syntax highlight print CLI
Stars: ✭ 24 (-91.75%)
Mutual labels:  syntax-highlighting, syntax, highlight
vscode-liquid
💧Liquid language support for VS Code
Stars: ✭ 137 (-52.92%)
Mutual labels:  syntax-highlighting, syntax
syntax highlighter
Syntax Highlighter for Dart/Flutter Code
Stars: ✭ 28 (-90.38%)
Mutual labels:  syntax-highlighting, syntax
nord-atom-syntax
An arctic, north-bluish clean and elegant Atom syntax theme.
Stars: ✭ 72 (-75.26%)
Mutual labels:  syntax-highlighting, syntax
Nord Sublime Text
An arctic, north-bluish clean and elegant Sublime Text theme.
Stars: ✭ 109 (-62.54%)
Mutual labels:  syntax-highlighting, syntax
CodeEditorView
Code Editor UITextView
Stars: ✭ 20 (-93.13%)
Mutual labels:  syntax, highlight
geshi-1.1
Next generation of Generic Syntax Highlighter for PHP
Stars: ✭ 70 (-75.95%)
Mutual labels:  syntax-highlighting, highlight
vdom
Simple JavaScript Virtual DOM
Stars: ✭ 17 (-94.16%)
Mutual labels:  virtual-dom, vdom
splash
🌊 Highlight source code embedded in HTML with a splash of color
Stars: ✭ 19 (-93.47%)
Mutual labels:  syntax-highlighting, syntax
nord-notepadplusplus
An arctic, north-bluish clean and elegant Notepad++ theme.
Stars: ✭ 112 (-61.51%)
Mutual labels:  syntax-highlighting, syntax
colocat
Fegeya Colocat, Colorized 'cat' implementation. Written in C++17.
Stars: ✭ 14 (-95.19%)
Mutual labels:  syntax, highlight
language-rainmeter
Syntax highlighting for Rainmeter files in Atom.
Stars: ✭ 19 (-93.47%)
Mutual labels:  syntax-highlighting, syntax
Geshi 1.0
Original version of Generic Syntax Highlighter for PHP
Stars: ✭ 149 (-48.8%)
Mutual labels:  syntax-highlighting, highlight
go-highlight
A Go (Golang) code syntax highlighting library.
Stars: ✭ 20 (-93.13%)
Mutual labels:  syntax-highlighting, highlight
KodeEditor
A simple code editor with syntax highlighting and pinch to zoom
Stars: ✭ 60 (-79.38%)
Mutual labels:  syntax-highlighting, syntax
fastedit
安卓端高性能输入框。
Stars: ✭ 38 (-86.94%)
Mutual labels:  syntax-highlighting, syntax
Alfred Hl
🔆 Syntax highlight code in the clipboard
Stars: ✭ 80 (-72.51%)
Mutual labels:  syntax-highlighting, highlight
Syntax Highlighter
Syntax Highlighter extension for Visual Studio Code (VSCode). Based on Tree-sitter.
Stars: ✭ 88 (-69.76%)
Mutual labels:  syntax-highlighting, syntax

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 237 languages (as of [email protected]) 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: ';'}]
  }
]

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-hyperscript to transform to other virtual DOMs, or DIY.

API

refractor.register(syntax)

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

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

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('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.

Returns

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

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

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

Returns

Array.<string>.

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

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

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, 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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].