codelight.js 1.0.0-test → 1.1.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 +19 -0
- package/LICENSE +21 -0
- package/README.CDN.md +60 -0
- package/README.md +199 -2
- package/SUPPORTED_LANGUAGES.md +48 -0
- package/build/codelight.js +858 -0
- package/build/codelight.min.js +133 -0
- package/build/styles/dark.css +47 -0
- package/build/styles/dark.min.css +1 -0
- package/build/styles/default.css +119 -0
- package/build/styles/default.min.css +5 -0
- package/build/styles/light.css +47 -0
- package/build/styles/light.min.css +1 -0
- package/es/chunk-TDW3KXGY.js +723 -0
- package/es/codelight.d.ts +184 -0
- package/es/codelight.js +140 -0
- package/es/react.d.ts +15 -0
- package/es/react.js +34 -0
- package/lib/chunk-I4AYT5OY.cjs +723 -0
- package/lib/codelight.cjs +140 -0
- package/lib/codelight.d.cts +184 -0
- package/lib/react.cjs +34 -0
- package/lib/react.d.cts +15 -0
- package/package.json +104 -6
- package/index.js +0 -1
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }/*! codelight.js 1.1.0 | MIT License | https://github.com/codelightjs/codelight.js */
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
var _chunkI4AYT5OYcjs = require('./chunk-I4AYT5OY.cjs');
|
|
15
|
+
|
|
16
|
+
// src/lib/auto.ts
|
|
17
|
+
var MARKDOWN_CLASS = /(?:^|\s)language-(\S+)/;
|
|
18
|
+
function languageOf(code) {
|
|
19
|
+
return _nullishCoalesce(_nullishCoalesce(_nullishCoalesce(code.getAttribute("data-lang"), () => ( _optionalChain([code, 'access', _ => _.parentElement, 'optionalAccess', _2 => _2.getAttribute, 'call', _3 => _3("data-lang")]))), () => ( _optionalChain([MARKDOWN_CLASS, 'access', _4 => _4.exec, 'call', _5 => _5(code.className), 'optionalAccess', _6 => _6[1]]))), () => ( ""));
|
|
20
|
+
}
|
|
21
|
+
function highlightElement(code, lang = languageOf(code)) {
|
|
22
|
+
code.innerHTML = _chunkI4AYT5OYcjs.highlight.call(void 0, _nullishCoalesce(code.textContent, () => ( "")), lang);
|
|
23
|
+
code.classList.add("cl");
|
|
24
|
+
code.setAttribute("data-cl", lang);
|
|
25
|
+
}
|
|
26
|
+
function highlightAll(root) {
|
|
27
|
+
if (!root && typeof document === "undefined") return;
|
|
28
|
+
const run = () => {
|
|
29
|
+
for (const code of (_nullishCoalesce(root, () => ( document))).querySelectorAll("pre > code:not([data-cl])")) highlightElement(code);
|
|
30
|
+
};
|
|
31
|
+
if (!root && document.readyState === "loading") document.addEventListener("DOMContentLoaded", run, { once: true });
|
|
32
|
+
else run();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/lib/fences.ts
|
|
36
|
+
var OPEN = /^ {0,3}(`{3,}|~{3,})(.*)$/;
|
|
37
|
+
var PARTIAL_FENCE = /^ {0,3}(?:`{1,2}|~{1,2})$/;
|
|
38
|
+
function splitFences(markdown) {
|
|
39
|
+
const lines = markdown.split("\n");
|
|
40
|
+
const out = [];
|
|
41
|
+
let text = [];
|
|
42
|
+
const flush = () => {
|
|
43
|
+
if (text.length) out.push({ type: "text", text: text.join("\n") });
|
|
44
|
+
text = [];
|
|
45
|
+
};
|
|
46
|
+
let i = 0;
|
|
47
|
+
while (i < lines.length) {
|
|
48
|
+
const line = lines[i].replace(/\r$/, "");
|
|
49
|
+
const open = OPEN.exec(line);
|
|
50
|
+
if (!open || open[1][0] === "`" && open[2].includes("`")) {
|
|
51
|
+
text.push(line);
|
|
52
|
+
i++;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
flush();
|
|
56
|
+
const fence = open[1];
|
|
57
|
+
const openerDone = i < lines.length - 1;
|
|
58
|
+
const close = new RegExp(`^ {0,3}${fence[0] === "`" ? "`" : "~"}{${fence.length},}\\s*$`);
|
|
59
|
+
const body = [];
|
|
60
|
+
let closed = false;
|
|
61
|
+
let j = i + 1;
|
|
62
|
+
for (; j < lines.length; j++) {
|
|
63
|
+
const l = lines[j].replace(/\r$/, "");
|
|
64
|
+
if (close.test(l)) {
|
|
65
|
+
closed = true;
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
body.push(l);
|
|
69
|
+
}
|
|
70
|
+
let code;
|
|
71
|
+
if (closed) {
|
|
72
|
+
code = body.map((l) => l + "\n").join("");
|
|
73
|
+
} else {
|
|
74
|
+
if (body.length && PARTIAL_FENCE.test(body[body.length - 1])) body[body.length - 1] = "";
|
|
75
|
+
code = body.join("\n");
|
|
76
|
+
}
|
|
77
|
+
out.push({ type: "code", lang: openerDone ? _nullishCoalesce(open[2].trim().split(/\s+/)[0], () => ( "")) : "", code, closed });
|
|
78
|
+
i = closed ? j + 1 : j;
|
|
79
|
+
}
|
|
80
|
+
if (text.length && PARTIAL_FENCE.test(text[text.length - 1])) text.pop();
|
|
81
|
+
flush();
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/styles/default.css
|
|
86
|
+
var default_default = '/*!\n codelight.js theme: default\n Light or dark to match the system. To force one, put a cl-light or cl-dark class\n (or light / dark, or data-theme="light" / "dark") on the code or any parent.\n*/\n.cl {\n --cl-fg: #1e2430;\n --cl-bg: #f7f7f9;\n --cl-keyword: #b4235a;\n --cl-string: #1d6b45;\n --cl-comment: #6b7280;\n --cl-number: #a35200;\n --cl-function: #3a52c4;\n --cl-type: #8a3ab9;\n --cl-constant: #a35200;\n --cl-variable: #9a3f0c;\n --cl-property: #1f6f9a;\n --cl-operator: #b4235a;\n --cl-punctuation: #4b5563;\n --cl-tag: #2459a8;\n --cl-attr: #8a3ab9;\n --cl-meta: #6b4fbb;\n --cl-inserted: #166534;\n --cl-inserted-bg: #dcfce7;\n --cl-deleted: #9f1239;\n --cl-deleted-bg: #ffe4e6;\n color: var(--cl-fg);\n background: var(--cl-bg);\n}\n@media (prefers-color-scheme: dark) {\n .cl {\n --cl-fg: #e4e7ee;\n --cl-bg: #11141a;\n --cl-keyword: #ff7aa8;\n --cl-string: #8fdba8;\n --cl-comment: #8a93a3;\n --cl-number: #ffb86b;\n --cl-function: #8fb4ff;\n --cl-type: #d0a3ff;\n --cl-constant: #ffb86b;\n --cl-variable: #ffa070;\n --cl-property: #7fd0f0;\n --cl-operator: #ff7aa8;\n --cl-punctuation: #aab1bd;\n --cl-tag: #8fb4ff;\n --cl-attr: #d0a3ff;\n --cl-meta: #b49cff;\n --cl-inserted: #a7f3c0;\n --cl-inserted-bg: #0f3a24;\n --cl-deleted: #fecdd3;\n --cl-deleted-bg: #4c1020;\n }\n}\n.cl-dark .cl, .cl.cl-dark, .dark .cl, [data-theme="dark"] .cl {\n --cl-fg: #e4e7ee;\n --cl-bg: #11141a;\n --cl-keyword: #ff7aa8;\n --cl-string: #8fdba8;\n --cl-comment: #8a93a3;\n --cl-number: #ffb86b;\n --cl-function: #8fb4ff;\n --cl-type: #d0a3ff;\n --cl-constant: #ffb86b;\n --cl-variable: #ffa070;\n --cl-property: #7fd0f0;\n --cl-operator: #ff7aa8;\n --cl-punctuation: #aab1bd;\n --cl-tag: #8fb4ff;\n --cl-attr: #d0a3ff;\n --cl-meta: #b49cff;\n --cl-inserted: #a7f3c0;\n --cl-inserted-bg: #0f3a24;\n --cl-deleted: #fecdd3;\n --cl-deleted-bg: #4c1020;\n}\n.cl-light .cl, .cl.cl-light, .light .cl, [data-theme="light"] .cl {\n --cl-fg: #1e2430;\n --cl-bg: #f7f7f9;\n --cl-keyword: #b4235a;\n --cl-string: #1d6b45;\n --cl-comment: #6b7280;\n --cl-number: #a35200;\n --cl-function: #3a52c4;\n --cl-type: #8a3ab9;\n --cl-constant: #a35200;\n --cl-variable: #9a3f0c;\n --cl-property: #1f6f9a;\n --cl-operator: #b4235a;\n --cl-punctuation: #4b5563;\n --cl-tag: #2459a8;\n --cl-attr: #8a3ab9;\n --cl-meta: #6b4fbb;\n --cl-inserted: #166534;\n --cl-inserted-bg: #dcfce7;\n --cl-deleted: #9f1239;\n --cl-deleted-bg: #ffe4e6;\n}\npre > code.cl {\n display: block;\n padding: 1rem 1.25rem;\n overflow: auto;\n}\n.cl-keyword { color: var(--cl-keyword); }\n.cl-string { color: var(--cl-string); }\n.cl-comment { color: var(--cl-comment); }\n.cl-number { color: var(--cl-number); }\n.cl-function { color: var(--cl-function); }\n.cl-type { color: var(--cl-type); }\n.cl-constant { color: var(--cl-constant); }\n.cl-variable { color: var(--cl-variable); }\n.cl-property { color: var(--cl-property); }\n.cl-operator { color: var(--cl-operator); }\n.cl-punctuation { color: var(--cl-punctuation); }\n.cl-tag { color: var(--cl-tag); }\n.cl-attr { color: var(--cl-attr); }\n.cl-meta { color: var(--cl-meta); }\n.cl-comment { font-style: italic; }\n.cl-inserted { color: var(--cl-inserted); background: var(--cl-inserted-bg); }\n.cl-deleted { color: var(--cl-deleted); background: var(--cl-deleted-bg); }\n';
|
|
87
|
+
|
|
88
|
+
// src/lib/theme.ts
|
|
89
|
+
var themeCss = default_default;
|
|
90
|
+
function injectTheme(doc = document) {
|
|
91
|
+
if (doc.getElementById("codelight-theme")) return;
|
|
92
|
+
const style = doc.createElement("style");
|
|
93
|
+
style.id = "codelight-theme";
|
|
94
|
+
style.textContent = themeCss;
|
|
95
|
+
doc.head.append(style);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// src/codelight.ts
|
|
99
|
+
var codelight = {
|
|
100
|
+
createMarkup: _chunkI4AYT5OYcjs.createMarkup,
|
|
101
|
+
createStream: _chunkI4AYT5OYcjs.createStream,
|
|
102
|
+
defineLanguage: _chunkI4AYT5OYcjs.defineLanguage,
|
|
103
|
+
escapeHtml: _chunkI4AYT5OYcjs.escapeHtml,
|
|
104
|
+
getLanguage: _chunkI4AYT5OYcjs.getLanguage,
|
|
105
|
+
highlight: _chunkI4AYT5OYcjs.highlight,
|
|
106
|
+
highlightAll,
|
|
107
|
+
highlightElement,
|
|
108
|
+
injectTheme,
|
|
109
|
+
listLanguages: _chunkI4AYT5OYcjs.listLanguages,
|
|
110
|
+
mount: _chunkI4AYT5OYcjs.mount,
|
|
111
|
+
registerLanguage: _chunkI4AYT5OYcjs.registerLanguage,
|
|
112
|
+
renderLine: _chunkI4AYT5OYcjs.renderLine,
|
|
113
|
+
splitFences,
|
|
114
|
+
themeCss,
|
|
115
|
+
tokenize: _chunkI4AYT5OYcjs.tokenize
|
|
116
|
+
};
|
|
117
|
+
var codelight_default = codelight;
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
exports.createMarkup = _chunkI4AYT5OYcjs.createMarkup; exports.createStream = _chunkI4AYT5OYcjs.createStream; exports.default = codelight_default; exports.defineLanguage = _chunkI4AYT5OYcjs.defineLanguage; exports.escapeHtml = _chunkI4AYT5OYcjs.escapeHtml; exports.getLanguage = _chunkI4AYT5OYcjs.getLanguage; exports.highlight = _chunkI4AYT5OYcjs.highlight; exports.highlightAll = highlightAll; exports.highlightElement = highlightElement; exports.injectTheme = injectTheme; exports.listLanguages = _chunkI4AYT5OYcjs.listLanguages; exports.mount = _chunkI4AYT5OYcjs.mount; exports.registerLanguage = _chunkI4AYT5OYcjs.registerLanguage; exports.renderLine = _chunkI4AYT5OYcjs.renderLine; exports.splitFences = splitFences; exports.themeCss = themeCss; exports.tokenize = _chunkI4AYT5OYcjs.tokenize;
|
|
137
|
+
/*!
|
|
138
|
+
codelight.js — syntax highlighting for code that streams in, like AI chat replies.
|
|
139
|
+
MIT License · https://github.com/codelightjs/codelight.js
|
|
140
|
+
*/
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Highlights a finished code element in place from its text. The language comes from `data-lang`
|
|
3
|
+
* or a `language-xxx` class unless you pass one. Marks the element with `data-cl` so it isn't done twice.
|
|
4
|
+
*/
|
|
5
|
+
declare function highlightElement(code: Element, lang?: string): void;
|
|
6
|
+
/**
|
|
7
|
+
* Highlights every `<pre><code>` inside `root` (the whole page by default) that hasn't been
|
|
8
|
+
* highlighted yet. For code that's already on the page; use `mount` for code that's streaming in.
|
|
9
|
+
* If the page is still loading, it waits for it.
|
|
10
|
+
*/
|
|
11
|
+
declare function highlightAll(root?: ParentNode): void;
|
|
12
|
+
|
|
13
|
+
interface CodeView {
|
|
14
|
+
/** Appends text. Finished lines are added once; only the last line is redrawn. */
|
|
15
|
+
write(chunk: string): void;
|
|
16
|
+
/**
|
|
17
|
+
* Shows `text`. If it starts with what's already shown, only the new part is written,
|
|
18
|
+
* so you can pass the whole reply on every update, as React and most chat state do.
|
|
19
|
+
*/
|
|
20
|
+
set(text: string): void;
|
|
21
|
+
/** Clears the code, optionally switching language. */
|
|
22
|
+
reset(lang?: string): void;
|
|
23
|
+
readonly text: string;
|
|
24
|
+
readonly lang: string | undefined;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Streams highlighted code into an element, usually a `<code>` inside a `<pre>`.
|
|
28
|
+
* Lines that are finished are never touched again, so there's no flicker, text selection
|
|
29
|
+
* keeps working while the reply streams in, and the cost doesn't grow with the length of the code.
|
|
30
|
+
*/
|
|
31
|
+
declare function mount(target: Element, options?: {
|
|
32
|
+
lang?: string;
|
|
33
|
+
}): CodeView;
|
|
34
|
+
|
|
35
|
+
/** What a piece of code is. Each type becomes a `cl-<type>` CSS class. */
|
|
36
|
+
type TokenType = "keyword" | "string" | "comment" | "number" | "function" | "type" | "constant" | "variable" | "property" | "operator" | "punctuation" | "tag" | "attr" | "meta" | "inserted" | "deleted";
|
|
37
|
+
/** A run of text. Plain text has no `type`. */
|
|
38
|
+
interface Token {
|
|
39
|
+
type?: TokenType;
|
|
40
|
+
text: string;
|
|
41
|
+
}
|
|
42
|
+
/** The tokens of one line, without its newline. */
|
|
43
|
+
type Line = Token[];
|
|
44
|
+
/**
|
|
45
|
+
* Where the tokenizer is at the end of a line, such as inside a block comment or a multi-line string.
|
|
46
|
+
* `null` means nothing is open. States are never changed after they're returned, so a line can be
|
|
47
|
+
* tokenized again from the same state as often as needed. That's what makes streaming cheap.
|
|
48
|
+
*/
|
|
49
|
+
type State = object | null;
|
|
50
|
+
/** A language tokenizes one line at a time, carrying state from line to line. */
|
|
51
|
+
interface Language {
|
|
52
|
+
tokenizeLine(line: string, state: State): {
|
|
53
|
+
tokens: Line;
|
|
54
|
+
state: State;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface StringDef {
|
|
59
|
+
open: string;
|
|
60
|
+
/** Defaults to `open`. */
|
|
61
|
+
close?: string;
|
|
62
|
+
/** Backslash escapes the next character. Default true. */
|
|
63
|
+
escape?: boolean;
|
|
64
|
+
/** The string can continue on the next line. */
|
|
65
|
+
multiline?: boolean;
|
|
66
|
+
/** Default "string". */
|
|
67
|
+
type?: TokenType;
|
|
68
|
+
}
|
|
69
|
+
interface Rule {
|
|
70
|
+
/** Tried at each position before anything else. Lookbehind works, so `(?<=^\s*)@\w+` matches at line start. */
|
|
71
|
+
match: RegExp;
|
|
72
|
+
type: TokenType;
|
|
73
|
+
}
|
|
74
|
+
/** Everything a typical programming language needs. Word lists are space-separated. */
|
|
75
|
+
interface LanguageDef {
|
|
76
|
+
lineComment?: string[];
|
|
77
|
+
blockComment?: [open: string, close: string][];
|
|
78
|
+
strings?: StringDef[];
|
|
79
|
+
keywords?: string;
|
|
80
|
+
types?: string;
|
|
81
|
+
constants?: string;
|
|
82
|
+
/** Built-in functions and commands. */
|
|
83
|
+
builtins?: string;
|
|
84
|
+
variables?: string;
|
|
85
|
+
caseInsensitive?: boolean;
|
|
86
|
+
rules?: Rule[];
|
|
87
|
+
identifier?: RegExp;
|
|
88
|
+
/** `Capitalised` names are types (classes, structs). */
|
|
89
|
+
classNames?: boolean;
|
|
90
|
+
/** A name followed by `(` is a function. Default true. */
|
|
91
|
+
calls?: boolean;
|
|
92
|
+
}
|
|
93
|
+
/** Builds a language from keyword lists and comment and string rules. */
|
|
94
|
+
declare function defineLanguage(def: LanguageDef): Language;
|
|
95
|
+
|
|
96
|
+
type Segment = {
|
|
97
|
+
type: "text";
|
|
98
|
+
text: string;
|
|
99
|
+
} | {
|
|
100
|
+
type: "code";
|
|
101
|
+
/** The language after the opening fence, such as `ts`. Empty until the opening line is complete. */
|
|
102
|
+
lang: string;
|
|
103
|
+
code: string;
|
|
104
|
+
/** False while the closing fence hasn't arrived yet. */
|
|
105
|
+
closed: boolean;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Splits Markdown into text and fenced code blocks, and copes with a reply that's cut off
|
|
109
|
+
* mid-stream: an unclosed fence is a code block (`closed: false`), and a half-typed fence
|
|
110
|
+
* isn't shown as stray backticks. A block's `code` only ever grows while it streams, so it
|
|
111
|
+
* can go straight into `mount(...).set(code)` or `<CodeLight code={code} />`.
|
|
112
|
+
*/
|
|
113
|
+
declare function splitFences(markdown: string): Segment[];
|
|
114
|
+
|
|
115
|
+
/** HTML, XML and SVG. `embeds` highlights the inside of tags such as `<script>` and `<style>`. */
|
|
116
|
+
declare function createMarkup(embeds?: Record<string, Language>): Language;
|
|
117
|
+
|
|
118
|
+
/** The language for a name or alias (`ts`, `py`, `sh`, `html`…), or undefined if it isn't known. */
|
|
119
|
+
declare function getLanguage(name: string | undefined): Language | undefined;
|
|
120
|
+
/** Adds a language, or replaces a built-in one. */
|
|
121
|
+
declare function registerLanguage(name: string, language: Language, alias?: string[]): void;
|
|
122
|
+
/** The names of every registered language. */
|
|
123
|
+
declare function listLanguages(): string[];
|
|
124
|
+
|
|
125
|
+
declare function escapeHtml(text: string): string;
|
|
126
|
+
/** One line as HTML: `<span class="cl-keyword">const</span> x`. */
|
|
127
|
+
declare function renderLine(line: Line): string;
|
|
128
|
+
/** Tokens for every line of `code`. */
|
|
129
|
+
declare function tokenize(code: string, lang?: string | Language): Line[];
|
|
130
|
+
/** Highlighted HTML for finished code, to put inside `<pre><code class="cl">`. */
|
|
131
|
+
declare function highlight(code: string, lang?: string | Language): string;
|
|
132
|
+
interface StreamUpdate {
|
|
133
|
+
/** Lines finished by this chunk. They never change again. */
|
|
134
|
+
lines: Line[];
|
|
135
|
+
/** The line still being written. It's replaced on every push. */
|
|
136
|
+
tail: Line;
|
|
137
|
+
}
|
|
138
|
+
interface Stream {
|
|
139
|
+
/** Adds text and returns only what changed. */
|
|
140
|
+
push(chunk: string): StreamUpdate;
|
|
141
|
+
/** Everything pushed so far. */
|
|
142
|
+
readonly text: string;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Highlights code as it arrives. Each finished line is tokenized exactly once; only the
|
|
146
|
+
* unfinished last line is tokenized again on each push. Pushing a 500-line reply one token
|
|
147
|
+
* at a time costs about the same as highlighting it once.
|
|
148
|
+
*/
|
|
149
|
+
declare function createStream(lang?: string | Language): Stream;
|
|
150
|
+
|
|
151
|
+
/** The default theme's CSS (light and dark, following the system setting). Same as `build/styles/default.css`. */
|
|
152
|
+
declare const themeCss: string;
|
|
153
|
+
/**
|
|
154
|
+
* Adds the default theme to the page once. Handy with the `<script>` build when you'd rather not add
|
|
155
|
+
* a `<link>`; bundlers can import `codelight.js/styles/default.css` instead.
|
|
156
|
+
*/
|
|
157
|
+
declare function injectTheme(doc?: Document): void;
|
|
158
|
+
|
|
159
|
+
/*!
|
|
160
|
+
codelight.js — syntax highlighting for code that streams in, like AI chat replies.
|
|
161
|
+
MIT License · https://github.com/codelightjs/codelight.js
|
|
162
|
+
*/
|
|
163
|
+
|
|
164
|
+
/** Everything in one object: `import codelight from "codelight.js"`. */
|
|
165
|
+
declare const codelight: {
|
|
166
|
+
createMarkup: typeof createMarkup;
|
|
167
|
+
createStream: typeof createStream;
|
|
168
|
+
defineLanguage: typeof defineLanguage;
|
|
169
|
+
escapeHtml: typeof escapeHtml;
|
|
170
|
+
getLanguage: typeof getLanguage;
|
|
171
|
+
highlight: typeof highlight;
|
|
172
|
+
highlightAll: typeof highlightAll;
|
|
173
|
+
highlightElement: typeof highlightElement;
|
|
174
|
+
injectTheme: typeof injectTheme;
|
|
175
|
+
listLanguages: typeof listLanguages;
|
|
176
|
+
mount: typeof mount;
|
|
177
|
+
registerLanguage: typeof registerLanguage;
|
|
178
|
+
renderLine: typeof renderLine;
|
|
179
|
+
splitFences: typeof splitFences;
|
|
180
|
+
themeCss: string;
|
|
181
|
+
tokenize: typeof tokenize;
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
export { type CodeView, type Language, type LanguageDef, type Line, type Rule, type Segment, type State, type Stream, type StreamUpdate, type StringDef, type Token, type TokenType, createMarkup, createStream, codelight as default, defineLanguage, escapeHtml, getLanguage, highlight, highlightAll, highlightElement, injectTheme, listLanguages, mount, registerLanguage, renderLine, splitFences, themeCss, tokenize };
|
package/lib/react.cjs
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }/*! codelight.js 1.1.0 | MIT License | https://github.com/codelightjs/codelight.js */
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
var _chunkI4AYT5OYcjs = require('./chunk-I4AYT5OY.cjs');
|
|
6
|
+
|
|
7
|
+
// src/react.ts
|
|
8
|
+
var _react = require('react');
|
|
9
|
+
var useIsomorphicLayoutEffect = typeof window === "undefined" ? _react.useEffect : _react.useLayoutEffect;
|
|
10
|
+
function CodeLight({ code, lang, className, ...rest }) {
|
|
11
|
+
const ref = _react.useRef.call(void 0, null);
|
|
12
|
+
const view = _react.useRef.call(void 0, null);
|
|
13
|
+
const [initial] = _react.useState.call(void 0, () => ({ __html: _chunkI4AYT5OYcjs.highlight.call(void 0, code, lang) }));
|
|
14
|
+
useIsomorphicLayoutEffect(() => {
|
|
15
|
+
const v = _chunkI4AYT5OYcjs.mount.call(void 0, ref.current, { lang });
|
|
16
|
+
v.set(code);
|
|
17
|
+
view.current = v;
|
|
18
|
+
return () => {
|
|
19
|
+
view.current = null;
|
|
20
|
+
};
|
|
21
|
+
}, [lang]);
|
|
22
|
+
useIsomorphicLayoutEffect(() => {
|
|
23
|
+
_optionalChain([view, 'access', _ => _.current, 'optionalAccess', _2 => _2.set, 'call', _3 => _3(code)]);
|
|
24
|
+
}, [code]);
|
|
25
|
+
return _react.createElement.call(void 0,
|
|
26
|
+
"pre",
|
|
27
|
+
{ ...rest, className, "data-lang": lang },
|
|
28
|
+
// `<pre><code class="cl">`, the same markup highlightAll() produces.
|
|
29
|
+
_react.createElement.call(void 0, "code", { ref, className: "cl", dangerouslySetInnerHTML: initial })
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
exports.CodeLight = CodeLight;
|
package/lib/react.d.cts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { HTMLAttributes, ReactElement } from 'react';
|
|
2
|
+
|
|
3
|
+
interface CodeLightProps extends Omit<HTMLAttributes<HTMLPreElement>, "children"> {
|
|
4
|
+
/** The code so far. Pass the whole thing on every update; only the new part is highlighted. */
|
|
5
|
+
code: string;
|
|
6
|
+
/** Language name or alias: `ts`, `python`, `sh`… Unknown names show plain text. */
|
|
7
|
+
lang?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Highlighted code that can grow while it streams in. Finished lines are never re-rendered,
|
|
11
|
+
* so long AI replies don't flicker or slow down. Renders highlighted HTML on the server too.
|
|
12
|
+
*/
|
|
13
|
+
declare function CodeLight({ code, lang, className, ...rest }: CodeLightProps): ReactElement;
|
|
14
|
+
|
|
15
|
+
export { CodeLight, type CodeLightProps };
|
package/package.json
CHANGED
|
@@ -1,10 +1,108 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codelight.js",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
5
|
-
"keywords": [
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Very fast, lightweight syntax highlighter. Highlights each line once and redraws only what changed, so live code never flickers. 19 languages, no dependencies, React or a script tag.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"syntax-highlighting",
|
|
7
|
+
"highlight",
|
|
8
|
+
"highlighter",
|
|
9
|
+
"syntax-highlighter",
|
|
10
|
+
"code-highlighting",
|
|
11
|
+
"lightweight",
|
|
12
|
+
"fast",
|
|
13
|
+
"code",
|
|
14
|
+
"streaming",
|
|
15
|
+
"stream",
|
|
16
|
+
"ai",
|
|
17
|
+
"llm",
|
|
18
|
+
"chat",
|
|
19
|
+
"chatbot",
|
|
20
|
+
"markdown",
|
|
21
|
+
"code-block",
|
|
22
|
+
"react",
|
|
23
|
+
"incremental",
|
|
24
|
+
"zero-dependency"
|
|
25
|
+
],
|
|
26
|
+
"license": "MIT",
|
|
8
27
|
"author": "CK Giri <ckgdev@gmail.com>",
|
|
9
|
-
"
|
|
28
|
+
"homepage": "https://github.com/codelightjs/codelight.js#readme",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/codelightjs/codelight.js.git"
|
|
32
|
+
},
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/codelightjs/codelight.js/issues"
|
|
35
|
+
},
|
|
36
|
+
"type": "module",
|
|
37
|
+
"sideEffects": false,
|
|
38
|
+
"main": "./lib/codelight.cjs",
|
|
39
|
+
"module": "./es/codelight.js",
|
|
40
|
+
"types": "./es/codelight.d.ts",
|
|
41
|
+
"unpkg": "./build/codelight.min.js",
|
|
42
|
+
"jsdelivr": "./build/codelight.min.js",
|
|
43
|
+
"exports": {
|
|
44
|
+
".": {
|
|
45
|
+
"import": {
|
|
46
|
+
"types": "./es/codelight.d.ts",
|
|
47
|
+
"default": "./es/codelight.js"
|
|
48
|
+
},
|
|
49
|
+
"require": {
|
|
50
|
+
"types": "./lib/codelight.d.cts",
|
|
51
|
+
"default": "./lib/codelight.cjs"
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"./react": {
|
|
55
|
+
"import": {
|
|
56
|
+
"types": "./es/react.d.ts",
|
|
57
|
+
"default": "./es/react.js"
|
|
58
|
+
},
|
|
59
|
+
"require": {
|
|
60
|
+
"types": "./lib/react.d.cts",
|
|
61
|
+
"default": "./lib/react.cjs"
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
"./styles/*": "./build/styles/*",
|
|
65
|
+
"./build/*": "./build/*",
|
|
66
|
+
"./package.json": "./package.json"
|
|
67
|
+
},
|
|
68
|
+
"files": [
|
|
69
|
+
"es",
|
|
70
|
+
"lib",
|
|
71
|
+
"build",
|
|
72
|
+
"CHANGES.md",
|
|
73
|
+
"README.CDN.md",
|
|
74
|
+
"SUPPORTED_LANGUAGES.md"
|
|
75
|
+
],
|
|
76
|
+
"peerDependencies": {
|
|
77
|
+
"react": ">=18"
|
|
78
|
+
},
|
|
79
|
+
"peerDependenciesMeta": {
|
|
80
|
+
"react": {
|
|
81
|
+
"optional": true
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
"scripts": {
|
|
85
|
+
"build": "tsup",
|
|
86
|
+
"test": "vitest run",
|
|
87
|
+
"typecheck": "tsc -p .",
|
|
88
|
+
"prepublishOnly": "npm run typecheck && npm run build && npm test",
|
|
89
|
+
"release": "npm version patch && git push --follow-tags",
|
|
90
|
+
"release:minor": "npm version minor && git push --follow-tags",
|
|
91
|
+
"release:major": "npm version major && git push --follow-tags",
|
|
92
|
+
"screenshot": "npm run demo && node tools/screenshot.mjs",
|
|
93
|
+
"demo": "npm run build && node tools/build-demo.mjs"
|
|
94
|
+
},
|
|
95
|
+
"devDependencies": {
|
|
96
|
+
"@types/node": "^20.19.43",
|
|
97
|
+
"@types/react": "^19.3.3",
|
|
98
|
+
"@types/react-dom": "^19.3.0",
|
|
99
|
+
"esbuild": "^0.27.7",
|
|
100
|
+
"happy-dom": "^21.0.4",
|
|
101
|
+
"puppeteer-core": "^25.19.0",
|
|
102
|
+
"react": "^19.3.1",
|
|
103
|
+
"react-dom": "^19.3.1",
|
|
104
|
+
"tsup": "^8.5.1",
|
|
105
|
+
"typescript": "^5.9.3",
|
|
106
|
+
"vitest": "^2.1.9"
|
|
107
|
+
}
|
|
10
108
|
}
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = {};
|