All Projects → wooorm → Lowlight

wooorm / Lowlight

Licence: mit
Virtual syntax highlighting for virtual DOMs and non-HTML things

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Lowlight

Refractor
Lightweight, robust, elegant virtual syntax highlighting using Prism
Stars: ✭ 291 (-6.13%)
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 (-41.61%)
Mutual labels:  virtual-dom, vdom, virtual
see-cli
A colorful 🌈 cat - syntax highlight print CLI
Stars: ✭ 24 (-92.26%)
Mutual labels:  syntax-highlighting, syntax, highlight
Nord Jetbrains
An arctic, north-bluish clean and elegant JetBrains IDE UI and editor color theme.
Stars: ✭ 293 (-5.48%)
Mutual labels:  syntax-highlighting, syntax
language-rainmeter
Syntax highlighting for Rainmeter files in Atom.
Stars: ✭ 19 (-93.87%)
Mutual labels:  syntax-highlighting, syntax
vscode-liquid
💧Liquid language support for VS Code
Stars: ✭ 137 (-55.81%)
Mutual labels:  syntax-highlighting, syntax
colocat
Fegeya Colocat, Colorized 'cat' implementation. Written in C++17.
Stars: ✭ 14 (-95.48%)
Mutual labels:  syntax, highlight
vdom
Simple JavaScript Virtual DOM
Stars: ✭ 17 (-94.52%)
Mutual labels:  virtual-dom, vdom
go-highlight
A Go (Golang) code syntax highlighting library.
Stars: ✭ 20 (-93.55%)
Mutual labels:  syntax-highlighting, highlight
enhanced-slack
🌴 PoC Slack enhancer/injector
Stars: ✭ 48 (-84.52%)
Mutual labels:  syntax-highlighting, highlight
splash
🌊 Highlight source code embedded in HTML with a splash of color
Stars: ✭ 19 (-93.87%)
Mutual labels:  syntax-highlighting, syntax
fastedit
安卓端高性能输入框。
Stars: ✭ 38 (-87.74%)
Mutual labels:  syntax-highlighting, syntax
nord-atom-syntax
An arctic, north-bluish clean and elegant Atom syntax theme.
Stars: ✭ 72 (-76.77%)
Mutual labels:  syntax-highlighting, syntax
syntax highlighter
Syntax Highlighter for Dart/Flutter Code
Stars: ✭ 28 (-90.97%)
Mutual labels:  syntax-highlighting, syntax
geshi-1.1
Next generation of Generic Syntax Highlighter for PHP
Stars: ✭ 70 (-77.42%)
Mutual labels:  syntax-highlighting, highlight
nord-notepadplusplus
An arctic, north-bluish clean and elegant Notepad++ theme.
Stars: ✭ 112 (-63.87%)
Mutual labels:  syntax-highlighting, syntax
CodeEditorView
Code Editor UITextView
Stars: ✭ 20 (-93.55%)
Mutual labels:  syntax, highlight
Nord Sublime Text
An arctic, north-bluish clean and elegant Sublime Text theme.
Stars: ✭ 109 (-64.84%)
Mutual labels:  syntax-highlighting, syntax
Geshi 1.0
Original version of Generic Syntax Highlighter for PHP
Stars: ✭ 149 (-51.94%)
Mutual labels:  syntax-highlighting, highlight
KodeEditor
A simple code editor with syntax highlighting and pinch to zoom
Stars: ✭ 60 (-80.65%)
Mutual labels:  syntax-highlighting, syntax

lowlight

Build Coverage Downloads Size

Virtual syntax highlighting for virtual DOMs and non-HTML things, with language auto-detection. Perfect for React, VDOM, and others.

Lowlight is built to work with all syntaxes supported by highlight.js, that’s 191 languages (and all 94 themes).

Want to use Prism instead? Try refractor!

Contents

Install

npm:

npm install lowlight

Usage in the browser »

Use

Highlight:

var low = require('lowlight')
var tree = low.highlight('js', '"use strict";').value

console.log(tree)

Yields:

[
  {
    type: 'element',
    tagName: 'span',
    properties: {className: ['hljs-meta']},
    children: [{type: 'text', value: '"use strict"'}]
  },
  {type: 'text', value: ';'}
]

Or, serialized with rehype-stringify:

var unified = require('unified')
var rehypeStringify = require('rehype-stringify')

var processor = unified().use(rehypeStringify)
var html = processor.stringify({type: 'root', children: tree}).toString()

console.log(html)

Yields:

<span class="hljs-meta">"use strict"</span>;

Tip: Use hast-to-hyperscript to transform to other virtual DOMs, or DIY.

API

low.highlight(language, value[, options])

Parse value (string) according to the language grammar.

options
  • prefix (string?, default: 'hljs-') — Class prefix
Returns

Result.

Example
var low = require('lowlight')

console.log(low.highlight('css', 'em { color: red }'))

Yields:

{relevance: 4, language: 'css', value: [Array]}

low.highlightAuto(value[, options])

Parse value by guessing its grammar.

options
  • prefix (string?, default: 'hljs-') — Class prefix
  • subset (Array.<string>? default: all registered languages) — List of allowed languages
Returns

Result, with a secondBest if found.

Example
var low = require('lowlight')

console.log(low.highlightAuto('"hello, " + name + "!"'))

Yields:

{
  relevance: 3,
  language: 'applescript',
  value: [Array],
  secondBest: {relevance: 3, language: 'basic', value: [Array]}
}

Result

Result is a highlighting result object.

Properties
  • relevance (number) — How sure low is that the given code is in the found language
  • language (string) — The detected language
  • value (Array.<Node>) — Virtual nodes representing the highlighted given code
  • secondBest (Result?) — Result of the second-best (based on relevance) match. Only set by highlightAuto, but can still be null.

low.registerLanguage(name, syntax)

Register a syntax as name (string). Useful in the browser or with custom grammars.

Example
var low = require('lowlight/lib/core')
var xml = require('highlight.js/lib/languages/xml')

low.registerLanguage('xml', xml)

console.log(low.highlight('html', '<em>Emphasis</em>'))

Yields:

{relevance: 2, language: 'html', value: [Array]}

low.registerAlias(name[, alias])

Register a new alias for the name language.

Signatures
  • registerAlias(name, alias|list)
  • registerAlias(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 low = require('lowlight/lib/core')
var md = require('highlight.js/lib/languages/markdown')

low.registerLanguage('markdown', md)

// low.highlight('mdown', '<em>Emphasis</em>')
// ^ would throw: Error: Unknown language: `mdown` is not registered

low.registerAlias({markdown: ['mdown', 'mkdn', 'mdwn', 'ron']})
low.highlight('mdown', '<em>Emphasis</em>')
// ^ Works!

low.listLanguages()

List all registered languages.

Returns

Array.<string>.

Example
var low = require('lowlight/lib/core')
var md = require('highlight.js/lib/languages/markdown')

console.log(low.listLanguages()) // => []

low.registerLanguage('markdown', md)

console.log(low.listLanguages()) // => ['markdown']

Browser

It is not suggested to use the pre-built files or requiring lowlight in the browser as that would include 916kB (260kB GZipped) of code.

Instead, require lowlight/lib/core, and include only the used highlighters. For example:

var low = require('lowlight/lib/core')
var js = require('highlight.js/lib/languages/javascript')

low.registerLanguage('javascript', js)

low.highlight('js', '"use strict";')
// See `Usage` for the results.

…when using browserify and minifying with tinyify this results in 24kB of code (9kB with GZip).

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