This package uses highlight.js for syntax highlighting and
outputs objects (ASTs) instead of a string of HTML.
It can support 190+ programming languages.
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 lowlight 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).
You can use the similar refractor if you want to use Prism
grammars instead.
If you’re looking for a really good (but rather heavy) alternative, use
starry-night.
Install
This package is ESM only.
In Node.js (version 16+), install with npm:
Tree (Root); with the following data fields: language
(string), detected programming language name; relevance (number), how
sure lowlight is that the given code is in the language.
Tree (Root); with the following data fields: language
(string), detected programming language name; relevance (number), how
sure lowlight is that the given code is in the language.
import {createLowlight} from'lowlight'import xml from'highlight.js/lib/languages/xml'const lowlight = createLowlight()
lowlight.register({xml})
// Note: `html` is an alias for `xml`.console.log(lowlight.highlight('html', '<em>Emphasis</em>'))
hast trees as returned by lowlight can be turned into nodes of any framework
that supports JSX, such as preact, react, solid, svelte, vue, and more, with
hast-util-to-jsx-runtime:
It also registers root.data with @types/hast.
If you’re working with the data fields, make sure to import this package
somewhere in your types, as that registers the new fields on the file.
/**
* @typedef {import('hast').Root} Root
*
* @typedef {import('lowlight')}
*/import {VFile} from'vfile'/** @type {Root} */const root = {type: 'root', children: []}
console.log(root.data?.language) //=> TS now knows that this is a `string?`.
Data
If you’re using createLowlight(), no syntaxes are included yet.
You can import all or common and pass them, such as with
createLowlight(all).
Checked syntaxes are included in common.
All syntaxes are included in all.
You can also manually import syntaxes from highlight.js/lib/languages/xxx,
where xxx is the name, such as 'highlight.js/lib/languages/wasm'.
xml (html, xhtml, rss, atom, xjb, xsd, xsl, plist, wsf, svg) — HTML, XML
xquery (xpath, xq, xqm) — XQuery
yaml (yml) — YAML
zephir (zep) — Zephir
CSS
lowlight does not inject CSS for the syntax highlighted code (because well,
lowlight 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 highlight.js theme.
For example, to get GitHub Dark from cdnjs:
This package is compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line,
lowlight@^3, compatible with Node.js 16.
Virtual syntax highlighting for virtual DOMs and non-HTML things
The npm package lowlight receives a total of 2,077,407 weekly downloads. As such, lowlight popularity was classified as popular.
We found that lowlight 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.
Last updated on 10 Oct 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.
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
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.