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,
and elegant virtual syntax highlighting using Prism.
This package wraps Prism to output objects (ASTs) instead of a
string of HTML.
Prism,
through refractor,
supports 290+ programming languages.
Supporting all of them requires a lot of code.
That’s why there are three entry points for refractor:
refractor/all — 297 languages
refractor/core — 0 languages
refractor (default) — 36 common languages
Bundled,
minified,
and gzipped,
those are roughly 12.7 kB (core),
40 kB (default),
and 211 kB (all).
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.
If you’re looking for a really good but rather heavy highlighter,
try starry-night.
If you’re using refractor/core,
no syntaxes are included.
Checked syntaxes are included if you import refractor.
Unchecked syntaxes are available through refractor/all.
You can import refractor/core or refractor 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.
It does not make sense: 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 esm.sh:
This package is at least compatible with all maintained versions of Node.js.
As of now,
that is Node.js 16+.
It also works in Deno and modern browsers.
Only the custom built syntaxes in refractor/* 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 1,981,209 weekly downloads. As such, refractor popularity was classified as popular.
We found that refractor demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 1 open source maintainer collaborating on the project.
Package last updated on 11 Mar 2025
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.
New CNA status enables OpenJS Foundation to assign CVEs for security vulnerabilities in projects like ESLint, Fastify, Electron, and others, while leaving disclosure responsibility with individual maintainers.