h.js
v5.3.0 highlight syntax
h.js
2KB JavaScript Syntax Highlighter
Features
- Small. The size is only 2.0KB or 1.4KB if gzipped.
- Dependency free. It needs no other libraries.
- It supports ES2022 syntax.
Install
NPM:
$ npm install h.js
Yarn:
$ yarn add h.js
Usage
const h = require("h.js");
console.log(h("1 + 2"));
// => <span class=v>1</span> <span class=o>+</span> <span class=v>2</span>
API
/**
* `h` highlights JavaScript source code.
*
* @param {string} source - JavaScript source code
* @return {string} - highlighted HTML string
*/
const h = source => ...
It returns an HTML string which contains some <span>
elements having such classes:
<span class=c>...</span>
: comment<span class=d>...</span>
: JSDoc tag (only in comment)<span class=k>...</span>
: keyword<span class=v>...</span>
: value (number literal, boolean and some constants)<span class=s>...</span>
: string literal (also means a template string)<span class=i>...</span>
: interpolation content (only in template string)<span class=r>...</span>
: regexp literal<span class=o>...</span>
: operator symbol<span class=p>...</span>
: punctual symbol<span class=f>...</span>
: function name or private field
License
(C) 2015-2023 TSUYUSATO "MakeNowJust" Kitsune