The lowlight npm package is a library for syntax highlighting of code using languages supported by highlight.js. It is primarily used to parse and transform code into a syntax-highlighted format without a browser, making it suitable for server-side applications or pre-processing of static pages.
What are lowlight's main functionalities?
Syntax Highlighting
This feature allows you to highlight syntax of various programming languages. The code sample demonstrates highlighting a simple JavaScript code snippet.
This feature enables the addition of new languages to lowlight's highlighting capabilities by registering them through highlight.js. The code sample shows how to register and highlight Python code.
This feature automatically detects the programming language of a given code snippet and applies appropriate syntax highlighting. The code sample demonstrates auto-detection for a JavaScript snippet.
const lowlight = require('lowlight');
const result = lowlight.highlightAuto('const x = 123;');
console.log(result.language); // 'javascript'
console.log(result.value);
highlight.js is a widely used library for syntax highlighting. It supports numerous languages and is often used directly in web browsers. Compared to lowlight, highlight.js is more versatile for client-side applications but both can be used server-side.
Prism is another popular syntax highlighting library that works both in the browser and on the server. It is known for its lightweight core and extensible plugins. Unlike lowlight, PrismJS focuses more on client-side performance and extensibility.
Shiki is a syntax highlighter powered by the same language grammars as Visual Studio Code. It provides accurate and visually appealing highlights. Shiki is similar to lowlight in that it can be used server-side, but it offers a richer set of themes and a different integration approach.
lowlight
Virtual syntax highlighting for virtual DOMs and non-HTML things based on
highlight.js.
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 472,353 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.
Package 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.
Elastic’s return to open source with the AGPL license has been met with skepticism, as many developers see it as a strategic move rather than a genuine effort to restore user trust and freedoms.
A new "revival hijack" supply chain attack targets deleted Python packages, with an estimated 22K packages at risk. Socket can detect and block hijacked packages that have added malicious code.