codelight.js 1.2.0 → 1.3.1

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/CHANGES.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # Changes
2
2
 
3
+ ## 1.3.1
4
+
5
+ - Benchmark against highlight.js, Prism and Shiki: docs/benchmark.md, and npm run bench to run it yourself.
6
+ - CDN links use @1, so they always load the latest 1.x release.
7
+ - Demo: side by side with highlight.js.
8
+
9
+ ## 1.3.0
10
+
11
+ - **8 new languages:** Kotlin, Swift, Dockerfile, TOML, Markdown, GraphQL, PowerShell and Terraform/HCL. 27 in all.
12
+ - **Code inside strings is highlighted as code:** `${…}` in JavaScript, TypeScript, Kotlin and Terraform; `{…}` in Python f-strings and C# `$"…"` strings; `\(…)` in Swift; `$(…)` in PowerShell.
13
+ - **JavaScript regex literals** are recognised, without mistaking division for a regex.
14
+ - **Bash and Terraform heredocs** (`<<EOF` … `EOF`) are recognised.
15
+ - **Line numbers and highlighted lines:** `lineNumbers` and `highlightLines` options, or `data-line-numbers` and `data-highlight-lines` attributes. They work while code streams in, too.
16
+ - **Copy button:** `addCopyButton()`, the `copyButton` option, and a `copyButton` prop in React.
17
+ - `highlightAll(options)` can take options without a root.
18
+ - Themes: new `regex`, `line-number`, `mark-bg` and `border` colours. Every colour still meets 4.5:1 contrast, now also on highlighted lines.
19
+ - Size: 12 KB gzipped with all 27 languages.
20
+
3
21
  ## 1.2.0
4
22
 
5
23
  - README: new introduction in simpler words: syntax highlighting that keeps up with live code.
package/README.CDN.md CHANGED
@@ -6,7 +6,7 @@ Every npm release is on jsDelivr and unpkg. No build step, no install.
6
6
 
7
7
  | File | Use |
8
8
  |---|---|
9
- | `build/codelight.min.js` | The script for production. Defines a global `codelight`. 8.4 KB gzipped, all 19 languages. |
9
+ | `build/codelight.min.js` | The script for production. Defines a global `codelight`. 12 KB gzipped, all 27 languages. |
10
10
  | `build/codelight.js` | The same, unminified, for reading and debugging. |
11
11
  | `build/styles/default.min.css` | Default theme: light or dark to match the system. |
12
12
  | `build/styles/light.min.css` | Light theme. |
@@ -17,24 +17,24 @@ Unminified `.css` versions of each theme are next to them.
17
17
  ## jsDelivr
18
18
 
19
19
  ```html
20
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/codelight.js@1.0.0/build/styles/default.min.css">
21
- <script src="https://cdn.jsdelivr.net/npm/codelight.js@1.0.0/build/codelight.min.js"></script>
20
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/codelight.js@1/build/styles/default.min.css">
21
+ <script src="https://cdn.jsdelivr.net/npm/codelight.js@1/build/codelight.min.js"></script>
22
22
  ```
23
23
 
24
24
  ## unpkg
25
25
 
26
26
  ```html
27
- <link rel="stylesheet" href="https://unpkg.com/codelight.js@1.0.0/build/styles/default.min.css">
28
- <script src="https://unpkg.com/codelight.js@1.0.0/build/codelight.min.js"></script>
27
+ <link rel="stylesheet" href="https://unpkg.com/codelight.js@1/build/styles/default.min.css">
28
+ <script src="https://unpkg.com/codelight.js@1/build/codelight.min.js"></script>
29
29
  ```
30
30
 
31
- Pin a version (`@1.0.0`) in production so an update never changes your page unexpectedly. `https://cdn.jsdelivr.net/npm/codelight.js` with no path also serves `build/codelight.min.js`.
31
+ `@1` always serves the newest 1.x release, which never has breaking changes. To lock an exact version, use it instead, for example `@1.3.1`. `https://cdn.jsdelivr.net/npm/codelight.js@1` with no path also serves `build/codelight.min.js`.
32
32
 
33
33
  ## ES module
34
34
 
35
35
  ```html
36
36
  <script type="module">
37
- import codelight from "https://cdn.jsdelivr.net/npm/codelight.js@1.0.0/es/codelight.js";
37
+ import codelight from "https://cdn.jsdelivr.net/npm/codelight.js@1/es/codelight.js";
38
38
  codelight.highlightAll();
39
39
  </script>
40
40
  ```
package/README.md CHANGED
@@ -15,7 +15,7 @@ Very fast and lightweight. Only the line being written gets updated, so it stays
15
15
 
