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.
This package wraps Prism to output objects (ASTs) instead of a string of
HTML.
Prism, through refractor, supports 270+ programming languages.
Supporting all of them requires a lot of code.
That’s why there are three entry points for refractor:
lib/core.js — 0 languages
lib/common.js (default) — 36 languages
lib/all.js — 276 languages
Bundled, minified, and gzipped, those are roughly 12.7 kB, 40 kB, and 211 kB.
When should I use this?
This package is useful when you want to perform syntax highlighting in a place
where serialized HTML wouldn’t work or wouldn’t work well.
For example, you can use refractor when you want to show code in a CLI by
rendering to ANSI sequences, when you’re using virtual DOM frameworks (such as
React or Preact) so that diffing can be performant, or when you’re working with
ASTs (rehype).
A different package, lowlight, does the same as refractor but
uses highlight.js instead.
This package is fully typed with TypeScript.
It exports additional Root, Grammar, and Syntax types that model their
respective interfaces.
Data
If you’re using refractor/lib/core.js, no syntaxes are included.
Checked syntaxes are included if you import refractor (or explicitly
refractor/lib/common.js).
Unchecked syntaxes are available through refractor/lib/all.js.
You can import core or common and manually add more languages as you please.
Prism operates as a singleton: once you register a language in one place, it’ll
be 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 importable.
refractor does not inject CSS for the syntax highlighted code (because well,
refractor doesn’t have to be turned into HTML and might not run in a browser!).
If you are in a browser, you can use any Prism theme.
For example, to get Prism Dark from cdnjs:
This package is at least compatible with all maintained versions of Node.js.
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
It also works in Deno and modern browsers.
Only the custom built syntaxes in refractor/lang/*.js will work with
refractor as Prism’s own syntaxes are made to work with global variables and
are not importable.
refractor also does not support Prism plugins, due to the same limitations, and
that they almost exclusively deal with the DOM.
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 06 Jan 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.