codelight.js 0.0.0-stage → 1.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/CHANGES.md ADDED
@@ -0,0 +1,14 @@
1
+ # Changes
2
+
3
+ ## 1.0.0
4
+
5
+ First release.
6
+
7
+ - Streaming highlighting: `createStream` and `mount` highlight each finished line once and redraw only the line being written.
8
+ - `highlightAll` and `highlightElement` for code that's already on the page.
9
+ - `splitFences` finds code blocks in Markdown that's still streaming, including unclosed and half-typed fences.
10
+ - `<CodeLight>` React component, with server rendering.
11
+ - 19 languages: JavaScript, TypeScript, JSX/TSX, JSON, Python, Bash, Ruby, PHP, Go, Rust, Java, C, C++, C#, SQL, CSS, YAML, HTML/XML and diff.
12
+ - Themes: default (follows the system), light and dark. Every colour is a CSS variable and meets WCAG AA contrast.
13
+ - Browser files `build/codelight.js` and `build/codelight.min.js` with a global `codelight`; ES module (`es/`) and CommonJS (`lib/`) builds.
14
+ - No dependencies, 8.4 KB gzipped.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 CK Giri
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.CDN.md ADDED
@@ -0,0 +1,60 @@
1
+ # codelight.js from a CDN
2
+
3
+ Every npm release is on jsDelivr and unpkg. No build step, no install.
4
+
5
+ ## Files
6
+
7
+ | File | Use |
8
+ |---|---|
9
+ | `build/codelight.min.js` | The script for production. Defines a global `codelight`. 8.4 KB gzipped, all 19 languages. |
10
+ | `build/codelight.js` | The same, unminified, for reading and debugging. |
11
+ | `build/styles/default.min.css` | Default theme: light or dark to match the system. |
12
+ | `build/styles/light.min.css` | Light theme. |
13
+ | `build/styles/dark.min.css` | Dark theme. |
14
+
15
+ Unminified `.css` versions of each theme are next to them.
16
+
17
+ ## jsDelivr
18
+
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>
22
+ ```
23
+
24
+ ## unpkg
25
+
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>
29
+ ```
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`.
32
+
33
+ ## ES module
34
+
35
+ ```html
36
+ <script type="module">
37
+ import codelight from "https://cdn.jsdelivr.net/npm/codelight.js@1.0.0/es/codelight.js";
38
+ codelight.highlightAll();
39
+ </script>
40
+ ```
41
+
42
+ ## Using it
43
+
44
+ Highlight code that's already on the page:
45
+
46
+ ```html
47
+ <pre><code class="language-js">const answer = 42;</code></pre>
48
+ <script>codelight.highlightAll();</script>
49
+ ```
50
+
51
+ Highlight code as it streams in:
52
+
53
+ ```html
54
+ <pre><code id="answer"></code></pre>
55
+ <script>
56
+ const view = codelight.mount(document.getElementById("answer"), { lang: "python" });
57
+ // for each chunk from your API:
58
+ view.write(chunk);
59
+ </script>
60
+ ```
package/README.md CHANGED
@@ -1,3 +1,193 @@
1
- # Temporary Holding Version
1
+ # codelight.js
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ **Syntax highlighting for code that streams in, like AI chat replies.** Each line is highlighted once, when it's finished. Only the line still being written is redrawn, so long answers don't flicker, don't slow down, and text stays selectable while the reply arrives. It works for code that's already on the page, too.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/codelight.js.svg)](https://www.npmjs.com/package/codelight.js)
6
+ [![CI](https://github.com/codelightjs/codelight.js/actions/workflows/ci.yml/badge.svg)](https://github.com/codelightjs/codelight.js/actions/workflows/ci.yml)
7
+ [![gzip size](https://img.shields.io/badge/gzip-8.4%20KB-blue)](https://www.npmjs.com/package/codelight.js)
8
+ [![license](https://img.shields.io/npm/l/codelight.js.svg)](https://github.com/codelightjs/codelight.js/blob/main/LICENSE)
9
+
10
+ **[Live demo](https://codelightjs.github.io/)** · **[Documentation](https://github.com/codelightjs/codelight.js/blob/main/docs/README.md)**
11
+
12
+ <picture>
13
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/codelightjs/codelight.js/main/assets/streaming-dark.png">
14
+ <img alt="An AI chat reply streaming in, with a TypeScript code block highlighted line by line as it arrives" src="https://raw.githubusercontent.com/codelightjs/codelight.js/main/assets/streaming.png">
15
+ </picture>
16
+
17
+ - **Made for streaming.** Each token costs one short line of work, however long the code already is. Re-highlighting the whole block on every token, the usual approach, gets slower as the reply grows.
18
+ - **Copes with half-finished Markdown.** Finds code blocks in a reply that's cut off mid-stream, including a fence that hasn't closed and a closing fence that's only half there.
19
+ - **Tiny, no dependencies.** 8.4 KB gzipped with all 19 languages.
20
+ - **Works anywhere.** Two files from a CDN, an npm package for bundlers, or a React component.
21
+ - **Light and dark themes.** Every colour is a CSS variable, and every colour meets WCAG AA contrast.
22
+
23
+ ## Quick start
24
+
25
+ ### On a web page
26
+
27
+ 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.
28
+
29
+ ```html
30
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/codelight.js@1.0.0/build/styles/default.min.css">
31
+ <script src="https://cdn.jsdelivr.net/npm/codelight.js@1.0.0/build/codelight.min.js"></script>
32
+
33
+ <pre><code class="language-python">print("hello")</code></pre>
34
+
35
+ <script>codelight.highlightAll();</script>
36
+ ```
37
+
38
+ For code that's streaming in, mount a view once and write each chunk as it arrives:
39
+
40
+ ```html
41
+ <pre><code id="answer"></code></pre>
42
+ <script>
43
+ const view = codelight.mount(document.getElementById("answer"), { lang: "ts" });
44
+ view.write(chunk); // call as each chunk arrives
45
+ </script>
46
+ ```
47
+
48
+ See [README.CDN.md](https://github.com/codelightjs/codelight.js/blob/main/README.CDN.md) for every browser file and theme.
49
+
50
+ ### With npm
51
+
52
+ ```sh
53
+ npm install codelight.js
54
+ ```
55
+
56
+ ```js
57
+ import codelight from "codelight.js";
58
+ import "codelight.js/styles/default.css";
59
+
60
+ codelight.highlightAll();
61
+ ```
62
+
63
+ ### React
64
+
65
+ ```tsx
66
+ import { CodeLight } from "codelight.js/react";
67
+ import "codelight.js/styles/default.css";
68
+
69
+ <CodeLight code={partialCode} lang="ts" />
70
+ ```
71
+
72
+ Pass the whole code so far on every update. Only the new part is highlighted. It renders highlighted HTML on the server too.
73
+
74
+ ## A streaming chat reply
75
+
76
+ LLMs send Markdown. `splitFences` splits a reply into text and code blocks at any point mid-stream, and each block's code only grows, so a view never has to start over:
77
+
78
+ ```js
79
+ import { mount, splitFences } from "codelight.js";
80
+
81
+ const views = [];
82
+
83
+ function onChunk(replySoFar) {
84
+ let i = 0;
85
+ for (const part of splitFences(replySoFar)) {
86
+ if (part.type === "text") {
87
+ // render part.text with your Markdown renderer
88
+ continue;
89
+ }
90
+ // part.lang is "" until the ```lang line is complete; part.closed is false while it streams
91
+ if (!views[i] || views[i].lang !== part.lang) views[i] = mount(codeElementFor(i), { lang: part.lang });
92
+ views[i].set(part.code);
93
+ i++;
94
+ }
95
+ }
96
+ ```
97
+
98
+ **With react-markdown**, use `CodeLight` for code blocks:
99
+
100
+ ```tsx
101
+ <ReactMarkdown
102
+ components={{
103
+ code({ className, children }) {
104
+ const lang = /language-(\w+)/.exec(className ?? "")?.[1];
105
+ return lang ? <CodeLight code={String(children)} lang={lang} /> : <code>{children}</code>;
106
+ },
107
+ }}
108
+ >
109
+ {replySoFar}
110
+ </ReactMarkdown>
111
+ ```
112
+
113
+ ## API
114
+
115
+ Everything is available as a named import, on the default export (`import codelight from "codelight.js"`), and on the global `codelight` in the browser build.
116
+
117
+ | Function | What it does |
118
+ |---|---|
119
+ | `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. |
120
+ | `highlightElement(code, lang?)` | Highlights one finished code element in place. |
121
+ | `mount(element, { lang })` | Streams highlighted code into an element. Returns a view with `write(chunk)`, `set(fullText)` and `reset(lang?)`. |
122
+ | `createStream(lang)` | The streaming core, without the DOM. `push(chunk)` returns `{ lines, tail }`: lines finished by this chunk, and the line still being written. |
123
+ | `highlight(code, lang)` | HTML for finished code, to put inside `<pre><code class="cl">`. |
124
+ | `tokenize(code, lang)` | Tokens for each line: `[{ type: "keyword", text: "const" }, …]`. |
125
+ | `splitFences(markdown)` | Text and code blocks from Markdown that may still be streaming. |
126
+ | `registerLanguage(name, language, aliases?)` | Adds a language. Build one with `defineLanguage({ keywords, lineComment, strings, … })`. |
127
+ | `listLanguages()`, `getLanguage(name)` | The languages available. |
128
+ | `injectTheme()` | Adds the default theme without a `<link>`. |
129
+ | `<CodeLight code lang />` | React component, from `codelight.js/react`. Other props go on the `<pre>`. |
130
+
131
+ 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)
132
+
133
+ ## Languages
134
+
135
+ 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.
136
+
137
+ ## Themes
138
+
139
+ | Theme | File |
140
+ |---|---|
141
+ | Default: light or dark to match the system | `styles/default.css` |
142
+ | Light | `styles/light.css` |
143
+ | Dark | `styles/dark.css` |
144
+
145
+ Each has a `.min.css` version. With the default theme, force a mode by adding `cl-light` or `cl-dark` (or `light` / `dark`, or `data-theme="light"` / `"dark"`) to the code or any parent.
146
+
147
+ Colours are CSS variables, so your own theme is a few lines:
148
+
149
+ ```css
150
+ .cl {
151
+ --cl-bg: #1e1e2e;
152
+ --cl-fg: #cdd6f4;
153
+ --cl-keyword: #cba6f7;
154
+ --cl-string: #a6e3a1;
155
+ --cl-comment: #6c7086;
156
+ --cl-function: #89b4fa;
157
+ }
158
+ ```
159
+
160
+ 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`.
161
+
162
+ ## Project layout
163
+
164
+ | Folder | What's in it |
165
+ |---|---|
166
+ | `src/codelight.ts` | The entry point |
167
+ | `src/lib/` | The tokenizer engine, streaming, DOM views, Markdown fences, themes |
168
+ | `src/languages/` | One file per language |
169
+ | `src/styles/` | Themes |
170
+ | `src/react.ts` | The React component |
171
+ | `build/` | Browser files: `codelight.js`, `codelight.min.js`, `styles/` (made by `npm run build`) |
172
+ | `es/`, `lib/` | ES module and CommonJS builds for bundlers and Node |
173
+ | `docs/` | Documentation |
174
+ | `demo/` | The demo page, published at [codelightjs.github.io](https://codelightjs.github.io/) |
175
+ | `tools/` | Build, demo and screenshot scripts |
176
+ | `test/` | Tests |
177
+
178
+ ## When to use something else
179
+
180
+ codelight.js is built for code that's still arriving. For other jobs there are better tools:
181
+
182
+ - **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.
183
+ - **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.
184
+
185
+ 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.
186
+
187
+ ## Contributing
188
+
189
+ See [CONTRIBUTING.md](https://github.com/codelightjs/codelight.js/blob/main/CONTRIBUTING.md). Security issues: [SECURITY.md](https://github.com/codelightjs/codelight.js/blob/main/SECURITY.md).
190
+
191
+ ## License
192
+
193
+ MIT
@@ -0,0 +1,48 @@
1
+ # Supported languages
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.
4
+
5
+ | Language | Name | Aliases |
6
+ |---|---|---|
7
+ | Bash, shell | `bash` | `sh`, `shell`, `zsh`, `shellscript`, `console`, `terminal` |
8
+ | C | `c` | `h` |
9
+ | C++ | `cpp` | `c++`, `cc`, `cxx`, `hpp` |
10
+ | C# | `csharp` | `cs`, `c#` |
11
+ | CSS, SCSS, Less | `css` | `scss`, `less` |
12
+ | Diff, patch | `diff` | `patch` |
13
+ | Go | `go` | `golang` |
14
+ | HTML, XML, SVG | `xml` | `html`, `xhtml`, `svg`, `vue`, `svelte` |
15
+ | Java | `java` | |
16
+ | JavaScript, JSX | `javascript` | `js`, `jsx`, `mjs`, `cjs`, `node` |
17
+ | JSON | `json` | `jsonc`, `json5` |
18
+ | PHP | `php` | |
19
+ | Plain text | `plaintext` | `text`, `txt`, `plain` |
20
+ | Python | `python` | `py`, `python3` |
21
+ | Ruby | `ruby` | `rb` |
22
+ | Rust | `rust` | `rs` |
23
+ | SQL | `sql` | `postgres`, `postgresql`, `mysql`, `sqlite`, `plsql` |
24
+ | TypeScript | `typescript` | `ts`, `mts`, `cts` |
25
+ | TypeScript with JSX | `tsx` | |
26
+ | YAML | `yaml` | `yml` |
27
+
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.
29
+
30
+ An unknown language shows as plain, escaped text.
31
+
32
+ ## Adding a language
33
+
34
+ ```js
35
+ import { defineLanguage, registerLanguage } from "codelight.js";
36
+
37
+ registerLanguage(
38
+ "ini",
39
+ defineLanguage({
40
+ lineComment: [";", "#"],
41
+ strings: [{ open: '"' }],
42
+ rules: [{ match: /^\[[^\]]*\]/, type: "tag" }],
43
+ }),
44
+ ["cfg", "conf"],
45
+ );
46
+ ```
47
+
48
+ To add one to codelight.js itself, see [CONTRIBUTING.md](CONTRIBUTING.md).