Syntax highlighting that keeps up with live code. Each line highlighted once, no flicker, no dependencies.
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.
- 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.
- 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.
- Tiny, no dependencies. 8.4 KB gzipped with all 19 languages.
- Works anywhere. Two files from a CDN, an npm package for bundlers, or a React component.
- Light and dark themes. Every colour is a CSS variable, and every colour meets WCAG AA contrast.
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.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/codelight.js@1.0.0/build/styles/default.min.css">
<script src="https://cdn.jsdelivr.net/npm/codelight.js@1.0.0/build/codelight.min.js"></script>
<pre><code class="language-python">print("hello")</code></pre>
<script>codelight.highlightAll();</script>For code that's streaming in, mount a view once and write each chunk as it arrives:
<pre><code id="answer"></code></pre>
<script>
const view = codelight.mount(document.getElementById("answer"), { lang: "ts" });
view.write(chunk); // call as each chunk arrives
</script>See README.CDN.md for every browser file and theme.
npm install codelight.jsimport codelight from "codelight.js";
import "codelight.js/styles/default.css";
codelight.highlightAll();import { CodeLight } from "codelight.js/react";
import "codelight.js/styles/default.css";
<CodeLight code={partialCode} lang="ts" />Pass the whole code so far on every update. Only the new part is highlighted. It renders highlighted HTML on the server too.
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:
import { mount, splitFences } from "codelight.js";
const views = [];
function onChunk(replySoFar) {
let i = 0;
for (const part of splitFences(replySoFar)) {
if (part.type === "text") {
// render part.text with your Markdown renderer
continue;
}
// part.lang is "" until the ```lang line is complete; part.closed is false while it streams
if (!views[i] || views[i].lang !== part.lang) views[i] = mount(codeElementFor(i), { lang: part.lang });
views[i].set(part.code);
i++;
}
}With react-markdown, use CodeLight for code blocks:
<ReactMarkdown
components={{
code({ className, children }) {
const lang = /language-(\w+)/.exec(className ?? "")?.[1];
return lang ? <CodeLight code={String(children)} lang={lang} /> : <code>{children}</code>;
},
}}
>
{replySoFar}
</ReactMarkdown>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.
| Function | What it does |
|---|---|
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. |
highlightElement(code, lang?) |
Highlights one finished code element in place. |
mount(element, { lang }) |
Streams highlighted code into an element. Returns a view with write(chunk), set(fullText) and reset(lang?). |
createStream(lang) |
The streaming core, without the DOM. push(chunk) returns { lines, tail }: lines finished by this chunk, and the line still being written. |
highlight(code, lang) |
HTML for finished code, to put inside <pre><code class="cl">. |
tokenize(code, lang) |
Tokens for each line: [{ type: "keyword", text: "const" }, …]. |
splitFences(markdown) |
Text and code blocks from Markdown that may still be streaming. |
registerLanguage(name, language, aliases?) |
Adds a language. Build one with defineLanguage({ keywords, lineComment, strings, … }). |
listLanguages(), getLanguage(name) |
The languages available. |
injectTheme() |
Adds the default theme without a <link>. |
<CodeLight code lang /> |
React component, from codelight.js/react. Other props go on the <pre>. |
Full details: API reference · Streaming guide · Writing a language · How it works
19 languages, including JavaScript, TypeScript, Python, Bash, SQL, HTML and CSS. See SUPPORTED_LANGUAGES.md for the full list and every alias. An unknown language shows as plain text, safely escaped.
| Theme | File |
|---|---|
| Default: light or dark to match the system | styles/default.css |
| Light | styles/light.css |
| Dark | styles/dark.css |
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.
Colours are CSS variables, so your own theme is a few lines:
.cl {
--cl-bg: #1e1e2e;
--cl-fg: #cdd6f4;
--cl-keyword: #cba6f7;
--cl-string: #a6e3a1;
--cl-comment: #6c7086;
--cl-function: #89b4fa;
}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.
| Folder | What's in it |
|---|---|
src/codelight.ts |
The entry point |
src/lib/ |
The tokenizer engine, streaming, DOM views, Markdown fences, themes |
src/languages/ |
One file per language |
src/styles/ |
Themes |
src/react.ts |
The React component |
build/ |
Browser files: codelight.js, codelight.min.js, styles/ (made by npm run build) |
es/, lib/ |
ES module and CommonJS builds for bundlers and Node |
docs/ |
Documentation |
demo/ |
The demo page, published at codelightjs.github.io |
tools/ |
Build, demo and screenshot scripts |
test/ |
Tests |
codelight.js is built for code that's still arriving. For other jobs there are better tools:
- Docs sites and blogs with finished code: Shiki gives VS Code-accurate colours for 200+ languages, and highlight.js or Prism cover far more languages.
- A React chat app that already uses Shiki: Streamdown renders streaming Markdown with Shiki highlighting, and
@shikijs/streamstreams Shiki itself.
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.
See CONTRIBUTING.md. Security issues: SECURITY.md.
MIT