codelight.js 1.1.1 → 1.3.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/CHANGES.md +16 -0
- package/README.CDN.md +1 -1
- package/README.md +24 -14
- package/SUPPORTED_LANGUAGES.md +17 -2
- package/build/codelight.js +566 -74
- package/build/codelight.min.js +74 -17
- package/build/styles/dark.css +45 -1
- package/build/styles/dark.min.css +1 -1
- package/build/styles/default.css +62 -6
- package/build/styles/default.min.css +1 -1
- package/build/styles/light.css +46 -2
- package/build/styles/light.min.css +1 -1
- package/es/{chunk-ZYEPC5GO.js → chunk-F46VGBFI.js} +378 -68
- package/es/codelight.d.ts +72 -37
- package/es/codelight.js +193 -8
- package/es/lines-BCdhnSO4.d.ts +8 -0
- package/es/react.d.ts +7 -4
- package/es/react.js +22 -8
- package/lib/{chunk-KY23A6M5.cjs → chunk-HSSBKI63.cjs} +379 -69
- package/lib/codelight.cjs +205 -20
- package/lib/codelight.d.cts +72 -37
- package/lib/lines-BCdhnSO4.d.cts +8 -0
- package/lib/react.cjs +22 -8
- package/lib/react.d.cts +7 -4
- package/package.json +2 -2
package/CHANGES.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# Changes
|
|
2
2
|
|
|
3
|
+
## 1.3.0
|
|
4
|
+
|
|
5
|
+
- **8 new languages:** Kotlin, Swift, Dockerfile, TOML, Markdown, GraphQL, PowerShell and Terraform/HCL. 27 in all.
|
|
6
|
+
- **Code inside strings is highlighted as code:** `${…}` in JavaScript, TypeScript, Kotlin and Terraform; `{…}` in Python f-strings and C# `$"…"` strings; `\(…)` in Swift; `$(…)` in PowerShell.
|
|
7
|
+
- **JavaScript regex literals** are recognised, without mistaking division for a regex.
|
|
8
|
+
- **Bash and Terraform heredocs** (`<<EOF` … `EOF`) are recognised.
|
|
9
|
+
- **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.
|
|
10
|
+
- **Copy button:** `addCopyButton()`, the `copyButton` option, and a `copyButton` prop in React.
|
|
11
|
+
- `highlightAll(options)` can take options without a root.
|
|
12
|
+
- Themes: new `regex`, `line-number`, `mark-bg` and `border` colours. Every colour still meets 4.5:1 contrast, now also on highlighted lines.
|
|
13
|
+
- Size: 12 KB gzipped with all 27 languages.
|
|
14
|
+
|
|
15
|
+
## 1.2.0
|
|
16
|
+
|
|
17
|
+
- README: new introduction in simpler words: syntax highlighting that keeps up with live code.
|
|
18
|
+
|
|
3
19
|
## 1.1.1
|
|
4
20
|
|
|
5
21
|
- npm homepage link now goes to the website, https://codelightjs.github.io/.
|
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`.
|
|
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. |
|
package/README.md
CHANGED
|
@@ -9,24 +9,27 @@
|
|
|
9
9
|
|
|
10
10
|
# codelight.js
|
|
11
11
|
|
|
12
|
-
**Syntax highlighting
|
|
12
|
+
**Syntax highlighting that keeps up with live code.** Each line highlighted once, no flicker, no dependencies.
|
|
13
|
+
|
|
14
|
+
Very fast and lightweight. Only the line being written gets updated, so it stays fast even when the code is long. You can still select and copy text while it changes. It also works for code that's already on the page.
|
|
13
15
|
|
|
14
16
|
[](https://www.npmjs.com/package/codelight.js)
|
|
15
17
|
[](https://github.com/codelightjs/codelight.js/actions/workflows/ci.yml)
|
|
16
|
-
[](https://www.npmjs.com/package/codelight.js)
|
|
17
19
|
[](https://github.com/codelightjs/codelight.js/blob/main/LICENSE)
|
|
18
20
|
|
|
19
21
|
**[Live demo](https://codelightjs.github.io/)** · **[Documentation](https://github.com/codelightjs/codelight.js/blob/main/docs/README.md)**
|
|
20
22
|
|
|
21
23
|
<picture>
|
|
22
24
|
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/codelightjs/codelight.js/main/assets/streaming-dark.png">
|
|
23
|
-
<img alt="
|
|
25
|
+
<img alt="A TypeScript code block being highlighted line by line as it arrives" src="https://raw.githubusercontent.com/codelightjs/codelight.js/main/assets/streaming.png">
|
|
24
26
|
</picture>
|
|
25
27
|
|
|
26
|
-
- **
|
|
27
|
-
- **Copes with half-finished Markdown.** Finds code blocks in
|
|
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
|
+
- **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
|
+
- **Lightweight, no dependencies.** 12 KB gzipped with all 27 languages.
|
|
29
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.
|
|
30
33
|
- **Light and dark themes.** Every colour is a CSS variable, and every colour meets WCAG AA contrast.
|
|
31
34
|
|
|
32
35
|
## Quick start
|
|
@@ -125,23 +128,30 @@ Everything is available as a named import, on the default export (`import codeli
|
|
|
125
128
|
|
|
126
129
|
| Function | What it does |
|
|
127
130
|
|---|---|
|
|
128
|
-
| `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. |
|
|
129
|
-
| `highlightElement(code, lang?)` | Highlights one finished code element in place. |
|
|
130
|
-
| `mount(element, { lang })` |
|
|
131
|
+
| `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. |
|
|
132
|
+
| `highlightElement(code, lang?, options?)` | Highlights one finished code element in place. |
|
|
133
|
+
| `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?)`. |
|
|
134
|
+
| `addCopyButton(code)` | Adds a Copy button to the code's `<pre>`. |
|
|
131
135
|
| `createStream(lang)` | The streaming core, without the DOM. `push(chunk)` returns `{ lines, tail }`: lines finished by this chunk, and the line still being written. |
|
|
132
|
-
| `highlight(code, lang)` | HTML for finished code, to put inside `<pre><code class="cl">`. |
|
|
136
|
+
| `highlight(code, lang, options?)` | HTML for finished code, to put inside `<pre><code class="cl">`. |
|
|
133
137
|
| `tokenize(code, lang)` | Tokens for each line: `[{ type: "keyword", text: "const" }, …]`. |
|
|
134
138
|
| `splitFences(markdown)` | Text and code blocks from Markdown that may still be streaming. |
|
|
135
139
|
| `registerLanguage(name, language, aliases?)` | Adds a language. Build one with `defineLanguage({ keywords, lineComment, strings, … })`. |
|
|
136
140
|
| `listLanguages()`, `getLanguage(name)` | The languages available. |
|
|
137
141
|
| `injectTheme()` | Adds the default theme without a `<link>`. |
|
|
138
|
-
| `<CodeLight code lang />` | React component, from `codelight.js/react`. Other props go on the `<pre>`. |
|
|
142
|
+
| `<CodeLight code lang />` | React component, from `codelight.js/react`. Also takes `lineNumbers`, `highlightLines` and `copyButton`. Other props go on the `<pre>`. |
|
|
143
|
+
|
|
144
|
+
**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.
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
codelight.highlightAll({ lineNumbers: true, copyButton: true });
|
|
148
|
+
```
|
|
139
149
|
|
|
140
150
|
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)
|
|
141
151
|
|
|
142
152
|
## Languages
|
|
143
153
|
|
|
144
|
-
|
|
154
|
+
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.
|
|
145
155
|
|
|
146
156
|
## Themes
|
|
147
157
|
|
|
@@ -166,7 +176,7 @@ Colours are CSS variables, so your own theme is a few lines:
|
|
|
166
176
|
}
|
|
167
177
|
```
|
|
168
178
|
|
|
169
|
-
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`.
|
|
179
|
+
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`.
|
|
170
180
|
|
|
171
181
|
## Project layout
|
|
172
182
|
|
|
@@ -191,7 +201,7 @@ codelight.js is built for code that's still arriving. For other jobs there are b
|
|
|
191
201
|
- **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.
|
|
192
202
|
- **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.
|
|
193
203
|
|
|
194
|
-
codelight.js is the small option: no dependencies, no WebAssembly, any framework, and accurate enough for the
|
|
204
|
+
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.
|
|
195
205
|
|
|
196
206
|
## Contributing
|
|
197
207
|
|
package/SUPPORTED_LANGUAGES.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Supported languages
|
|
2
2
|
|
|
3
|
-
All
|
|
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
|
-
|
|
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
|
|