@ai-gui/plugin-katex 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Liang Li
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 ADDED
@@ -0,0 +1,22 @@
1
+ # @ai-gui/plugin-katex
2
+
3
+ KaTeX math plugin for [AIGUI](../../README.md). Renders inline `$…$` and block `$$…$$` math.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ pnpm add @ai-gui/plugin-katex
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```tsx
14
+ import { katex } from "@ai-gui/plugin-katex"
15
+ import { AIRenderer } from "@ai-gui/react"
16
+
17
+ <AIRenderer plugins={[katex()]} />
18
+ ```
19
+
20
+ The model can then emit `$E = mc^2$` inline or a `$$…$$` block, and it renders as math. Include KaTeX's stylesheet in your app (the package exports `katexCss` for convenience).
21
+
22
+ See the [root README](../../README.md) for the full plugin list.
package/dist/index.cjs ADDED
@@ -0,0 +1,166 @@
1
+ "use strict";
2
+ //#region rolldown:runtime
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
+ get: ((k) => from[k]).bind(null, key),
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
20
+ value: mod,
21
+ enumerable: true
22
+ }) : target, mod));
23
+
24
+ //#endregion
25
+ const katex = __toESM(require("katex"));
26
+
27
+ //#region src/index.ts
28
+ /**
29
+ * Render a TeX expression to a KaTeX HTML string.
30
+ *
31
+ * Uses `output: "html"` (no MathML) so the markup is plain `<span class="katex">`
32
+ * elements that survive DOMPurify sanitization in the core Renderer. Never throws:
33
+ * invalid input is rendered as a red error node via `throwOnError: false`.
34
+ */
35
+ function renderMath(expr, displayMode) {
36
+ return katex.default.renderToString(expr, {
37
+ throwOnError: false,
38
+ output: "html",
39
+ displayMode
40
+ });
41
+ }
42
+ /**
43
+ * Decide whether a `$` at `pos` may open and/or close an inline math span.
44
+ * Mirrors the markdown-it-katex delimiter rules: a closer may not sit right
45
+ * after whitespace or right before a digit; an opener may not precede whitespace.
46
+ */
47
+ function isValidDelim(state, pos) {
48
+ const max = state.posMax;
49
+ const prevChar = pos > 0 ? state.src.charCodeAt(pos - 1) : -1;
50
+ const nextChar = pos + 1 <= max ? state.src.charCodeAt(pos + 1) : -1;
51
+ let canOpen = true;
52
+ let canClose = true;
53
+ if (prevChar === 32 || prevChar === 9 || nextChar >= 48 && nextChar <= 57) canClose = false;
54
+ if (nextChar === 32 || nextChar === 9) canOpen = false;
55
+ return {
56
+ canOpen,
57
+ canClose
58
+ };
59
+ }
60
+ /** Inline rule for `$...$`. */
61
+ function mathInline(state, silent) {
62
+ if (state.src[state.pos] !== "$") return false;
63
+ let res = isValidDelim(state, state.pos);
64
+ if (!res.canOpen) {
65
+ if (!silent) state.pending += "$";
66
+ state.pos += 1;
67
+ return true;
68
+ }
69
+ const start = state.pos + 1;
70
+ let match = start;
71
+ let pos;
72
+ while ((match = state.src.indexOf("$", match)) !== -1) {
73
+ pos = match - 1;
74
+ while (state.src[pos] === "\\") pos -= 1;
75
+ if ((match - pos) % 2 === 1) break;
76
+ match += 1;
77
+ }
78
+ if (match === -1) {
79
+ if (!silent) state.pending += "$";
80
+ state.pos = start;
81
+ return true;
82
+ }
83
+ if (match - start === 0) {
84
+ if (!silent) state.pending += "$$";
85
+ state.pos = start + 1;
86
+ return true;
87
+ }
88
+ res = isValidDelim(state, match);
89
+ if (!res.canClose) {
90
+ if (!silent) state.pending += "$";
91
+ state.pos = start;
92
+ return true;
93
+ }
94
+ if (!silent) {
95
+ const token = state.push("math_inline", "math", 0);
96
+ token.markup = "$";
97
+ token.content = state.src.slice(start, match);
98
+ }
99
+ state.pos = match + 1;
100
+ return true;
101
+ }
102
+ /** Block rule for `$$...$$` (single or multi line). */
103
+ function mathBlock(state, start, end, silent) {
104
+ let firstLine;
105
+ let lastLine = "";
106
+ let next;
107
+ let lastPos;
108
+ let found = false;
109
+ let pos = state.bMarks[start] + state.tShift[start];
110
+ let max = state.eMarks[start];
111
+ if (pos + 2 > max) return false;
112
+ if (state.src.slice(pos, pos + 2) !== "$$") return false;
113
+ pos += 2;
114
+ firstLine = state.src.slice(pos, max);
115
+ if (silent) return true;
116
+ if (firstLine.trim().slice(-2) === "$$") {
117
+ firstLine = firstLine.trim().slice(0, -2);
118
+ found = true;
119
+ }
120
+ for (next = start; !found;) {
121
+ next++;
122
+ if (next >= end) break;
123
+ pos = state.bMarks[next] + state.tShift[next];
124
+ max = state.eMarks[next];
125
+ if (pos < max && state.tShift[next] < state.blkIndent) break;
126
+ if (state.src.slice(pos, max).trim().slice(-2) === "$$") {
127
+ lastPos = state.src.slice(0, max).lastIndexOf("$$");
128
+ lastLine = state.src.slice(pos, lastPos);
129
+ found = true;
130
+ }
131
+ }
132
+ state.line = next + 1;
133
+ const token = state.push("math_block", "math", 0);
134
+ token.block = true;
135
+ token.content = (firstLine.trim() ? firstLine + "\n" : "") + state.getLines(start + 1, next, state.tShift[start], true) + (lastLine.trim() ? lastLine : "");
136
+ token.map = [start, state.line];
137
+ token.markup = "$$";
138
+ return true;
139
+ }
140
+ /** KaTeX stylesheet import hint — consumers must load KaTeX's CSS for correct layout. */
141
+ const katexCss = "@import \"katex/dist/katex.min.css\";";
142
+ /**
143
+ * KaTeX plugin: renders inline `$...$` and block `$$...$$` math to HTML during
144
+ * markdown parsing, flowing through the core Renderer's sanitized `html` pipeline.
145
+ */
146
+ function katex$1() {
147
+ return {
148
+ name: "katex",
149
+ css: katexCss,
150
+ extendParser: (md) => {
151
+ md.inline.ruler.after("escape", "math_inline", mathInline);
152
+ md.block.ruler.after("blockquote", "math_block", mathBlock, { alt: [
153
+ "paragraph",
154
+ "reference",
155
+ "blockquote",
156
+ "list"
157
+ ] });
158
+ md.renderer.rules.math_inline = (tokens, idx) => renderMath(tokens[idx].content, false);
159
+ md.renderer.rules.math_block = (tokens, idx) => renderMath(tokens[idx].content, true) + "\n";
160
+ }
161
+ };
162
+ }
163
+
164
+ //#endregion
165
+ exports.katex = katex$1
166
+ exports.katexCss = katexCss
@@ -0,0 +1,14 @@
1
+ import { AIGuiPlugin } from "@ai-gui/core";
2
+
3
+ //#region src/index.d.ts
4
+ /** KaTeX stylesheet import hint — consumers must load KaTeX's CSS for correct layout. */
5
+ /** KaTeX stylesheet import hint — consumers must load KaTeX's CSS for correct layout. */
6
+ declare const katexCss = "@import \"katex/dist/katex.min.css\";";
7
+ /**
8
+ * KaTeX plugin: renders inline `$...$` and block `$$...$$` math to HTML during
9
+ * markdown parsing, flowing through the core Renderer's sanitized `html` pipeline.
10
+ */
11
+ declare function katex(): AIGuiPlugin;
12
+
13
+ //#endregion
14
+ export { katex, katexCss };
@@ -0,0 +1,14 @@
1
+ import { AIGuiPlugin } from "@ai-gui/core";
2
+
3
+ //#region src/index.d.ts
4
+ /** KaTeX stylesheet import hint — consumers must load KaTeX's CSS for correct layout. */
5
+ /** KaTeX stylesheet import hint — consumers must load KaTeX's CSS for correct layout. */
6
+ declare const katexCss = "@import \"katex/dist/katex.min.css\";";
7
+ /**
8
+ * KaTeX plugin: renders inline `$...$` and block `$$...$$` math to HTML during
9
+ * markdown parsing, flowing through the core Renderer's sanitized `html` pipeline.
10
+ */
11
+ declare function katex(): AIGuiPlugin;
12
+
13
+ //#endregion
14
+ export { katex, katexCss };
package/dist/index.js ADDED
@@ -0,0 +1,141 @@
1
+ import katexLib from "katex";
2
+
3
+ //#region src/index.ts
4
+ /**
5
+ * Render a TeX expression to a KaTeX HTML string.
6
+ *
7
+ * Uses `output: "html"` (no MathML) so the markup is plain `<span class="katex">`
8
+ * elements that survive DOMPurify sanitization in the core Renderer. Never throws:
9
+ * invalid input is rendered as a red error node via `throwOnError: false`.
10
+ */
11
+ function renderMath(expr, displayMode) {
12
+ return katexLib.renderToString(expr, {
13
+ throwOnError: false,
14
+ output: "html",
15
+ displayMode
16
+ });
17
+ }
18
+ /**
19
+ * Decide whether a `$` at `pos` may open and/or close an inline math span.
20
+ * Mirrors the markdown-it-katex delimiter rules: a closer may not sit right
21
+ * after whitespace or right before a digit; an opener may not precede whitespace.
22
+ */
23
+ function isValidDelim(state, pos) {
24
+ const max = state.posMax;
25
+ const prevChar = pos > 0 ? state.src.charCodeAt(pos - 1) : -1;
26
+ const nextChar = pos + 1 <= max ? state.src.charCodeAt(pos + 1) : -1;
27
+ let canOpen = true;
28
+ let canClose = true;
29
+ if (prevChar === 32 || prevChar === 9 || nextChar >= 48 && nextChar <= 57) canClose = false;
30
+ if (nextChar === 32 || nextChar === 9) canOpen = false;
31
+ return {
32
+ canOpen,
33
+ canClose
34
+ };
35
+ }
36
+ /** Inline rule for `$...$`. */
37
+ function mathInline(state, silent) {
38
+ if (state.src[state.pos] !== "$") return false;
39
+ let res = isValidDelim(state, state.pos);
40
+ if (!res.canOpen) {
41
+ if (!silent) state.pending += "$";
42
+ state.pos += 1;
43
+ return true;
44
+ }
45
+ const start = state.pos + 1;
46
+ let match = start;
47
+ let pos;
48
+ while ((match = state.src.indexOf("$", match)) !== -1) {
49
+ pos = match - 1;
50
+ while (state.src[pos] === "\\") pos -= 1;
51
+ if ((match - pos) % 2 === 1) break;
52
+ match += 1;
53
+ }
54
+ if (match === -1) {
55
+ if (!silent) state.pending += "$";
56
+ state.pos = start;
57
+ return true;
58
+ }
59
+ if (match - start === 0) {
60
+ if (!silent) state.pending += "$$";
61
+ state.pos = start + 1;
62
+ return true;
63
+ }
64
+ res = isValidDelim(state, match);
65
+ if (!res.canClose) {
66
+ if (!silent) state.pending += "$";
67
+ state.pos = start;
68
+ return true;
69
+ }
70
+ if (!silent) {
71
+ const token = state.push("math_inline", "math", 0);
72
+ token.markup = "$";
73
+ token.content = state.src.slice(start, match);
74
+ }
75
+ state.pos = match + 1;
76
+ return true;
77
+ }
78
+ /** Block rule for `$$...$$` (single or multi line). */
79
+ function mathBlock(state, start, end, silent) {
80
+ let firstLine;
81
+ let lastLine = "";
82
+ let next;
83
+ let lastPos;
84
+ let found = false;
85
+ let pos = state.bMarks[start] + state.tShift[start];
86
+ let max = state.eMarks[start];
87
+ if (pos + 2 > max) return false;
88
+ if (state.src.slice(pos, pos + 2) !== "$$") return false;
89
+ pos += 2;
90
+ firstLine = state.src.slice(pos, max);
91
+ if (silent) return true;
92
+ if (firstLine.trim().slice(-2) === "$$") {
93
+ firstLine = firstLine.trim().slice(0, -2);
94
+ found = true;
95
+ }
96
+ for (next = start; !found;) {
97
+ next++;
98
+ if (next >= end) break;
99
+ pos = state.bMarks[next] + state.tShift[next];
100
+ max = state.eMarks[next];
101
+ if (pos < max && state.tShift[next] < state.blkIndent) break;
102
+ if (state.src.slice(pos, max).trim().slice(-2) === "$$") {
103
+ lastPos = state.src.slice(0, max).lastIndexOf("$$");
104
+ lastLine = state.src.slice(pos, lastPos);
105
+ found = true;
106
+ }
107
+ }
108
+ state.line = next + 1;
109
+ const token = state.push("math_block", "math", 0);
110
+ token.block = true;
111
+ token.content = (firstLine.trim() ? firstLine + "\n" : "") + state.getLines(start + 1, next, state.tShift[start], true) + (lastLine.trim() ? lastLine : "");
112
+ token.map = [start, state.line];
113
+ token.markup = "$$";
114
+ return true;
115
+ }
116
+ /** KaTeX stylesheet import hint — consumers must load KaTeX's CSS for correct layout. */
117
+ const katexCss = "@import \"katex/dist/katex.min.css\";";
118
+ /**
119
+ * KaTeX plugin: renders inline `$...$` and block `$$...$$` math to HTML during
120
+ * markdown parsing, flowing through the core Renderer's sanitized `html` pipeline.
121
+ */
122
+ function katex() {
123
+ return {
124
+ name: "katex",
125
+ css: katexCss,
126
+ extendParser: (md) => {
127
+ md.inline.ruler.after("escape", "math_inline", mathInline);
128
+ md.block.ruler.after("blockquote", "math_block", mathBlock, { alt: [
129
+ "paragraph",
130
+ "reference",
131
+ "blockquote",
132
+ "list"
133
+ ] });
134
+ md.renderer.rules.math_inline = (tokens, idx) => renderMath(tokens[idx].content, false);
135
+ md.renderer.rules.math_block = (tokens, idx) => renderMath(tokens[idx].content, true) + "\n";
136
+ }
137
+ };
138
+ }
139
+
140
+ //#endregion
141
+ export { katex, katexCss };
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@ai-gui/plugin-katex",
3
+ "version": "0.1.0",
4
+ "description": "KaTeX math rendering plugin for AIGUI.",
5
+ "keywords": [
6
+ "llm",
7
+ "ai",
8
+ "streaming",
9
+ "markdown",
10
+ "katex",
11
+ "math",
12
+ "latex",
13
+ "plugin",
14
+ "aigui"
15
+ ],
16
+ "license": "MIT",
17
+ "author": "Liang Li <ll_faw@hotmail.com>",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/liliang-cn/aigui.git",
21
+ "directory": "packages/plugin-katex"
22
+ },
23
+ "homepage": "https://github.com/liliang-cn/aigui#readme",
24
+ "bugs": "https://github.com/liliang-cn/aigui/issues",
25
+ "type": "module",
26
+ "main": "./dist/index.cjs",
27
+ "module": "./dist/index.js",
28
+ "types": "./dist/index.d.ts",
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "import": "./dist/index.js",
33
+ "require": "./dist/index.cjs"
34
+ }
35
+ },
36
+ "files": [
37
+ "dist",
38
+ "README.md",
39
+ "LICENSE"
40
+ ],
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "dependencies": {
45
+ "katex": "^0.16.11",
46
+ "@ai-gui/core": "0.1.0"
47
+ },
48
+ "devDependencies": {
49
+ "@types/katex": "^0.16.7",
50
+ "@types/markdown-it": "^14.1.2"
51
+ },
52
+ "scripts": {
53
+ "build": "tsdown",
54
+ "typecheck": "tsc --noEmit"
55
+ }
56
+ }