Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
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.
Syntax Highlighting
This feature allows you to highlight syntax of various programming languages. The code sample demonstrates highlighting a simple JavaScript code snippet.
const lowlight = require('lowlight');
const highlightedCode = lowlight.highlight('javascript', 'const x = 123;').value;
console.log(highlightedCode);
Registering Languages
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.
const lowlight = require('lowlight');
lowlight.registerLanguage('python', require('highlight.js/lib/languages/python'));
const highlightedPython = lowlight.highlight('python', 'def foo(): return 123').value;
console.log(highlightedPython);
Auto-Detection of Language
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.
Virtual syntax highlighting for virtual DOMs and non-HTML things, with language auto-detection. Perfect for React, VDOM, and others.
Lowlight is built to work with all syntaxes supported by highlight.js. There are three builds of lowlight:
lib/core.js
— 0 languageslib/common.js
(default) — 35 languageslib/all.js
— 191 languagesWant to use Prism instead?
Try refractor
!
This package is ESM only:
Node 12+ is needed to use it and it must be import
ed instead of require
d.
npm:
npm install lowlight
Highlight:
import {lowlight} from 'lowlight'
const tree = lowlight.highlight('js', '"use strict";')
console.dir(tree, {depth: null})
Yields:
{
type: 'root',
data: {language: 'js', relevance: 10},
children: [
{
type: 'element',
tagName: 'span',
properties: {className: ['hljs-meta']},
children: [{type: 'text', value: '"use strict"'}]
},
{type: 'text', value: ';'}
]
}
hast trees can be serialized with hast-util-to-html
:
import {lowlight} from 'lowlight'
import {toHtml} from 'hast-util-to-html'
const tree = lowlight.highlight('js', '"use strict";')
console.log(toHtml(tree))
Yields:
<span class="hljs-meta">"use strict"</span>;
hast trees can be turned into other things, like virtual DOMs, with
hast-to-hyperscript
.
hast trees are also used throughout the rehype (unified) ecosystem:
import {unified} from 'unified'
import rehypeStringify from 'rehype-stringify'
import {lowlight} from 'lowlight'
const tree = lowlight.highlight('js', '"use strict";')
const processor = unified().use(rehypeStringify)
const html = processor.stringify(tree).toString()
console.log(html)
Yields:
<span class="hljs-meta">"use strict"</span>;
This package exports the following identifiers: lowlight
.
There is no default export.
lowlight.highlight(language, value[, options])
Parse value
(string
) according to the language
grammar.
options
prefix
(string?
, default: 'hljs-'
) — Class prefixA hast Root
with two data
fields:
relevance
(number
) — How sure low is that the given code is in the
languagelanguage
(string
) — The detected language
nameimport {lowlight} from 'lowlight'
console.log(lowlight.highlight('css', 'em { color: red }'))
Yields:
{type: 'root', data: {language: 'css', relevance: 3}, children: [Array]}
lowlight.highlightAuto(value[, options])
Parse value
by guessing its grammar.
options
The options of lowlight.highlight
are supported, plus:
subset
(Array.<string>?
default: all registered languages)
— List of allowed languagesThe same result as lowlight.highlight
is returned.
import {lowlight} from 'lowlight'
console.log(lowlight.highlightAuto('"hello, " + name + "!"'))
Yields:
{type: 'root', data: {language: 'applescript', relevance: 3}, children: [Array]}
lowlight.registerLanguage(language, syntax)
Register a syntax as language
(string
).
Useful in the browser or with custom grammars.
import {lowlight} from 'lowlight/lib/core.js'
import xml from 'highlight.js/lib/languages/xml.js'
lowlight.registerLanguage('xml', xml)
console.log(lowlight.highlight('html', '<em>Emphasis</em>'))
Yields:
{type: 'root', data: {language: 'html', relevance: 2}, children: [Array]}
lowlight.registerAlias(language, alias)
Register a new alias
for language
.
registerAlias(language, alias|list)
registerAlias(aliases)
language
(string
) — Name of a registered languagealias
(string
) — New alias for the registered languagelist
(Array.<alias>
) — List of aliasesaliases
(Object.<language, alias|list>
) — Map where each key is a
language
and each value an alias
or a list
import {lowlight} from 'lowlight/lib/core.js'
import md from 'highlight.js/lib/languages/markdown.js'
lowlight.registerLanguage('markdown', md)
// lowlight.highlight('mdown', '<em>Emphasis</em>')
// ^ would throw: Error: Unknown language: `mdown` is not registered
lowlight.registerAlias({markdown: ['mdown', 'mkdn', 'mdwn', 'ron']})
lowlight.highlight('mdown', '<em>Emphasis</em>')
// ^ Works!
lowlight.listLanguages()
List all registered languages.
Array.<string>
.
import {lowlight} from 'lowlight/lib/core.js'
import md from 'highlight.js/lib/languages/markdown.js'
console.log(lowlight.listLanguages()) // => []
lowlight.registerLanguage('markdown', md)
console.log(lowlight.listLanguages()) // => ['markdown']
If you’re using lowlight/lib/core.js
, no syntaxes are included.
Checked syntaxes are included if you import lowlight
(or
lowlight/lib/common.js
).
Unchecked syntaxes are available through lowlight/lib/all.js
Note that highlight.js works as a singleton. That means that if you register a syntax anywhere in your project, it’ll become available everywhere!
1c
— 1C:Enterpriseabnf
— Augmented Backus-Naur Formaccesslog
— Apache Access Logactionscript
(as
) — ActionScriptada
— Adaangelscript
(asc
) — AngelScriptapache
(apacheconf
) — Apache configapplescript
(osascript
) — AppleScriptarcade
— ArcGIS Arcadearduino
(ino
) — Arduinoarmasm
(arm
) — ARM Assemblyasciidoc
(adoc
) — AsciiDocaspectj
— AspectJautohotkey
(ahk
) — AutoHotkeyautoit
— AutoItavrasm
— AVR Assemblyawk
— Awkaxapta
(x++
) — X++bash
(sh
) — Bashbasic
— BASICbnf
— Backus–Naur Formbrainfuck
(bf
) — Brainfuckc
(h
) — Ccal
— C/ALcapnproto
(capnp
) — Cap’n Protoceylon
— Ceylonclean
(icl
, dcl
) — Cleanclojure
(clj
, edn
) — Clojureclojure-repl
— Clojure REPLcmake
(cmake.in
) — CMakecoffeescript
(coffee
, cson
, iced
) — CoffeeScriptcoq
— Coqcos
(cls
) — Caché Object Scriptcpp
(cc
, c++
, h++
, hpp
, hh
, hxx
, cxx
) — C++crmsh
(crm
, pcmk
) — crmshcrystal
(cr
) — Crystalcsharp
(cs
, c#
) — C#csp
— CSPcss
— CSSd
— Ddart
— Dartdelphi
(dpr
, dfm
, pas
, pascal
) — Delphidiff
(patch
) — Diffdjango
(jinja
) — Djangodns
(bind
, zone
) — DNS Zonedockerfile
(docker
) — Dockerfiledos
(bat
, cmd
) — Batch file (DOS)dsconfig
— undefineddts
— Device Treedust
(dst
) — Dustebnf
— Extended Backus-Naur Formelixir
(ex
, exs
) — Elixirelm
— Elmerb
— ERBerlang
(erl
) — Erlangerlang-repl
— Erlang REPLexcel
(xlsx
, xls
) — Excel formulaefix
— FIXflix
— Flixfortran
(f90
, f95
) — Fortranfsharp
(fs
) — F#gams
(gms
) — GAMSgauss
(gss
) — GAUSSgcode
(nc
) — G-code (ISO 6983)gherkin
(feature
) — Gherkinglsl
— GLSLgml
— GMLgo
(golang
) — Gogolo
— Gologradle
— Gradlegroovy
— Groovyhaml
— HAMLhandlebars
(hbs
, html.hbs
, html.handlebars
, htmlbars
) — Handlebarshaskell
(hs
) — Haskellhaxe
(hx
) — Haxehsp
— HSPhttp
(https
) — HTTPhy
(hylang
) — Hyinform7
(i7
) — Inform 7ini
(toml
) — TOML, also INIirpf90
— IRPF90isbl
— ISBLjava
(jsp
) — Javajavascript
(js
, jsx
, mjs
, cjs
) — Javascriptjboss-cli
(wildfly-cli
) — JBoss CLIjson
— JSONjulia
— Juliajulia-repl
— Julia REPLkotlin
(kt
, kts
) — Kotlinlasso
(ls
, lassoscript
) — Lassolatex
(tex
) — LaTeXldif
— LDIFleaf
— Leafless
— Lesslisp
— Lisplivecodeserver
— LiveCodelivescript
(ls
) — LiveScriptllvm
— LLVM IRlsl
— LSL (Linden Scripting Language)lua
— Luamakefile
(mk
, mak
, make
) — Makefilemarkdown
(md
, mkdown
, mkd
) — Markdownmathematica
(mma
, wl
) — Mathematicamatlab
— Matlabmaxima
— Maximamel
— MELmercury
(m
, moo
) — Mercurymipsasm
(mips
) — MIPS Assemblymizar
— Mizarmojolicious
— Mojoliciousmonkey
— Monkeymoonscript
(moon
) — MoonScriptn1ql
— N1QLnestedtext
(nt
) — Nested Textnginx
(nginxconf
) — Nginx confignim
— Nimnix
(nixos
) — Nixnode-repl
— Node REPLnsis
— NSISobjectivec
(mm
, objc
, obj-c
, obj-c++
, objective-c++
) — Objective-Cocaml
(ml
) — OCamlopenscad
(scad
) — OpenSCADoxygene
— Oxygeneparser3
— Parser3perl
(pl
, pm
) — Perlpf
(pf.conf
) — Packet Filter configpgsql
(postgres
, postgresql
) — PostgreSQLphp
— undefinedphp-template
— PHP templateplaintext
(text
, txt
) — Plain textpony
— Ponypowershell
(pwsh
, ps
, ps1
) — PowerShellprocessing
(pde
) — Processingprofile
— Python profilerprolog
— Prologproperties
— .propertiesprotobuf
— Protocol Bufferspuppet
(pp
) — Puppetpurebasic
(pb
, pbi
) — PureBASICpython
(py
, gyp
, ipython
) — Pythonpython-repl
(pycon
) — undefinedq
(k
, kdb
) — Qqml
(qt
) — QMLr
— Rreasonml
(re
) — ReasonMLrib
— RenderMan RIBroboconf
(graph
, instances
) — Roboconfrouteros
(mikrotik
) — Microtik RouterOS scriptrsl
— RenderMan RSLruby
(rb
, gemspec
, podspec
, thor
, irb
) — Rubyruleslanguage
— Oracle Rules Languagerust
(rs
) — Rustsas
— SASscala
— Scalascheme
— Schemescilab
(sci
) — Scilabscss
— SCSSshell
(console
, shellsession
) — Shell Sessionsmali
— Smalismalltalk
(st
) — Smalltalksml
(ml
) — SML (Standard ML)sqf
— SQFsql
— SQLstan
(stanfuncs
) — Stanstata
(do
, ado
) — Statastep21
(p21
, step
, stp
) — STEP Part 21stylus
(styl
) — Stylussubunit
— SubUnitswift
— Swifttaggerscript
— Tagger Scripttap
— Test Anything Protocoltcl
(tk
) — Tclthrift
— Thrifttp
— TPtwig
(craftcms
) — Twigtypescript
(ts
, tsx
) — TypeScriptvala
— Valavbnet
(vb
) — Visual Basic .NETvbscript
(vbs
) — VBScriptvbscript-html
— VBScript in HTMLverilog
(v
, sv
, svh
) — Verilogvhdl
— VHDLvim
— Vim Scriptwasm
— WebAssemblywren
— Wrenx86asm
— Intel x86 Assemblyxl
(tao
) — XLxml
(html
, xhtml
, rss
, atom
, xjb
, xsd
, xsl
, plist
, wsf
, svg
) — HTML, XMLxquery
(xpath
, xq
) — XQueryyaml
(yml
) — YAMLzephir
(zep
) — Zephiremphasize
— Syntax highlighting in ANSI, for the terminalreact-lowlight
— Syntax highlighter for Reactreact-syntax-highlighter
— React component for syntax highlightingrehype-highlight
— Syntax highlighting in rehyperemark-highlight.js
— Syntax highlighting in remarkjstransformer-lowlight
— Syntax highlighting for JSTransformers
and PugFAQs
Virtual syntax highlighting for virtual DOMs and non-HTML things
The npm package lowlight receives a total of 1,985,344 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.
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.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.