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 — 297 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.
If you’re looking for a really good (but rather heavy) highlighter, try
starry-night.
This package is fully typed with TypeScript.
It exports the additional types Root, Grammar, and Syntax.
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 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,005,171 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.
Last updated on 28 Feb 2023
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.
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.