H.js Save

2KB JavaScript Syntax Highlighter

Project README

h.js

2KB JavaScript Syntax Highlighter

Version MIT License SUSHI-WARE License

Features

  • Small. The size is only 2.0KB or 1.4KB if gzipped.
  • Dependency free. It needs no other libraries.
  • It supports ES2021 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

License

MIT and 🍣

(C) 2015-2022 TSUYUSATO "MakeNowJust" Kitsune

Open Source Agenda is not affiliated with "H.js" Project. README Source: makenowjust/h.js