@vortexlabs/kirari-react 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/CHANGELOG.md ADDED
@@ -0,0 +1,24 @@
1
+ # @vortexlabs/kirari-react
2
+
3
+ ## 1.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - 14accd0: Release Kirari 1.0.0 with the core syntax highlighting APIs, Rehype and MDX integration, and React components and streaming hooks. Update the integrations to require Kirari 1.x.
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [14accd0]
12
+ - Updated dependencies [149d19f]
13
+ - @vortexlabs/kirari@1.0.0
14
+
15
+ ## 1.0.0
16
+
17
+ ### Major Changes
18
+
19
+ - Release Kirari 1.0.0 with the core syntax highlighting APIs, Rehype and MDX integration, and React components and streaming hooks. Update the integrations to require Kirari 1.x.
20
+
21
+ ### Patch Changes
22
+
23
+ - Updated dependencies
24
+ - @vortexlabs/kirari@1.0.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ugi (https://ugi.dev)
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.md CHANGED
@@ -1,3 +1,60 @@
1
- # Temporary Holding Version
1
+ # @vortexlabs/kirari-react
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
+ Kirari highlighting as React elements. `<Code>` renders tokens as `<span>`
4
+ elements, with no `dangerouslySetInnerHTML` and no hooks, so it works as a
5
+ Server Component.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install @vortexlabs/kirari @vortexlabs/kirari-react
11
+ ```
12
+
13
+ Requires React 19. Kirari is a peer dependency.
14
+
15
+ ## Usage
16
+
17
+ ```tsx
18
+ import { Code } from '@vortexlabs/kirari-react'
19
+ import { typescript } from '@vortexlabs/kirari/languages/typescript'
20
+ import '@vortexlabs/kirari/theme.css'
21
+
22
+ <Code language={typescript} highlightLines={[1, [3, 4]]}>{source}</Code>
23
+ ```
24
+
25
+ Output is `<pre class="kirari"><code>…</code></pre>` with the standard
26
+ `kirari-*` classes, so every Kirari theme works. `language` defaults to
27
+ JavaScript. Other props go to the `<pre>`.
28
+
29
+ | Prop | Effect |
30
+ | --- | --- |
31
+ | `lines` | Wrap each line in `<span class="kirari-line" data-line="n">`. |
32
+ | `highlightLines` | Lines or `[first, last]` ranges get `kirari-line-highlighted`. Implies `lines`. |
33
+ | `lineClassName` | `(line) => class` for a line wrapper. `line` is `{ number, start, end }`. |
34
+ | `decorations` | `{ start, end, className }` source ranges, as in [decorations](../../docs/decorations.md). |
35
+ | `classes` | Extra classes per token type, e.g. `{ keyword: 'kw' }`. |
36
+ | `tokenClassName` | `(token) => class` per token. |
37
+ | `tokens` | Pre-scanned tokens instead of `children`. |
38
+
39
+ Line numbers are CSS:
40
+
41
+ ```css
42
+ .kirari-line::before { content: attr(data-line); display: inline-block; width: 3ch; margin-right: 2ch; opacity: 0.5; }
43
+ ```
44
+
45
+ ## Streaming
46
+
47
+ ```tsx
48
+ 'use client'
49
+ import { useTokenStream } from '@vortexlabs/kirari-react/stream'
50
+
51
+ const { tokens, write, end, reset } = useTokenStream(javascript)
52
+ // write(chunk) as text arrives, then end()
53
+ return <Code tokens={tokens} />
54
+ ```
55
+
56
+ - Rescans only the unfinished tail. See [token streaming](../../docs/streaming.md).
57
+ - Changing `language` starts a new, empty stream.
58
+ - After `end()`, call `reset()` before writing again.
59
+ - `/stream` is a client module; `<Code>` is not.
60
+ - Managing tokens yourself? Use `createTokenStream` from `@vortexlabs/kirari/stream` with `applyPatch(tokens, patch)`.
@@ -0,0 +1,28 @@
1
+ import type { ComponentPropsWithoutRef, ReactNode } from 'react';
2
+ import type { Language, Token } from '@vortexlabs/kirari/core';
3
+ import type { Decoration } from '@vortexlabs/kirari/decorations';
4
+ export type LineRange = number | readonly [first: number, last: number];
5
+ export interface Line {
6
+ readonly number: number;
7
+ readonly start: number;
8
+ readonly end: number;
9
+ }
10
+ export interface CodeProps extends Omit<ComponentPropsWithoutRef<'pre'>, 'children'> {
11
+ /** Source to highlight. Ignored when `tokens` is set. */
12
+ readonly children?: string;
13
+ /** Pre-scanned tokens, such as the output of `useTokenStream`. */
14
+ readonly tokens?: readonly Token[];
15
+ readonly language?: Language;
16
+ readonly classes?: Readonly<Record<string, string>>;
17
+ readonly tokenClassName?: (token: Token) => string | undefined;
18
+ readonly decorations?: readonly Decoration[];
19
+ /** Wrap each line in `<span class="kirari-line" data-line="n">`. */
20
+ readonly lines?: boolean;
21
+ readonly lineClassName?: (line: Line) => string | undefined;
22
+ /** One-based lines or inclusive ranges that receive `kirari-line-highlighted`. Implies `lines`. */
23
+ readonly highlightLines?: readonly LineRange[];
24
+ }
25
+ /** Highlighted code as React elements. Uses no hooks, so it also renders as a Server Component. */
26
+ export declare const Code: ({ children, tokens, language, classes, tokenClassName, decorations, lines, lineClassName, highlightLines, className, ...props }: CodeProps) => ReactNode;
27
+ export type { Decoration } from '@vortexlabs/kirari/decorations';
28
+ export type { Language, Token } from '@vortexlabs/kirari/core';
package/dist/index.js ADDED
@@ -0,0 +1,103 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { tokenize } from '@vortexlabs/kirari/core';
3
+ import { javascript } from '@vortexlabs/kirari/languages/javascript';
4
+ const join = (...names) => {
5
+ return names.filter(Boolean).join(' ');
6
+ };
7
+ const splitsSurrogate = (source, offset) => {
8
+ const before = source.charCodeAt(offset - 1);
9
+ const after = source.charCodeAt(offset);
10
+ return before >= 0xd800 && before <= 0xdbff && after >= 0xdc00 && after <= 0xdfff;
11
+ };
12
+ const highlighted = (ranges) => {
13
+ const pairs = ranges.map(range => typeof range === 'number' ? [range, range] : range);
14
+ return line => pairs.some(([first, last]) => first <= line && line <= last);
15
+ };
16
+ const codeNodes = (tokens, options = {}) => {
17
+ let offset = 0;
18
+ for (const token of tokens) {
19
+ if (token.start !== offset || token.end !== offset + token.value.length) {
20
+ throw new RangeError('Kirari: tokens must cover contiguous source offsets starting at zero');
21
+ }
22
+ offset = token.end;
23
+ }
24
+ const source = tokens.map(token => token.value).join('');
25
+ const decorations = options.decorations ?? [];
26
+ const events = [];
27
+ for (const [index, decoration] of decorations.entries()) {
28
+ if (!Number.isInteger(decoration.start) || !Number.isInteger(decoration.end)
29
+ || decoration.start < 0 || decoration.start >= decoration.end || decoration.end > source.length) {
30
+ throw new RangeError('Kirari: decoration offsets must be integers with 0 <= start < end <= source length');
31
+ }
32
+ if (splitsSurrogate(source, decoration.start) || splitsSurrogate(source, decoration.end)) {
33
+ throw new RangeError('Kirari: decoration offsets must not split a UTF-16 surrogate pair');
34
+ }
35
+ events.push({ offset: decoration.start, index, entering: true }, { offset: decoration.end, index, entering: false });
36
+ }
37
+ events.sort((a, b) => a.offset - b.offset);
38
+ // Resolve callbacks once so tokens split by lines or decorations see them a single time.
39
+ const tokenClasses = tokens.map(token => join(options.classes && Object.hasOwn(options.classes, token.type) ? options.classes[token.type] : undefined, options.tokenClassName?.(token)));
40
+ const active = new Set();
41
+ let activeClasses = '';
42
+ let eventIndex = 0;
43
+ let tokenIndex = 0;
44
+ const rangeNodes = (start, end) => {
45
+ const nodes = [];
46
+ while (start < end) {
47
+ while (tokens[tokenIndex].end <= start)
48
+ tokenIndex++;
49
+ let changed = false;
50
+ while (eventIndex < events.length && events[eventIndex].offset <= start) {
51
+ const event = events[eventIndex++];
52
+ if (event.entering)
53
+ active.add(event.index);
54
+ else
55
+ active.delete(event.index);
56
+ changed = true;
57
+ }
58
+ if (changed)
59
+ activeClasses = [...active].sort((a, b) => a - b).map(index => decorations[index].className).join(' ');
60
+ const token = tokens[tokenIndex];
61
+ const stop = Math.min(end, token.end, events[eventIndex]?.offset ?? end);
62
+ const value = source.slice(start, stop);
63
+ const extra = join(tokenClasses[tokenIndex], activeClasses);
64
+ nodes.push(token.type === 'text' && !extra
65
+ ? value
66
+ : _jsx("span", { className: join(`kirari-${token.type}`, extra), children: value }, start));
67
+ start = stop;
68
+ }
69
+ return nodes;
70
+ };
71
+ const ranges = options.highlightLines ?? [];
72
+ if (!options.lines && ranges.length === 0)
73
+ return rangeNodes(0, source.length);
74
+ const isHighlighted = highlighted(ranges);
75
+ const nodes = [];
76
+ const newline = /\r\n|\r|\n/g;
77
+ let start = 0;
78
+ let number = 1;
79
+ do {
80
+ const match = newline.exec(source);
81
+ const end = match?.index ?? source.length;
82
+ const className = join('kirari-line', options.lineClassName?.({ number, start, end }), isHighlighted(number) ? 'kirari-line-highlighted' : undefined);
83
+ nodes.push(_jsx("span", { className: className, "data-line": number, children: rangeNodes(start, end) }, `line-${number}`));
84
+ if (!match)
85
+ break;
86
+ nodes.push(match[0]);
87
+ start = end + match[0].length;
88
+ number++;
89
+ } while (true);
90
+ return nodes;
91
+ };
92
+ /** Highlighted code as React elements. Uses no hooks, so it also renders as a Server Component. */
93
+ export const Code = ({ children = '', tokens, language = javascript, classes, tokenClassName, decorations, lines, lineClassName, highlightLines, className, ...props }) => {
94
+ const nodes = codeNodes(tokens ?? tokenize(children, language), {
95
+ ...(classes && { classes }),
96
+ ...(tokenClassName && { tokenClassName }),
97
+ ...(decorations && { decorations }),
98
+ ...(lines && { lines }),
99
+ ...(lineClassName && { lineClassName }),
100
+ ...(highlightLines && { highlightLines }),
101
+ });
102
+ return _jsx("pre", { ...props, className: join('kirari', className), children: _jsx("code", { children: nodes }) });
103
+ };
@@ -0,0 +1,11 @@
1
+ import type { Language, Token } from '@vortexlabs/kirari/core';
2
+ import type { TokenPatch } from '@vortexlabs/kirari/stream';
3
+ export interface StreamedTokens {
4
+ readonly tokens: readonly Token[];
5
+ write(chunk: string): void;
6
+ end(chunk?: string): void;
7
+ reset(): void;
8
+ }
9
+ export declare const applyPatch: (tokens: readonly Token[], patch: TokenPatch) => readonly Token[];
10
+ /** Tokens for source that arrives in chunks. Pass the result's `tokens` to `<Code tokens>`. */
11
+ export declare const useTokenStream: (language: Language) => StreamedTokens;
package/dist/stream.js ADDED
@@ -0,0 +1,24 @@
1
+ 'use client';
2
+ import { useCallback, useMemo, useState } from 'react';
3
+ import { createTokenStream } from '@vortexlabs/kirari/stream';
4
+ export const applyPatch = (tokens, patch) => {
5
+ return [...tokens.filter(token => token.end <= patch.from), ...patch.tokens];
6
+ };
7
+ /** Tokens for source that arrives in chunks. Pass the result's `tokens` to `<Code tokens>`. */
8
+ export const useTokenStream = (language) => {
9
+ const stream = useMemo(() => createTokenStream(language), [language]);
10
+ const [snapshot, setSnapshot] = useState({ stream, tokens: [] });
11
+ // A new language starts a new stream; tokens from the previous one no longer apply.
12
+ if (snapshot.stream !== stream)
13
+ setSnapshot({ stream, tokens: [] });
14
+ const apply = useCallback((patch) => {
15
+ setSnapshot(previous => previous.stream === stream ? { stream, tokens: applyPatch(previous.tokens, patch) } : previous);
16
+ }, [stream]);
17
+ const write = useCallback((chunk) => apply(stream.write(chunk)), [stream, apply]);
18
+ const end = useCallback((chunk) => apply(stream.end(chunk)), [stream, apply]);
19
+ const reset = useCallback(() => {
20
+ stream.reset();
21
+ setSnapshot({ stream, tokens: [] });
22
+ }, [stream]);
23
+ return { tokens: snapshot.stream === stream ? snapshot.tokens : [], write, end, reset };
24
+ };
package/package.json CHANGED
@@ -1,6 +1,65 @@
1
1
  {
2
2
  "name": "@vortexlabs/kirari-react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "Kirari syntax highlighting as React elements.",
5
+ "keywords": [
6
+ "react",
7
+ "react-component",
8
+ "syntax-highlighting",
9
+ "server-components"
10
+ ],
11
+ "author": "Ugi (https://ugi.dev)",
12
+ "license": "MIT",
13
+ "homepage": "https://kirari.vortex.design",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/vortexkit/kirari.git",
17
+ "directory": "packages/react"
18
+ },
19
+ "type": "module",
20
+ "sideEffects": false,
21
+ "files": [
22
+ "dist",
23
+ "README.md",
24
+ "LICENSE",
25
+ "CHANGELOG.md"
26
+ ],
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "import": "./dist/index.js"
31
+ },
32
+ "./stream": {
33
+ "types": "./dist/stream.d.ts",
34
+ "import": "./dist/stream.js"
35
+ }
36
+ },
37
+ "scripts": {
38
+ "build": "tsc -p tsconfig.build.json",
39
+ "test": "bun test ./test",
40
+ "typecheck": "tsc --noEmit",
41
+ "check": "bun run typecheck && bun run test && bun run build && bun test ./checks",
42
+ "prepublishOnly": "bun run build"
43
+ },
44
+ "peerDependencies": {
45
+ "@types/react": "^19",
46
+ "@vortexlabs/kirari": "^1.0.0",
47
+ "react": "^19"
48
+ },
49
+ "peerDependenciesMeta": {
50
+ "@types/react": {
51
+ "optional": true
52
+ }
53
+ },
54
+ "devDependencies": {
55
+ "@happy-dom/global-registrator": "^20.14.6",
56
+ "@types/react": "^19.3.0",
57
+ "@types/react-dom": "^19.3.0",
58
+ "@vortexlabs/kirari": "1.0.0",
59
+ "react": "^19.3.1",
60
+ "react-dom": "^19.3.1"
61
+ },
62
+ "publishConfig": {
63
+ "access": "public"
64
+ }
65
+ }