codelight.js 1.2.0 → 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 CHANGED
@@ -1,5 +1,17 @@
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
+
3
15
  ## 1.2.0
4
16
 
5
17
  - 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. |
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,8 +27,9 @@ 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
 
34
35
  ## Quick start
@@ -127,23 +128,30 @@ Everything is available as a named import, on the default export (`import codeli
127
128
 
128
129
  | Function | What it does |
129
130
  |---|---|
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?)`. |
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>`. |
133
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. |
134
- | `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">`. |
135
137
  | `tokenize(code, lang)` | Tokens for each line: `[{ type: "keyword", text: "const" }, …]`. |
136
138
  | `splitFences(markdown)` | Text and code blocks from Markdown that may still be streaming. |
137
139
  | `registerLanguage(name, language, aliases?)` | Adds a language. Build one with `defineLanguage({ keywords, lineComment, strings, … })`. |
138
140
  | `listLanguages()`, `getLanguage(name)` | The languages available. |
139
141
  | `injectTheme()` | Adds the default theme without a `<link>`. |
140
- | `<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
+ ```
141
149
 
142
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)
143
151
 
144
152
  ## Languages
145
153
 
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.
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.
147
155
 
148
156
  ## Themes
149
157
 
@@ -168,7 +176,7 @@ Colours are CSS variables, so your own theme is a few lines:
168
176
  }
169
177
  ```
170
178
 
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`.
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`.
172
180
 
173
181
  ## Project layout
174
182
 
@@ -193,7 +201,7 @@ codelight.js is built for code that's still arriving. For other jobs there are b
193
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.
194
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.
195
203
 
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.
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.
197
205
 
198
206
  ## Contributing
199
207
 
@@ -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