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.
…When using browserify and minifying with tinyify this results in
just 65kb of code (23kb with GZip).
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 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.
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 25 Feb 2022
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.