16
16
  [![npm version](https://img.shields.io/npm/v/codelight.js.svg)](https://www.npmjs.com/package/codelight.js)
17
17
  [![CI](https://github.com/codelightjs/codelight.js/actions/workflows/ci.yml/badge.svg)](https://github.com/codelightjs/codelight.js/actions/workflows/ci.yml)
18
- [![gzip size](https://img.shields.io/badge/gzip-8.4%20KB-blue)](https://www.npmjs.com/package/codelight.js)
18
+ [![gzip size](https://img.shields.io/badge/gzip-12%20KB-blue)](https://www.npmjs.com/package/codelight.js)
19
19
  [![license](https://img.shields.io/npm/l/codelight.js.svg)](https://github.com/codelightjs/codelight.js/blob/main/LICENSE)
20
20
 
21
21
  **[Live demo](https://codelightjs.github.io/)** · **[Documentation](https://github.com/codelightjs/codelight.js/blob/main/docs/README.md)**
@@ -27,10 +27,25 @@ Very fast and lightweight. Only the line being written gets updated, so it stays
27
27
 
28
28
  - **Keeps up with live code.** Each update costs one short line of work, however long the code already is. Re-highlighting the whole block on every update, the usual approach, gets slower as the code grows.
29
29
  - **Copes with half-finished Markdown.** Finds code blocks in Markdown that's still arriving, including a fence that hasn't closed and a closing fence that's only half there.
30
- - **Tiny, no dependencies.** 8.4 KB gzipped with all 19 languages.
30
+ - **Lightweight, no dependencies.** 12 KB gzipped with all 27 languages.
31
31
  - **Works anywhere.** Two files from a CDN, an npm package for bundlers, or a React component.
32
+ - **Line numbers, highlighted lines and a copy button,** built in. Copying never includes the line numbers.
32
33
  - **Light and dark themes.** Every colour is a CSS variable, and every colour meets WCAG AA contrast.
33
34
 
35
+ ## How fast?
36
+
37
+ A real 282-line TypeScript file arriving 4 characters at a time (2,538 updates), in Edge on a 2020 laptop. After each update the page is updated and laid out.
38
+
39
+ | | Whole file, as it arrives | Per update, late in the file | Finished file, once | Size, gzipped |
40
+ |---|---|---|---|---|
41
+ | **codelight.js** | **7.6 s** | **6.0 ms** | **21 ms** | 11.8 KB, 27 languages |
42
+ | highlight.js | 33.9 s | 24 ms | 23 ms | 11.1 KB, 1 language |
43
+ | Prism | 35.3 s | 30 ms | 23 ms | 7.7 KB, 1 language |
44
+ | Shiki | 218 s | 190 ms | 175 ms | 71.6 KB, 1 language |
45
+ | Shiki + `@shikijs/stream` | 9.0 s | 7.0 ms | 174 ms | 72.2 KB, 1 language |
46
+
47
+ About 4.5× faster than highlight.js and Prism on code that's arriving, and about as fast on finished code. Shiki's streaming package comes close on live code, at six times the size. Method, raw numbers and how to run it yourself: [docs/benchmark.md](https://github.com/codelightjs/codelight.js/blob/main/docs/benchmark.md).
48
+
34
49
  ## Quick start
35
50
 
36
51
  ### On a web page
@@ -38,8 +53,8 @@ Very fast and lightweight. Only the line being written gets updated, so it stays
38
53
  Add the stylesheet and the script, then highlight the page. Code blocks need a `language-xxx` class (what Markdown renderers already produce) or a `data-lang` attribute.
39
54
 
40
55
  ```html
41
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/codelight.js@1.0.0/build/styles/default.min.css">
42
- <script src="https://cdn.jsdelivr.net/npm/codelight.js@1.0.0/build/codelight.min.js"></script>
56
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/codelight.js@1/build/styles/default.min.css">
57
+ <script src="https://cdn.jsdelivr.net/npm/codelight.js@1/build/codelight.min.js"></script>
43
58
 
44
59
  <pre><code class="language-python">print("hello")</code></pre>
45
60
 
@@ -127,23 +142,30 @@ Everything is available as a named import, on the default export (`import codeli
127
142
 
128
143
  | Function | What it does |
129
144
  |---|---|
130
- | `highlightAll(root?)` | Highlights every `<pre><code>` in `root` (the whole page by default) that isn't highlighted yet. Waits for the page to load if needed. |
131
- | `highlightElement(code, lang?)` | Highlights one finished code element in place. |
132
- | `mount(element, { lang })` | Streams highlighted code into an element. Returns a view with `write(chunk)`, `set(fullText)` and `reset(lang?)`. |
145
+ | `highlightAll(root?, options?)`, `highlightAll(options)` | Highlights every `<pre><code>` in `root` (the whole page by default) that isn't highlighted yet. Waits for the page to load if needed. |
146
+ | `highlightElement(code, lang?, options?)` | Highlights one finished code element in place. |
147
+ | `mount(element, { lang, … })` | Shows highlighted code in an element and keeps it up to date. Returns a view with `write(chunk)`, `set(fullText)` and `reset(lang?)`. |
148
+ | `addCopyButton(code)` | Adds a Copy button to the code's `<pre>`. |
133
149
  | `createStream(lang)` | The streaming core, without the DOM. `push(chunk)` returns `{ lines, tail }`: lines finished by this chunk, and the line still being written. |
134
- | `highlight(code, lang)` | HTML for finished code, to put inside `<pre><code class="cl">`. |
150
+ | `highlight(code, lang, options?)` | HTML for finished code, to put inside `<pre><code class="cl">`. |
135
151
  | `tokenize(code, lang)` | Tokens for each line: `[{ type: "keyword", text: "const" }, …]`. |
136
152
  | `splitFences(markdown)` | Text and code blocks from Markdown that may still be streaming. |
137
153
  | `registerLanguage(name, language, aliases?)` | Adds a language. Build one with `defineLanguage({ keywords, lineComment, strings, … })`. |
138
154
  | `listLanguages()`, `getLanguage(name)` | The languages available. |
139
155
  | `injectTheme()` | Adds the default theme without a `<link>`. |
140
- | `<CodeLight code lang />` | React component, from `codelight.js/react`. Other props go on the `<pre>`. |
156
+ | `<CodeLight code lang />` | React component, from `codelight.js/react`. Also takes `lineNumbers`, `highlightLines` and `copyButton`. Other props go on the `<pre>`. |
157
+
158
+ **Options:** `lineNumbers: true` numbers the lines; `highlightLines: "2,5-7"` highlights lines; `copyButton: true` adds a Copy button (`highlightAll` and `highlightElement` only; React has its own prop). On a page, `data-line-numbers` and `data-highlight-lines="2,5-7"` on the `<pre>` or `<code>` do the same.
159
+
160
+ ```js
161
+ codelight.highlightAll({ lineNumbers: true, copyButton: true });
162
+ ```
141
163
 
142
164
  Full details: [API reference](https://github.com/codelightjs/codelight.js/blob/main/docs/api.md) · [Streaming guide](https://github.com/codelightjs/codelight.js/blob/main/docs/streaming.md) · [Writing a language](https://github.com/codelightjs/codelight.js/blob/main/docs/languages.md) · [How it works](https://github.com/codelightjs/codelight.js/blob/main/docs/how-it-works.md)
143
165
 
144
166
  ## Languages
145
167
 
146
- 19 languages, including JavaScript, TypeScript, Python, Bash, SQL, HTML and CSS. See [SUPPORTED_LANGUAGES.md](https://github.com/codelightjs/codelight.js/blob/main/SUPPORTED_LANGUAGES.md) for the full list and every alias. An unknown language shows as plain text, safely escaped.
168
+ 27 languages, including JavaScript, TypeScript, Python, Bash, SQL, HTML, CSS, Kotlin, Swift and Terraform. See [SUPPORTED_LANGUAGES.md](https://github.com/codelightjs/codelight.js/blob/main/SUPPORTED_LANGUAGES.md) for the full list and every alias. An unknown language shows as plain text, safely escaped.
147
169
 
148
170
  ## Themes
149
171
 
@@ -168,7 +190,7 @@ Colours are CSS variables, so your own theme is a few lines:
168
190
  }
169
191
  ```
170
192
 
171
- Tokens are `<span class="cl-keyword">`, or `cl-string`, `cl-comment`, `cl-number`, `cl-function`, `cl-type`, `cl-constant`, `cl-variable`, `cl-property`, `cl-operator`, `cl-punctuation`, `cl-tag`, `cl-attr`, `cl-meta`, `cl-inserted`, `cl-deleted`.
193
+ Tokens are `<span class="cl-keyword">`, or `cl-string`, `cl-regex`, `cl-comment`, `cl-number`, `cl-function`, `cl-type`, `cl-constant`, `cl-variable`, `cl-property`, `cl-operator`, `cl-punctuation`, `cl-tag`, `cl-attr`, `cl-meta`, `cl-inserted`, `cl-deleted`.
172
194
 
173
195
  ## Project layout
174
196
 
@@ -193,7 +215,7 @@ codelight.js is built for code that's still arriving. For other jobs there are b
193
215
  - **Docs sites and blogs** with finished code: [Shiki](https://shiki.style) gives VS Code-accurate colours for 200+ languages, and [highlight.js](https://highlightjs.org) or [Prism](https://prismjs.com) cover far more languages.
194
216
  - **A React chat app that already uses Shiki**: [Streamdown](https://streamdown.ai) renders streaming Markdown with Shiki highlighting, and [`@shikijs/stream`](https://shiki.style/packages/stream) streams Shiki itself.
195
217
 
196
- codelight.js is the small option: no dependencies, no WebAssembly, any framework, and accurate enough for the 19 languages chat assistants write most. Its highlighting is rule-based, so unusual syntax can be coloured less precisely than in Shiki.
218
+ codelight.js is the small option: no dependencies, no WebAssembly, any framework, and accurate enough for the 27 languages people write most. Its highlighting is rule-based, so unusual syntax can be coloured less precisely than in Shiki.
197
219
 
198
220
  ## Contributing
199
221
 
@@ -1,6 +1,6 @@
1
1
  # Supported languages
2
2
 
3
- All 19 are in every build. Use the name or any alias as the `lang` option, a `data-lang` attribute, or a `language-xxx` class. Names and aliases aren't case-sensitive.
3
+ All 27 are in every build. Use the name or any alias as the `lang` option, a `data-lang` attribute, or a `language-xxx` class. Names and aliases aren't case-sensitive.
4
4
 
5
5
  | Language | Name | Aliases |
6
6
  |---|---|---|
@@ -10,22 +10,37 @@ All 19 are in every build. Use the name or any alias as the `lang` option, a `da
10
10
  | C# | `csharp` | `cs`, `c#` |
11
11
  | CSS, SCSS, Less | `css` | `scss`, `less` |
12
12
  | Diff, patch | `diff` | `patch` |
13
+ | Dockerfile | `dockerfile` | `docker`, `containerfile` |
13
14
  | Go | `go` | `golang` |
15
+ | GraphQL | `graphql` | `gql` |
14
16
  | HTML, XML, SVG | `xml` | `html`, `xhtml`, `svg`, `vue`, `svelte` |
15
17
  | Java | `java` | |
16
18
  | JavaScript, JSX | `javascript` | `js`, `jsx`, `mjs`, `cjs`, `node` |
17
19
  | JSON | `json` | `jsonc`, `json5` |
20
+ | Kotlin | `kotlin` | `kt`, `kts` |
21
+ | Markdown | `markdown` | `md`, `mdx` |
18
22
  | PHP | `php` | |
19
23
  | Plain text | `plaintext` | `text`, `txt`, `plain` |
24
+ | PowerShell | `powershell` | `ps1`, `pwsh`, `ps` |
20
25
  | Python | `python` | `py`, `python3` |
21
26
  | Ruby | `ruby` | `rb` |
22
27
  | Rust | `rust` | `rs` |
23
28
  | SQL | `sql` | `postgres`, `postgresql`, `mysql`, `sqlite`, `plsql` |
29
+ | Swift | `swift` | |
30
+ | Terraform, HCL | `hcl` | `terraform`, `tf`, `tfvars`, `opentofu` |
31
+ | TOML | `toml` | |
24
32
  | TypeScript | `typescript` | `ts`, `mts`, `cts` |
25
33
  | TypeScript with JSX | `tsx` | |
26
34
  | YAML | `yaml` | `yml` |
27
35
 
28
- HTML highlights JavaScript inside `<script>` and CSS inside `<style>`. Vue and Svelte files are highlighted as HTML, so their templates, scripts and styles all get colour.
36
+ **Code inside strings is highlighted as code:** `${…}` in JavaScript, TypeScript, Kotlin and Terraform; `{…}` in Python f-strings and C# `$"…"` strings; `\(…)` in Swift; `$(…)` in PowerShell.
37
+
38
+ **Other details:**
39
+ - HTML highlights JavaScript inside `<script>` and CSS inside `<style>`. Vue and Svelte files are highlighted as HTML, so their templates, scripts and styles all get colour.
40
+ - JavaScript regex literals are recognised.
41
+ - Bash and Terraform heredocs (`<<EOF` … `EOF`) are recognised.
42
+ - Dockerfile instructions are only coloured at the start of a line, so `RUN npm run build` doesn't colour `run`.
43
+ - In Markdown, the structure (headings, emphasis, links, code) is coloured, and prose stays plain.
29
44
 
30
45
  An unknown language shows as plain, escaped text.
31
46