@speed-highlight/core 1.2.23 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +326 -157
- package/dist/detect.d.ts +7 -0
- package/dist/detect.d.ts.map +1 -1
- package/dist/detect.js +1 -1
- package/dist/index.d.ts +102 -47
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -4
- package/dist/languages/asm.d.ts +24 -4
- package/dist/languages/bash.d.ts +4 -8
- package/dist/languages/bash.js +1 -1
- package/dist/languages/bf.d.ts +10 -2
- package/dist/languages/bf.js +1 -1
- package/dist/languages/c.d.ts +27 -7
- package/dist/languages/c.js +1 -1
- package/dist/languages/css.d.ts +28 -10
- package/dist/languages/css.js +1 -1
- package/dist/languages/csv.d.ts +2 -2
- package/dist/languages/diff.d.ts +12 -3
- package/dist/languages/diff.js +1 -1
- package/dist/languages/docker.d.ts +4 -8
- package/dist/languages/docker.js +1 -1
- package/dist/languages/git.d.ts +18 -2
- package/dist/languages/git.js +1 -1
- package/dist/languages/go.d.ts +28 -3
- package/dist/languages/go.js +1 -1
- package/dist/languages/html.d.ts +30 -19
- package/dist/languages/html.js +1 -1
- package/dist/languages/http.d.ts +24 -3
- package/dist/languages/http.d.ts.map +1 -1
- package/dist/languages/http.js +1 -1
- package/dist/languages/index.d.ts +35 -0
- package/dist/languages/index.d.ts.map +1 -0
- package/dist/languages/index.js +1 -0
- package/dist/languages/ini.d.ts +14 -2
- package/dist/languages/ini.js +1 -1
- package/dist/languages/java.d.ts +28 -3
- package/dist/languages/java.js +1 -1
- package/dist/languages/js.d.ts +61 -4
- package/dist/languages/js.js +1 -1
- package/dist/languages/jsdoc.d.ts +22 -5
- package/dist/languages/jsdoc.d.ts.map +1 -1
- package/dist/languages/jsdoc.js +1 -1
- package/dist/languages/json.d.ts +14 -2
- package/dist/languages/leanpub-md.d.ts +43 -7
- package/dist/languages/leanpub-md.d.ts.map +1 -1
- package/dist/languages/leanpub-md.js +2 -2
- package/dist/languages/log.d.ts +22 -2
- package/dist/languages/lua.d.ts +23 -3
- package/dist/languages/lua.js +1 -1
- package/dist/languages/make.d.ts +33 -3
- package/dist/languages/make.js +1 -1
- package/dist/languages/md.d.ts +22 -6
- package/dist/languages/md.js +2 -2
- package/dist/languages/pl.d.ts +19 -4
- package/dist/languages/pl.js +1 -1
- package/dist/languages/plain.d.ts +1 -1
- package/dist/languages/py.d.ts +30 -5
- package/dist/languages/py.js +1 -1
- package/dist/languages/regex.d.ts +20 -10
- package/dist/languages/regex.d.ts.map +1 -1
- package/dist/languages/regex.js +1 -1
- package/dist/languages/rs.d.ts +28 -3
- package/dist/languages/rs.js +1 -1
- package/dist/languages/sql.d.ts +29 -4
- package/dist/languages/sql.js +1 -1
- package/dist/languages/todo.d.ts +16 -5
- package/dist/languages/todo.d.ts.map +1 -1
- package/dist/languages/todo.js +1 -1
- package/dist/languages/toml.d.ts +33 -3
- package/dist/languages/toml.js +1 -1
- package/dist/languages/ts.d.ts +64 -4
- package/dist/languages/ts.js +1 -1
- package/dist/languages/uri.d.ts +18 -2
- package/dist/languages/uri.js +1 -1
- package/dist/languages/xml.d.ts +21 -23
- package/dist/languages/xml.d.ts.map +1 -1
- package/dist/languages/xml.js +1 -1
- package/dist/languages/yaml.d.ts +28 -3
- package/dist/languages/yaml.js +1 -1
- package/dist/node/detect.d.ts +7 -0
- package/dist/node/detect.d.ts.map +1 -1
- package/dist/node/detect.js +1 -1
- package/dist/node/index.d.ts +102 -47
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +2 -4
- package/dist/node/languages/asm.d.ts +24 -4
- package/dist/node/languages/bash.d.ts +4 -8
- package/dist/node/languages/bash.js +1 -1
- package/dist/node/languages/bf.d.ts +10 -2
- package/dist/node/languages/bf.js +1 -1
- package/dist/node/languages/c.d.ts +27 -7
- package/dist/node/languages/c.js +1 -1
- package/dist/node/languages/css.d.ts +28 -10
- package/dist/node/languages/css.js +1 -1
- package/dist/node/languages/csv.d.ts +2 -2
- package/dist/node/languages/diff.d.ts +12 -3
- package/dist/node/languages/diff.js +1 -1
- package/dist/node/languages/docker.d.ts +4 -8
- package/dist/node/languages/docker.js +1 -1
- package/dist/node/languages/git.d.ts +18 -2
- package/dist/node/languages/git.js +1 -1
- package/dist/node/languages/go.d.ts +28 -3
- package/dist/node/languages/go.js +1 -1
- package/dist/node/languages/html.d.ts +30 -19
- package/dist/node/languages/html.js +1 -1
- package/dist/node/languages/http.d.ts +24 -3
- package/dist/node/languages/http.d.ts.map +1 -1
- package/dist/node/languages/http.js +1 -1
- package/dist/node/languages/index.d.ts +35 -0
- package/dist/node/languages/index.d.ts.map +1 -0
- package/dist/node/languages/index.js +1 -0
- package/dist/node/languages/ini.d.ts +14 -2
- package/dist/node/languages/ini.js +1 -1
- package/dist/node/languages/java.d.ts +28 -3
- package/dist/node/languages/java.js +1 -1
- package/dist/node/languages/js.d.ts +61 -4
- package/dist/node/languages/js.js +1 -1
- package/dist/node/languages/jsdoc.d.ts +22 -5
- package/dist/node/languages/jsdoc.d.ts.map +1 -1
- package/dist/node/languages/jsdoc.js +1 -1
- package/dist/node/languages/json.d.ts +14 -2
- package/dist/node/languages/leanpub-md.d.ts +43 -7
- package/dist/node/languages/leanpub-md.d.ts.map +1 -1
- package/dist/node/languages/leanpub-md.js +2 -2
- package/dist/node/languages/log.d.ts +22 -2
- package/dist/node/languages/lua.d.ts +23 -3
- package/dist/node/languages/lua.js +1 -1
- package/dist/node/languages/make.d.ts +33 -3
- package/dist/node/languages/make.js +1 -1
- package/dist/node/languages/md.d.ts +22 -6
- package/dist/node/languages/md.js +1 -1
- package/dist/node/languages/pl.d.ts +19 -4
- package/dist/node/languages/pl.js +1 -1
- package/dist/node/languages/plain.d.ts +1 -1
- package/dist/node/languages/py.d.ts +30 -5
- package/dist/node/languages/py.js +1 -1
- package/dist/node/languages/regex.d.ts +20 -10
- package/dist/node/languages/regex.d.ts.map +1 -1
- package/dist/node/languages/regex.js +1 -1
- package/dist/node/languages/rs.d.ts +28 -3
- package/dist/node/languages/rs.js +1 -1
- package/dist/node/languages/sql.d.ts +29 -4
- package/dist/node/languages/sql.js +1 -1
- package/dist/node/languages/todo.d.ts +16 -5
- package/dist/node/languages/todo.d.ts.map +1 -1
- package/dist/node/languages/todo.js +1 -1
- package/dist/node/languages/toml.d.ts +33 -3
- package/dist/node/languages/toml.js +1 -1
- package/dist/node/languages/ts.d.ts +64 -4
- package/dist/node/languages/ts.js +1 -1
- package/dist/node/languages/uri.d.ts +18 -2
- package/dist/node/languages/uri.js +1 -1
- package/dist/node/languages/xml.d.ts +21 -23
- package/dist/node/languages/xml.d.ts.map +1 -1
- package/dist/node/languages/xml.js +1 -1
- package/dist/node/languages/yaml.d.ts +28 -3
- package/dist/node/languages/yaml.js +1 -1
- package/dist/node/themes/atom-dark.js +1 -1
- package/dist/node/themes/default.js +1 -1
- package/dist/node/themes/termcolor.d.ts +9 -12
- package/dist/node/themes/termcolor.d.ts.map +1 -1
- package/dist/node/themes/termcolor.js +1 -1
- package/dist/node/tokenize.d.ts +77 -0
- package/dist/node/tokenize.d.ts.map +1 -0
- package/dist/node/tokenize.js +1 -0
- package/dist/themes/atom-dark.css +1 -1
- package/dist/themes/atom-dark.js +1 -1
- package/dist/themes/dark.css +1 -1
- package/dist/themes/default.css +1 -1
- package/dist/themes/default.js +1 -1
- package/dist/themes/github-dark.css +1 -1
- package/dist/themes/github-dim.css +1 -1
- package/dist/themes/github-light.css +1 -1
- package/dist/themes/termcolor.d.ts +9 -12
- package/dist/themes/termcolor.d.ts.map +1 -1
- package/dist/themes/termcolor.js +1 -1
- package/dist/themes/visual-studio-dark.css +1 -1
- package/dist/tokenize.d.ts +77 -0
- package/dist/tokenize.d.ts.map +1 -0
- package/dist/tokenize.js +1 -0
- package/package.json +37 -16
- package/src/detect.js +49 -0
- package/src/index.js +171 -0
- package/src/languages/asm.js +41 -0
- package/src/languages/bash.js +57 -0
- package/src/languages/bf.js +23 -0
- package/src/languages/c.js +56 -0
- package/src/languages/css.js +63 -0
- package/src/languages/csv.js +13 -0
- package/src/languages/diff.js +21 -0
- package/src/languages/docker.js +13 -0
- package/src/languages/git.js +26 -0
- package/src/languages/go.js +36 -0
- package/src/languages/html.js +55 -0
- package/src/languages/http.js +35 -0
- package/src/languages/index.js +39 -0
- package/src/languages/ini.js +26 -0
- package/src/languages/java.js +36 -0
- package/src/languages/js.js +92 -0
- package/src/languages/jsdoc.js +23 -0
- package/src/languages/json.js +24 -0
- package/src/languages/leanpub-md.js +38 -0
- package/src/languages/log.js +32 -0
- package/src/languages/lua.js +32 -0
- package/src/languages/make.js +40 -0
- package/src/languages/md.js +54 -0
- package/src/languages/pl.js +29 -0
- package/src/languages/plain.js +8 -0
- package/src/languages/py.js +57 -0
- package/src/languages/regex.js +25 -0
- package/src/languages/rs.js +37 -0
- package/src/languages/sql.js +38 -0
- package/src/languages/todo.js +23 -0
- package/src/languages/toml.js +41 -0
- package/src/languages/ts.js +18 -0
- package/src/languages/uri.js +30 -0
- package/src/languages/xml.js +82 -0
- package/src/languages/yaml.js +37 -0
- package/src/themes/atom-dark.css +23 -0
- package/src/themes/atom-dark.js +18 -0
- package/src/themes/dark.css +23 -0
- package/src/themes/default.css +72 -0
- package/src/themes/default.js +18 -0
- package/src/themes/github-dark.css +23 -0
- package/src/themes/github-dim.css +3 -0
- package/src/themes/github-light.css +21 -0
- package/src/themes/termcolor.js +9 -0
- package/src/themes/visual-studio-dark.css +23 -0
- package/src/tokenize.js +154 -0
- package/dist/common.d.ts +0 -20
- package/dist/common.d.ts.map +0 -1
- package/dist/common.js +0 -1
- package/dist/languages/js_template_literals.d.ts +0 -21
- package/dist/languages/js_template_literals.d.ts.map +0 -1
- package/dist/languages/js_template_literals.js +0 -1
- package/dist/node/common.d.ts +0 -20
- package/dist/node/common.d.ts.map +0 -1
- package/dist/node/common.js +0 -1
- package/dist/node/languages/js_template_literals.d.ts +0 -21
- package/dist/node/languages/js_template_literals.d.ts.map +0 -1
- package/dist/node/languages/js_template_literals.js +0 -1
- package/dist/node/terminal.d.ts +0 -9
- package/dist/node/terminal.d.ts.map +0 -1
- package/dist/node/terminal.js +0 -2
- package/dist/terminal.d.ts +0 -9
- package/dist/terminal.d.ts.map +0 -1
- package/dist/terminal.js +0 -2
package/README.md
CHANGED
|
@@ -1,214 +1,383 @@
|
|
|
1
1
|
# speed-highlight
|
|
2
2
|
|
|
3
|
-
[](https://
|
|
3
|
+
[](https://www.npmjs.com/package/@speed-highlight/core) [](https://www.npmjs.com/package/@speed-highlight/core)
|
|
4
4
|
|
|
5
|
-
A
|
|
5
|
+
A tiny, fast, simple syntax highlighter for the web and the terminal in JavaScript
|
|
6
6
|
|
|
7
|
-
- **Tiny**
|
|
8
|
-
- **Fast**
|
|
9
|
-
- **Simple**
|
|
7
|
+
- **Tiny** (~1.5 kB gzipped core, ~1 kB gzipped per language)
|
|
8
|
+
- **Fast** (generally outperforms Prism and highlight.js, see the [benchmark](#benchmark))
|
|
9
|
+
- **Simple** (zero dependencies)
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
<a href="https://speed-highlight.github.io/core/examples">Demo</a> |
|
|
13
|
-
<a href="https://github.com/speed-highlight/core/wiki">Wiki</a> |
|
|
14
|
-
<a href="https://speed-highlight.github.io/core/docs">Docs</a>
|
|
15
|
-
</p>
|
|
11
|
+
[**Playground**](https://speed-highlight.github.io)
|
|
16
12
|
|
|
17
13
|

|
|
18
14
|
|
|
19
|
-
|
|
15
|
+
<!-- AUTOGENERATED:TOC:START (run `npm run build` to regenerate) -->
|
|
16
|
+
* [Quick start](#quick-start)
|
|
17
|
+
* [How it works](#how-it-works)
|
|
18
|
+
* [Comparison](#comparison)
|
|
19
|
+
* [Web usage](#web-usage)
|
|
20
|
+
* [In a component](#in-a-component)
|
|
21
|
+
* [On a plain page](#on-a-plain-page)
|
|
22
|
+
* [Detect the language](#detect-the-language)
|
|
23
|
+
* [Control loading and bundling](#control-loading-and-bundling)
|
|
24
|
+
* [CDN (no build step)](#cdn-no-build-step)
|
|
25
|
+
* [Terminal usage](#terminal-usage)
|
|
26
|
+
* [API](#api)
|
|
27
|
+
* [Languages](#languages)
|
|
28
|
+
* [Themes](#themes)
|
|
29
|
+
* [Custom languages](#custom-languages)
|
|
30
|
+
* [Custom themes](#custom-themes)
|
|
31
|
+
* [Migrating from v1](#migrating-from-v1)
|
|
32
|
+
* [Benchmark](#benchmark)
|
|
33
|
+
<!-- AUTOGENERATED:TOC:END -->
|
|
34
|
+
|
|
35
|
+
## Quick start
|
|
20
36
|
|
|
21
|
-
|
|
37
|
+
```bash
|
|
38
|
+
npm i @speed-highlight/core
|
|
39
|
+
```
|
|
22
40
|
|
|
23
|
-
|
|
41
|
+
In a terminal, print the highlighted string:
|
|
24
42
|
|
|
25
|
-
```
|
|
26
|
-
|
|
43
|
+
```js
|
|
44
|
+
import { highlightANSI } from '@speed-highlight/core';
|
|
45
|
+
import theme from '@speed-highlight/core/themes/default.js';
|
|
46
|
+
|
|
47
|
+
console.log(await highlightANSI('console.log("hello")', 'js', theme));
|
|
27
48
|
```
|
|
28
49
|
|
|
29
|
-
In
|
|
50
|
+
In a component, highlight a string and render it:
|
|
51
|
+
|
|
52
|
+
```jsx
|
|
53
|
+
import { useEffect, useState } from 'react';
|
|
54
|
+
import { highlightHTML } from '@speed-highlight/core';
|
|
55
|
+
import '@speed-highlight/core/themes/default.css';
|
|
56
|
+
|
|
57
|
+
export function Code({ code, lang }) {
|
|
58
|
+
const [html, setHtml] = useState('');
|
|
59
|
+
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
// highlightHTML is async (languages load on first use), skip stale results
|
|
62
|
+
let stale = false;
|
|
63
|
+
highlightHTML(code, lang).then(result => { if (!stale) setHtml(result); });
|
|
64
|
+
return () => { stale = true; };
|
|
65
|
+
}, [code, lang]);
|
|
66
|
+
|
|
67
|
+
return <div className={`shj-lang-${lang} shj-block`} dangerouslySetInnerHTML={{ __html: html }} />;
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## How it works
|
|
72
|
+
|
|
73
|
+
The tokenizer runs a language's regex rules over your code and emits typed tokens (`kwd`, `str`, `cmnt`, ...). On the web each token becomes a `<span class="shj-syn-kwd">`; in the terminal it becomes an ANSI escape code. A theme is just CSS (or a token-to-escape map) coloring those names, which is why themes are under 1 kB and writing your own is a few lines.
|
|
74
|
+
|
|
75
|
+
## Comparison
|
|
76
|
+
|
|
77
|
+
Highlighters trade size for grammar fidelity: TextMate engines (Shiki, starry-night) are the most faithful and heaviest, mature regex engines (highlight.js, Prism) sit in the middle, lightweight regex tokenizers (speed-highlight, sugar-high) are the smallest and approximate on exotic syntax.
|
|
78
|
+
|
|
79
|
+
| | Core (gzip) | Per language (gzip) | Languages | Grammar model | Terminal | Status | Choose it for |
|
|
80
|
+
| --- | --- | --- | --- | --- | --- | --- | --- |
|
|
81
|
+
| speed-highlight | 1.5 kB | 0.07–1.6 kB | ~30 | lightweight regex | ✅ built in | ✅ v2 (this repo) | runtime highlighting where size and startup matter |
|
|
82
|
+
| [sugar-high](https://github.com/huozhi/sugar-high) | 1.7 kB | 0.18–2.9 kB | 25 | lightweight regex | ❌ | ✅ v2.0.0 | CSS-variable theming, JSX/TSX-aware JavaScript |
|
|
83
|
+
| [Prism](https://prismjs.com) | 3.1 kB | 0.3–3 kB | ~290 | mature regex | ❌ | ⚠️ v1.30.0, frozen since Mar 2025 (v2 rewrite) | its plugin ecosystem |
|
|
84
|
+
| [highlight.js](https://highlightjs.org) | 8.3 kB | 0.3–2.5 kB | ~190 | mature regex | ❌ (via wrappers) | ✅ v11.11 (Jun 2026) | broad auto-detection and rare languages |
|
|
85
|
+
| [Shiki](https://shiki.style) | 35 kB + engine: 145 kB WASM or 20 kB JS | 5–16 kB | ~220 | TextMate (VS Code) | ✅ via `@shikijs/cli` | ✅ v4.4, very active | highest fidelity (the same grammars as VS Code); zero client JS when run at build time |
|
|
86
|
+
| [starry-night](https://github.com/wooorm/starry-night) | 185 kB incl. WASM | 3–25 kB | 600+ | TextMate (GitHub) | ❌ | ✅ active | GitHub-identical rendering in Node |
|
|
87
|
+
|
|
88
|
+
Sizes are min+gzip, measured from the installed packages at the versions shown (per-language = range over the benchmark corpus; starry-night figures from its own README). Wrappers reuse these engines and inherit their numbers: lowlight/refractor wrap highlight.js/Prism for virtual DOMs, rehype-pretty-code and bright wrap Shiki, and the terminal-only [emphasize](https://github.com/wooorm/emphasize) and [cli-highlight](https://github.com/felixfbecker/cli-highlight) wrap highlight.js grammars into ANSI. Editors (CodeMirror, Monaco, tree-sitter) are a different category.
|
|
89
|
+
|
|
90
|
+
If you highlight at build time and bytes do not matter, use Shiki. speed-highlight's case is the opposite one: highlighting at runtime, where the entire library with all 34 grammars bundled into one file gzips to 9.0 kB, barely more than highlight.js's core alone, before it has loaded a single grammar.
|
|
91
|
+
|
|
92
|
+
## Web usage
|
|
93
|
+
|
|
94
|
+
### In a component
|
|
95
|
+
|
|
96
|
+
Frameworks own their DOM, so highlight the *string* and render it, as in the quick start (mutating a mounted node with `highlightElement` gets wiped on the next render). The output is HTML-escaped (`&`, `<`, `>`), safe to inject even for untrusted code; the `shj-lang-*` and `shj-block` classes hook it into the theme. The same pattern works in Vue, Svelte, and Angular; ready-made components are in [#85](https://github.com/speed-highlight/core/pull/85).
|
|
97
|
+
|
|
98
|
+
### On a plain page
|
|
99
|
+
|
|
100
|
+
Mark code blocks with a `shj-lang-*` class and call `highlightAll` once:
|
|
30
101
|
|
|
31
102
|
```html
|
|
32
|
-
<div class=
|
|
33
|
-
|
|
34
|
-
|
|
103
|
+
<div class="shj-lang-js">console.log('hello')</div>
|
|
104
|
+
<code class="shj-lang-js">inline code</code>
|
|
105
|
+
|
|
106
|
+
<script type="module">
|
|
107
|
+
import { highlightAll } from '@speed-highlight/core';
|
|
108
|
+
highlightAll();
|
|
109
|
+
</script>
|
|
35
110
|
```
|
|
36
111
|
|
|
37
|
-
|
|
112
|
+
Blocks are a single `<div>` instead of `<pre><code>` so the line-number gutter can be laid out inside; the `shj-lang-` prefix avoids colliding with Prism's `language-*` during a migration.
|
|
113
|
+
|
|
114
|
+
For per-element control use `highlightElement`. It renders a `code` element inline and anything else as a block, accepts `block` as an override, and sets `data-lang` so a theme can render a language header with `content: attr(data-lang)`:
|
|
38
115
|
|
|
39
116
|
```js
|
|
40
|
-
import {
|
|
41
|
-
|
|
117
|
+
import { highlightElement } from '@speed-highlight/core';
|
|
118
|
+
|
|
119
|
+
await highlightElement(element, 'js', { showLineNumbers: true });
|
|
42
120
|
```
|
|
43
121
|
|
|
44
|
-
|
|
122
|
+
### Detect the language
|
|
123
|
+
|
|
124
|
+
Detection is a separate ~1 kB import so the core stays small. It recognizes about 20 common languages and returns `'plain'` when unsure:
|
|
45
125
|
|
|
46
126
|
```js
|
|
47
|
-
import { highlightElement } from '
|
|
48
|
-
import { detectLanguage } from '
|
|
127
|
+
import { highlightElement } from '@speed-highlight/core';
|
|
128
|
+
import { detectLanguage } from '@speed-highlight/core/detect';
|
|
49
129
|
|
|
50
|
-
|
|
51
|
-
highlightElement(
|
|
130
|
+
element.textContent = code;
|
|
131
|
+
await highlightElement(element, detectLanguage(code));
|
|
52
132
|
```
|
|
53
133
|
|
|
54
|
-
|
|
134
|
+
### Control loading and bundling
|
|
135
|
+
|
|
136
|
+
Languages load lazily through a loader. Replace it with `setLoader` to add custom languages or restrict what your bundler includes; a name the loader cannot resolve renders as plain text:
|
|
55
137
|
|
|
56
138
|
```js
|
|
57
|
-
import {
|
|
139
|
+
import { setLoader, defaultLoader } from '@speed-highlight/core';
|
|
58
140
|
|
|
59
|
-
|
|
141
|
+
// add custom languages on top of the bundled ones
|
|
142
|
+
setLoader(name => customs[name] ?? defaultLoader(name));
|
|
143
|
+
|
|
144
|
+
// or allow only the languages your bundler can code-split
|
|
145
|
+
setLoader(name => ({
|
|
146
|
+
js: () => import('@speed-highlight/core/languages/js.js'),
|
|
147
|
+
css: () => import('@speed-highlight/core/languages/css.js'),
|
|
148
|
+
})[name]?.());
|
|
60
149
|
```
|
|
61
150
|
|
|
62
|
-
|
|
151
|
+
For full tree-shaking skip the loader entirely: `tokenizeWith` takes every language from the caller, so a bundler keeps only what you import. Include the sub-languages a grammar embeds (`html` uses `css` and `js`; `js` uses `jsdoc`, `todo`, and `regex`). A sub that is not given keeps the type of its rule and only skips the inner highlighting:
|
|
63
152
|
|
|
64
153
|
```js
|
|
65
|
-
import {
|
|
66
|
-
import
|
|
154
|
+
import { tokenizeWith } from '@speed-highlight/core/tokenize';
|
|
155
|
+
import { html, css, js, jsdoc, todo, regex } from '@speed-highlight/core/languages';
|
|
67
156
|
|
|
68
|
-
|
|
157
|
+
tokenizeWith(code, html, (str, type) => { /* ... */ }, { languages: { css, js, jsdoc, todo, regex } });
|
|
69
158
|
```
|
|
70
159
|
|
|
71
|
-
|
|
160
|
+
> [!NOTE]
|
|
161
|
+
> `highlightHTML` and `tokenizeWith` never touch the DOM, so they also run server-side or in a web worker: highlight there and send the string over.
|
|
72
162
|
|
|
73
|
-
|
|
163
|
+
### CDN (no build step)
|
|
74
164
|
|
|
75
165
|
```html
|
|
76
|
-
<link rel="stylesheet" href="https://
|
|
77
|
-
<
|
|
166
|
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@speed-highlight/core@2/dist/themes/default.css">
|
|
167
|
+
<script type="module">
|
|
168
|
+
import { highlightAll } from 'https://cdn.jsdelivr.net/npm/@speed-highlight/core@2/dist/index.js';
|
|
169
|
+
highlightAll();
|
|
170
|
+
</script>
|
|
78
171
|
```
|
|
79
172
|
|
|
173
|
+
## Terminal usage
|
|
174
|
+
|
|
175
|
+
`highlightANSI` returns a string ready to print; the theme is required, import one from `themes/*.js`:
|
|
176
|
+
|
|
80
177
|
```js
|
|
81
|
-
import
|
|
82
|
-
import
|
|
178
|
+
import { highlightANSI } from '@speed-highlight/core';
|
|
179
|
+
import theme from '@speed-highlight/core/themes/atom-dark.js';
|
|
180
|
+
|
|
181
|
+
console.log(await highlightANSI(code, 'js', theme));
|
|
83
182
|
```
|
|
84
183
|
|
|
85
|
-
|
|
184
|
+
A terminal theme is a plain token-to-escape map, built with the `termcolor` helpers or raw escapes:
|
|
86
185
|
|
|
87
|
-
|
|
186
|
+
```js
|
|
187
|
+
import * as col from '@speed-highlight/core/themes/termcolor.js';
|
|
88
188
|
|
|
89
|
-
|
|
189
|
+
export default {
|
|
190
|
+
kwd: col.red,
|
|
191
|
+
str: col.green,
|
|
192
|
+
cmnt: col.gray,
|
|
193
|
+
};
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
For Deno, use the [deno module](https://deno.land/x/speed_highlight_js):
|
|
90
197
|
|
|
91
198
|
```js
|
|
92
|
-
import {
|
|
199
|
+
import { highlightANSI } from 'https://deno.land/x/speed_highlight_js/dist/index.js';
|
|
200
|
+
import theme from 'https://deno.land/x/speed_highlight_js/dist/themes/default.js';
|
|
93
201
|
|
|
94
|
-
await
|
|
95
|
-
printHighlight('console.log("hello")', 'js');
|
|
202
|
+
console.log(await highlightANSI('console.log("hello")', 'js', theme));
|
|
96
203
|
```
|
|
97
204
|
|
|
98
|
-
|
|
205
|
+
## API
|
|
206
|
+
|
|
207
|
+
The main entry covers most apps; reach for `/tokenize` when you want the raw token stream and full control over what gets bundled. Everything ships TypeScript types.
|
|
208
|
+
|
|
209
|
+
| Entry | Export | Description |
|
|
210
|
+
| --- | --- | --- |
|
|
211
|
+
| [`@speed-highlight/core`](src/index.js) | `highlightAll(opt?)` | Highlight every element with a `shj-lang-*` class |
|
|
212
|
+
| | `highlightElement(elm, lang?, opt?)` | Highlight one element (language read from its class by default) |
|
|
213
|
+
| | `highlightHTML(src, lang, opt?)` | Highlight a string, resolves to an HTML string |
|
|
214
|
+
| | `highlightANSI(src, lang, theme)` | Highlight a string, resolves to an ANSI string for terminals |
|
|
215
|
+
| | `tokenize(src, lang, onToken)` | Loader-based tokenizer, calls `onToken(text, type)` |
|
|
216
|
+
| | `setLoader(loader)` / `defaultLoader` | Replace or compose how language names are resolved |
|
|
217
|
+
| [`.../detect`](src/detect.js) | `detectLanguage(code)` | Guess the language, `'plain'` when unsure |
|
|
218
|
+
| [`.../tokenize`](src/tokenize.js) | `tokenizeWith(src, lang, onToken, opt?)`, `tokenizer` | Registry-free synchronous tokenizer (and the underlying generator), languages passed by the caller |
|
|
219
|
+
| [`.../languages`](src/languages/) | one named export per language | Grammars, import only what you need |
|
|
220
|
+
| [`.../themes/*.css`](src/themes/) | | Web themes |
|
|
221
|
+
| [`.../themes/*.js`](src/themes/) | | Terminal themes, plus `termcolor.js` helpers |
|
|
222
|
+
|
|
223
|
+
`lang` is a name (`'js'`) or a grammar object passed directly. `opt` is `{ block?: boolean, showLineNumbers?: boolean }`: line numbers are opt-in, `block` defaults to `true`, except that `highlightElement` and `highlightAll` read it off the element instead, where a `code` element is inline and anything else is a block.
|
|
224
|
+
|
|
225
|
+
## Languages
|
|
226
|
+
|
|
227
|
+
<!-- AUTOGENERATED:LANGUAGES:START (run `npm run build` to regenerate) -->
|
|
228
|
+
| Name | [CSS Class](#web-usage) | Support | Detection | Size (gzip, 14.2 kB total) |
|
|
229
|
+
| --- | --- | --- | --- | --- |
|
|
230
|
+
| Assembly | `shj-lang-asm` | | ✅ | [`194 B`](src/languages/asm.js) |
|
|
231
|
+
| Bash | `shj-lang-bash` | | ✅ | [`430 B`](src/languages/bash.js) |
|
|
232
|
+
| Brainfuck | `shj-lang-bf` | increment, operator, print, comment | ❌ | [`137 B`](src/languages/bf.js) |
|
|
233
|
+
| C | `shj-lang-c` | | ✅ | [`429 B`](src/languages/c.js) |
|
|
234
|
+
| CSS | `shj-lang-css` | comment, str, selector, units, function, ... | ✅ | [`343 B`](src/languages/css.js) |
|
|
235
|
+
| CSV | `shj-lang-csv` | punctuation, ... | ❌ | [`96 B`](src/languages/csv.js) |
|
|
236
|
+
| Diff | `shj-lang-diff` | | ✅ | [`144 B`](src/languages/diff.js) |
|
|
237
|
+
| Dockerfile | `shj-lang-docker` | | ✅ | [`566 B`](src/languages/docker.js) |
|
|
238
|
+
| Git | `shj-lang-git` | comment, insert, deleted, string, ... | ❌ | [`222 B`](src/languages/git.js) |
|
|
239
|
+
| Go | `shj-lang-go` | | ✅ | [`329 B`](src/languages/go.js) |
|
|
240
|
+
| HTML | `shj-lang-html` | | ✅ | [`627 B`](src/languages/html.js) |
|
|
241
|
+
| HTTP | `shj-lang-http` | keywork, string, punctuation, variable, version | ✅ | [`986 B`](src/languages/http.js) |
|
|
242
|
+
| INI | `shj-lang-ini` | | ❌ | [`158 B`](src/languages/ini.js) |
|
|
243
|
+
| Java | `shj-lang-java` | | ✅ | [`457 B`](src/languages/java.js) |
|
|
244
|
+
| JavaScript | `shj-lang-js` | basic syntax, regex, jsdoc, json, template literals | ⛔ reported as TypeScript | [`758 B`](src/languages/js.js) |
|
|
245
|
+
| JSDoc | `shj-lang-jsdoc` | | ❌ | [`247 B`](src/languages/jsdoc.js) |
|
|
246
|
+
| JSON | `shj-lang-json` | string, number, bool, ... | ❌ | [`172 B`](src/languages/json.js) |
|
|
247
|
+
| LeanPub Markdown | `shj-lang-leanpub-md` | | ❌ | [`1.2 kB`](src/languages/leanpub-md.js) |
|
|
248
|
+
| Log | `shj-lang-log` | number, string, comment, errors | ❌ | [`223 B`](src/languages/log.js) |
|
|
249
|
+
| Lua | `shj-lang-lua` | | ✅ | [`273 B`](src/languages/lua.js) |
|
|
250
|
+
| Makefile | `shj-lang-make` | | ✅ | [`223 B`](src/languages/make.js) |
|
|
251
|
+
| Markdown | `shj-lang-md` | | ✅ | [`1.1 kB`](src/languages/md.js) |
|
|
252
|
+
| Perl | `shj-lang-pl` | | ✅ | [`329 B`](src/languages/pl.js) |
|
|
253
|
+
| Plain text | `shj-lang-plain` | | ❌ | [`71 B`](src/languages/plain.js) |
|
|
254
|
+
| Python | `shj-lang-py` | | ✅ | [`416 B`](src/languages/py.js) |
|
|
255
|
+
| Regex | `shj-lang-regex` | count, set, ... | ❌ | [`172 B`](src/languages/regex.js) |
|
|
256
|
+
| Rust | `shj-lang-rs` | | ✅ | [`414 B`](src/languages/rs.js) |
|
|
257
|
+
| SQL | `shj-lang-sql` | number, string, function, ... | ✅ | [`1.7 kB`](src/languages/sql.js) |
|
|
258
|
+
| TODO | `shj-lang-todo` | | ❌ | [`185 B`](src/languages/todo.js) |
|
|
259
|
+
| TOML | `shj-lang-toml` | comment, table, string, bool, variable | ❌ | [`236 B`](src/languages/toml.js) |
|
|
260
|
+
| TypeScript | `shj-lang-ts` | js syntax, ts keyword, types | ✅ | [`849 B`](src/languages/ts.js) |
|
|
261
|
+
| URI | `shj-lang-uri` | | ✅ | [`176 B`](src/languages/uri.js) |
|
|
262
|
+
| XML | `shj-lang-xml` | | ✅ | [`511 B`](src/languages/xml.js) |
|
|
263
|
+
| YAML | `shj-lang-yaml` | comment, numbers, variable, string, bool | ✅ | [`208 B`](src/languages/yaml.js) |
|
|
264
|
+
<!-- AUTOGENERATED:LANGUAGES:END -->
|
|
265
|
+
|
|
266
|
+
## Themes
|
|
267
|
+
|
|
268
|
+
<!-- AUTOGENERATED:THEMES:START (run `npm run build` to regenerate) -->
|
|
269
|
+
| Name | Terminal (gzip) | Web (gzip) |
|
|
270
|
+
| --- | --- | --- |
|
|
271
|
+
| `default` | ✅ [`174 B`](src/themes/default.js) | ✅ [`603 B`](src/themes/default.css) |
|
|
272
|
+
| `atom-dark` | ✅ [`174 B`](src/themes/atom-dark.js) | ✅ [`699 B`](src/themes/atom-dark.css) |
|
|
273
|
+
| `dark` | ❌ | ✅ [`695 B`](src/themes/dark.css) |
|
|
274
|
+
| `github-dark` | ❌ | ✅ [`691 B`](src/themes/github-dark.css) |
|
|
275
|
+
| `github-dim` | ❌ | ✅ [`700 B`](src/themes/github-dim.css) |
|
|
276
|
+
| `github-light` | ❌ | ✅ [`672 B`](src/themes/github-light.css) |
|
|
277
|
+
| `visual-studio-dark` | ❌ | ✅ [`694 B`](src/themes/visual-studio-dark.css) |
|
|
278
|
+
<!-- AUTOGENERATED:THEMES:END -->
|
|
279
|
+
|
|
280
|
+
## Custom languages
|
|
281
|
+
|
|
282
|
+
A language is an array of rules. Every rule's regex (global flag required) is tried; the earliest match in the string wins, ties go to the earlier rule:
|
|
99
283
|
|
|
100
|
-
|
|
284
|
+
```js
|
|
285
|
+
export default [
|
|
286
|
+
{ match: /\/\/.*/g, type: 'cmnt' },
|
|
287
|
+
{ expand: 'str' },
|
|
288
|
+
{ expand: 'num' },
|
|
289
|
+
{ match: /\b(if|else|for|while|return)\b/g, type: 'kwd' },
|
|
290
|
+
];
|
|
291
|
+
```
|
|
101
292
|
|
|
102
|
-
|
|
293
|
+
- `{ match, type }` tags what the regex matches with a token type
|
|
294
|
+
- `{ expand }` reuses a shared pattern: `'num'`, `'str'`, or `'strDouble'`
|
|
295
|
+
- `{ match, sub }` re-tokenizes the matched region with another language: a name (loaded through the loader), an inline grammar array, or a function `code => name | grammar` deciding per match
|
|
103
296
|
|
|
104
|
-
|
|
105
|
-
npm i @speed-highlight/core
|
|
106
|
-
```
|
|
297
|
+
A language can also set a default token for unmatched text by exporting `{ type, sub }` instead of a bare array (see [`http.js`](src/languages/http.js)). Use a grammar by passing it directly as `lang`, or register a name with `setLoader`. To extend an existing language, spread it after your rules:
|
|
107
298
|
|
|
108
299
|
```js
|
|
109
|
-
|
|
300
|
+
import js from '@speed-highlight/core/languages/js.js';
|
|
301
|
+
|
|
302
|
+
export default [
|
|
303
|
+
{ match: /\b(signal|effect)\b/g, type: 'func' },
|
|
304
|
+
...js,
|
|
305
|
+
];
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
Token types:
|
|
309
|
+
|
|
310
|
+
| Token | Used for | | Token | Used for | | Token | Used for |
|
|
311
|
+
| --- | --- | --- | --- | --- | --- | --- | --- |
|
|
312
|
+
| `kwd` | keywords | | `type` | types | | `esc` | escape sequences |
|
|
313
|
+
| `str` | strings | | `class` | classes | | `section` | section delimiters |
|
|
314
|
+
| `num` | numbers | | `var` | variables | | `insert` | inserted parts (diff) |
|
|
315
|
+
| `cmnt` | comments | | `oper` | operators | | `deleted` | deleted parts (diff) |
|
|
316
|
+
| `func` | functions | | `bool` | booleans | | `err` | errors |
|
|
317
|
+
|
|
318
|
+
Missing a language? [Open an issue](https://github.com/speed-highlight/core/issues) or send a PR adding a file to [`src/languages/`](src/languages/).
|
|
319
|
+
|
|
320
|
+
## Custom themes
|
|
321
|
+
|
|
322
|
+
A web theme colors the token classes; start from [`default.css`](src/themes/default.css) and override:
|
|
323
|
+
|
|
324
|
+
```css
|
|
325
|
+
[class*="shj-lang-"] { color: #f8f8f2; background: #282a36; }
|
|
326
|
+
.shj-syn-kwd { color: #ff79c6; }
|
|
327
|
+
.shj-syn-str, .shj-syn-insert { color: #50fa7b; }
|
|
328
|
+
.shj-syn-cmnt { color: #6272a4; font-style: italic; }
|
|
329
|
+
.shj-numbers { color: #6272a4; }
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
Display-mode hooks: `.shj-inline` (inside `code`), `.shj-block`, and `.shj-numbers` for the gutter. Terminal themes are the token-to-escape maps shown in [Terminal usage](#terminal-usage).
|
|
333
|
+
|
|
334
|
+
## Migrating from v1
|
|
335
|
+
|
|
336
|
+
| v1 | v2 |
|
|
337
|
+
| --- | --- |
|
|
338
|
+
| `highlightText(src, lang)` | `highlightHTML(src, lang)` |
|
|
339
|
+
| `printHighlight(src, lang)` from `/terminal` | `console.log(await highlightANSI(src, lang, theme))` |
|
|
340
|
+
| `setTheme('atom-dark')` | pass the theme: `highlightANSI(src, lang, theme)` |
|
|
341
|
+
| `loadLanguage(name, grammar)` | `setLoader(...)` or pass the grammar directly as `lang` |
|
|
342
|
+
| `@speed-highlight/core/terminal` entry | merged into `@speed-highlight/core` |
|
|
343
|
+
| `common.js` shared patterns | `{ expand: 'num' \| 'str' \| 'strDouble' }` built into the tokenizer |
|
|
344
|
+
| `{ hideLineNumbers: true }` | now the default, line numbers are opt-in with `{ showLineNumbers: true }` |
|
|
345
|
+
| `oneline` display mode | removed, a `div` is always a block |
|
|
346
|
+
| `highlightElement(elm, lang, mode, opt)` | the mode moved into the options: `highlightElement(elm, lang, { block })` |
|
|
347
|
+
| `shj-multiline` class | `shj-block` |
|
|
348
|
+
|
|
349
|
+
## Benchmark
|
|
350
|
+
|
|
351
|
+
<!-- AUTOGENERATED:BENCHMARK:START (run `npm run benchmark -- --write` to regenerate) -->
|
|
352
|
+
```rb
|
|
353
|
+
$ npm run benchmark
|
|
354
|
+
node v26.7.0, darwin arm64, Apple M4
|
|
355
|
+
corpus: js, css, json, md, sql, py, bash, tiled to 3 sizes (tiny (1 KB) / medium (16 KB) / huge (128 KB)), median of 9 trials per language, averaged across the corpus
|
|
356
|
+
|
|
357
|
+
tiny (1 KB) medium (16 KB) huge (128 KB)
|
|
358
|
+
speed-highlight 1,144,010 ops/min 74,720 ops/min 8,583 ops/min
|
|
359
|
+
prismjs 655,152 ops/min 34,310 ops/min 2,575 ops/min
|
|
360
|
+
highlight.js 595,113 ops/min 48,666 ops/min 5,473 ops/min
|
|
361
|
+
sugar-high 238,406 ops/min 13,405 ops/min 1,376 ops/min
|
|
362
|
+
shiki (js engine) 89,925 ops/min 6,682 ops/min 841 ops/min
|
|
363
|
+
|
|
364
|
+
cold start (import + first highlight of test.js):
|
|
365
|
+
speed-highlight 7.3 ms
|
|
366
|
+
prismjs 8.6 ms
|
|
367
|
+
highlight.js 19 ms
|
|
368
|
+
sugar-high 18 ms
|
|
369
|
+
shiki (js engine) 169 ms
|
|
370
|
+
|
|
371
|
+
speed-highlight per language (warm, median of 9 trials):
|
|
372
|
+
tiny (1 KB) medium (16 KB) huge (128 KB)
|
|
373
|
+
js 666,642 ops/min 40,773 ops/min 4,704 ops/min
|
|
374
|
+
css 1,006,895 ops/min 62,774 ops/min 7,284 ops/min
|
|
375
|
+
json 1,964,840 ops/min 127,890 ops/min 15,057 ops/min
|
|
376
|
+
md 1,080,739 ops/min 86,883 ops/min 10,453 ops/min
|
|
377
|
+
sql 1,369,247 ops/min 88,513 ops/min 10,180 ops/min
|
|
378
|
+
py 974,179 ops/min 56,165 ops/min 6,260 ops/min
|
|
379
|
+
bash 945,526 ops/min 60,045 ops/min 6,141 ops/min
|
|
380
|
+
```
|
|
381
|
+
<!-- AUTOGENERATED:BENCHMARK:END -->
|
|
110
382
|
|
|
111
|
-
|
|
112
|
-
printHighlight('console.log("hello")', 'js');
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
## Migrating from prism
|
|
116
|
-
|
|
117
|
-
speed-highlight is a lighter and faster version of prism that share a similar API
|
|
118
|
-
|
|
119
|
-
### Style
|
|
120
|
-
|
|
121
|
-
Remove the prism stylesheet in the head of your html file
|
|
122
|
-
Clone this repository or use a cdn to load our stylesheet
|
|
123
|
-
|
|
124
|
-
```diff
|
|
125
|
-
<head>
|
|
126
|
-
- <link href="themes/prism.css" rel="stylesheet" />
|
|
127
|
-
+ <link rel="stylesheet" href="https://unpkg.com/@speed-highlight/core/dist/themes/default.css">
|
|
128
|
-
</head>
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
### Script
|
|
132
|
-
|
|
133
|
-
For the script part remove the prism.js script and replace it by a import and a call to `highlightAll`
|
|
134
|
-
|
|
135
|
-
```diff
|
|
136
|
-
<body>
|
|
137
|
-
- <script src="prism.js"></script>
|
|
138
|
-
+<script>
|
|
139
|
-
+ import { highlightAll } from 'https://unpkg.com/@speed-highlight/core/dist/index.js';
|
|
140
|
-
+ highlightAll();
|
|
141
|
-
+</script>
|
|
142
|
-
</body>
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
If you want to highlight only a specific element you can use the `highlightElement` function instead
|
|
146
|
-
|
|
147
|
-
### Code block
|
|
148
|
-
|
|
149
|
-
For the code blocks replace the `<pre><code>` by only one `<div>`
|
|
150
|
-
And use `shj-lang-` prefix instead of `language-` for the class property
|
|
151
|
-
|
|
152
|
-
```diff
|
|
153
|
-
-<pre><code class="language-css">p { color: red }</code></pre>
|
|
154
|
-
+<div class="shj-lang-css">p { color: red }</div>
|
|
155
|
-
```
|
|
156
|
-
|
|
157
|
-
And for inline code block you just have to change the class property
|
|
158
|
-
|
|
159
|
-
```diff
|
|
160
|
-
-<code class="language-css">p { color: red }</code>
|
|
161
|
-
+<code class="shj-lang-css">p { color: red }</code>
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
## Languages supported 🌐
|
|
165
|
-
|
|
166
|
-
| Name | Class name | Support | Language detection |
|
|
167
|
-
| ---------- | ------------------- | --------------------------------------------------- | ------------------ |
|
|
168
|
-
| asm | shj-lang-asm | | ✅ |
|
|
169
|
-
| bash | shj-lang-bash | | ✅ |
|
|
170
|
-
| brainfuck | shj-lang-bf | increment, operator, print, comment | ❌ |
|
|
171
|
-
| c | shj-lang-c | | ✅ |
|
|
172
|
-
| css | shj-lang-css | comment, str, selector, units, function, ... | ✅ |
|
|
173
|
-
| csv | shj-lang-csv | punctuation, ... | ❌ |
|
|
174
|
-
| diff | shj-lang-diff | | ✅ |
|
|
175
|
-
| docker | shj-lang-docker | | ✅ |
|
|
176
|
-
| git | shj-lang-git | comment, insert, deleted, string, ... | ❌ |
|
|
177
|
-
| go | shj-lang-go | | ✅ |
|
|
178
|
-
| html | shj-lang-html | | ✅ |
|
|
179
|
-
| http | shj-lang-http | keywork, string, punctuation, variable, version | ✅ |
|
|
180
|
-
| ini | shj-lang-ini | | ❌ |
|
|
181
|
-
| java | shj-lang-java | | ✅ |
|
|
182
|
-
| javascipt | shj-lang-js | basic syntax, regex, jsdoc, json, template literals | ✅ |
|
|
183
|
-
| jsdoc | shj-lang-jsdoc | | ❌ |
|
|
184
|
-
| json | shj-lang-json | string, number, bool, ... | ❌ |
|
|
185
|
-
| leanpub-md | shj-lang-leanpub-md | | ❌ |
|
|
186
|
-
| log | shj-lang-log | number, string, comment, errors | ❌ |
|
|
187
|
-
| lua | shj-lang-lua | | ✅ |
|
|
188
|
-
| makefile | shj-lang-make | | ✅ |
|
|
189
|
-
| markdown | shj-lang-md | | ✅ |
|
|
190
|
-
| perl | shj-lang-pl | | ✅ |
|
|
191
|
-
| plain | shj-lang-plain | | ❌ |
|
|
192
|
-
| python | shj-lang-py | | ✅ |
|
|
193
|
-
| regex | shj-lang-regex | count, set, ... | ❌ |
|
|
194
|
-
| rust | shj-lang-rs | | ✅ |
|
|
195
|
-
| sql | shj-lang-sql | number, string, function, ... | ✅ |
|
|
196
|
-
| todo | shj-lang-todo | | ❌ |
|
|
197
|
-
| toml | shj-lang-toml | comment, table, string, bool, variable | ❌ |
|
|
198
|
-
| typescript | shj-lang-ts | js syntax, ts keyword, types | ✅ |
|
|
199
|
-
| uri | shj-lang-uri | | ✅ |
|
|
200
|
-
| xml | shj-lang-xml | | ✅ |
|
|
201
|
-
| yaml | shj-lang-yaml | comment, numbers, variable, string, bool | ❌ |
|
|
202
|
-
|
|
203
|
-
## Themes 🌈
|
|
204
|
-
|
|
205
|
-
A modern theme by default
|
|
206
|
-
|
|
207
|
-
| Name | Terminal | Web |
|
|
208
|
-
| ------------------- | -------- | --- |
|
|
209
|
-
| default | ✅ | ✅ |
|
|
210
|
-
| github-dark | ❌ | ✅ |
|
|
211
|
-
| github-light | ❌ | ✅ |
|
|
212
|
-
| github-dim | ❌ | ✅ |
|
|
213
|
-
| atom-dark | ✅ | ✅ |
|
|
214
|
-
| visual-studio-dark | ❌ | ✅ |
|
|
383
|
+
Identical inputs from [`examples/languages/`](examples/languages/), tiled up to each size bucket, HTML-string output for every library, one op = one highlighted file. Each figure is the median of 9 repeated trials, reported as ops/min (the huge bucket can drop below 1 op/sec for the slower libraries). Warm runs have grammars preloaded; cold start is import plus first highlight, measured once (not size-swept). Shiki does more work by design (see [Comparison](#comparison)).
|
package/dist/detect.d.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Try to find the language the given code belong to
|
|
3
|
+
*
|
|
4
|
+
* @param {string} code The code
|
|
5
|
+
* @returns {ShjLanguage} The language of the code
|
|
6
|
+
*/
|
|
1
7
|
export function detectLanguage(code: string): ShjLanguage;
|
|
2
8
|
export type ShjLanguage = import("./index.js").ShjLanguage;
|
|
9
|
+
export type ShjBuiltinLanguage = import("./index.js").ShjBuiltinLanguage;
|
|
3
10
|
//# sourceMappingURL=detect.d.ts.map
|
package/dist/detect.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"detect.d.ts","sourceRoot":"","sources":["../src/detect.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"detect.d.ts","sourceRoot":"","sources":["../src/detect.js"],"names":[],"mappings":"AAkCA;;;;;GAKG;AACH,qCAHW,MAAM,GACJ,WAAW,CAUvB;0BA/CY,OAAO,YAAY,EAAE,WAAW;iCAChC,OAAO,YAAY,EAAE,kBAAkB"}
|
package/dist/detect.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var m=[[
|
|
1
|
+
var m={bash:[[/#!(\/usr)?\/bin\/bash/g,500],[/\b(if|elif|then|fi|echo)\b|\$/g,10]],html:[[/<\/?[a-z-]+[^\n>]*>/g,10],[/^\s+<!DOCTYPE\s+html/g,500]],http:[[/^(GET|HEAD|POST|PUT|DELETE|PATCH|HTTP)\b/g,500]],ts:[[/\b(console|await|async|function|export|import|this|class|for|let|const|map|join|require|document|window|implements|interface|namespace|true|false|null)\b/g,10]],py:[[/\b(def|print|await|async|class|and|or|lambda|import|from|self|asyncio|pass|True|False|None|__init__)\b/g,10]],sql:[[/\b(SELECT|INSERT|FROM)\b/g,50]],pl:[[/#!(\/usr)?\/bin\/perl/g,500],[/\b(use|print)\b|\$/g,10]],lua:[[/#!(\/usr)?\/bin\/lua/g,500]],make:[[/\b(ifneq|endif|if|elif|then|fi|echo|.PHONY|^[a-z]+ ?:$)\b|\$/gm,10]],uri:[[/https?:|mailto:|tel:|ftp:/g,30]],css:[[/^(@import|@page|@media|(\.|#)[a-z]+)/gm,20]],diff:[[/^[+><-]/gm,10],[/^@@ ?[-+,0-9 ]+ ?@@/gm,25]],md:[[/^(>|\t\*|\t\d+.)/gm,10],[/\[.*\](.*)/g,10]],docker:[[/^(FROM|ENTRYPOINT|RUN)/gm,500]],xml:[[/<\/?[a-z-]+[^\n>]*>/g,10],[/^<\?xml/g,500]],c:[[/#include\b|\bprintf\s+\(/g,100]],rs:[[/^\s+(use|fn|mut|match)\b/gm,100]],go:[[/\b(func|fmt|package)\b/g,100]],java:[[/^import\s+java/gm,500]],asm:[[/^(section|global main|extern|\t(call|mov|ret))/gm,100]],yaml:[[/^(\s+)?[a-z][a-z0-9]*:/gmi,10]]};function g(t){return Object.entries(m).map(([a,e])=>[a,e.reduce((n,[i,s])=>n+[...t.matchAll(i)].length*s,0)]).filter(([a,e])=>e>20).sort((a,e)=>e[1]-a[1])[0]?.[0]||"plain"}export{g as detectLanguage};
|