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.
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.
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 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 [highlight.js][].
There are three builds:
…when using esbuild this results in 35.8kB of code (14kb minzipped).
Plugins
refractor does not support Prism plugins:
Prism plugins often deal with the DOM, not Prism tokens
Prism is made using global variables instead of a module format, so all
syntaxes below are custom built to work so you can import just what you
need
Syntaxes
If you’re using refractor/lib/core.js, no syntaxes are included.
Checked syntaxes are included if you import refractor (or
refractor/lib/common.js).
Unchecked syntaxes are available through refractor/lib/all.js.
Note that Prism works as a singleton.
That means that if you register a syntax anywhere in your project, it’ll
become available everywhere!
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.
Lightweight, robust, elegant virtual syntax highlighting using Prism
The npm package refractor receives a total of 2,238,014 weekly downloads. As such, refractor popularity was classified as popular.
We found that refractor demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.It has 1 open source maintainer collaborating on the project.
Package last updated on 28 Jun 2021
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.