codecolor.js
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

Package logo

Build Status npm Standard Shared Config

Fast, small and simple syntax highlighter library.

Install

npm install codecolor.js

Cautions:

  • Regular expressions of language files use "Lookbehind Assertions" (see proposal-regexp-lookbehind).
  • Due to the lack of a PCRE recursive parameter (?R), nested structures (for example "Template literals") have a maximum nesting equal to two.

Usage

import CodeColor from 'codecolor.js';
import css from 'codecolor.js/languages/css';
import javascript from 'codecolor.js/javascript';
import json from 'codecolor.js/languages/json';

const codecolor = new CodeColor();

codecolor.register([css, javascript, json]);

// source code
const code = '...';
const html = codecolor.highlight(code, javascript.name);

// highlighted code
console.log(html);

/*
<pre>
  <code class="cc-container">
    ...
  </code>
</pre>
*/

Benchmark

npm run benchmark

---

Test perf:
- css:
codecolor.js x 3,193 ops/sec ±0.28% (95 runs sampled)
Prism.js x 2,931 ops/sec ±0.36% (96 runs sampled)
highlight.js x 849 ops/sec ±0.80% (95 runs sampled)
# Fastest is codecolor.js

- javascript:
codecolor.js x 4,782 ops/sec ±0.48% (96 runs sampled)
Prism.js x 3,031 ops/sec ±0.36% (96 runs sampled)
highlight.js x 1,275 ops/sec ±0.40% (96 runs sampled)
# Fastest is codecolor.js

- json:
codecolor.js x 6,743 ops/sec ±1.02% (95 runs sampled)
Prism.js x 4,243 ops/sec ±0.36% (95 runs sampled)
highlight.js x 1,468 ops/sec ±0.81% (96 runs sampled)
# Fastest is codecolor.js

---

OS: Ubuntu 21.10 x86_64
DE: GNOME 40.5
Terminal: tilix
CPU: Intel i9-9900 (16) @ 5.000GHz
GPU: NVIDIA GeForce RTX 2070
Memory: 32019MiB

API

Read the API documentation for more information.

Package Sidebar

Install

npm i codecolor.js

Weekly Downloads

4

Version

2.0.0

License

MIT

Unpacked Size

39.3 kB

Total Files

24

Last publish

Collaborators

  • keindev