@ai-gui/plugin-highlight 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 +21 -0
- package/README.md +26 -0
- package/dist/index.cjs +86 -0
- package/dist/index.d.cts +26 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +62 -0
- package/package.json +52 -0
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,26 @@
|
|
|
1
|
+
# @ai-gui/plugin-highlight
|
|
2
|
+
|
|
3
|
+
Syntax-highlighting plugin for [AIGUI](../../README.md), powered by [Shiki](https://shiki.style). Overrides fenced code blocks with highlighted output. Async.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @ai-gui/plugin-highlight
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { highlight } from "@ai-gui/plugin-highlight"
|
|
15
|
+
import { AIRenderer } from "@ai-gui/react"
|
|
16
|
+
|
|
17
|
+
<AIRenderer plugins={[highlight({ themes: ["github-dark"], langs: ["ts", "python"] })]} />
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Options
|
|
21
|
+
|
|
22
|
+
- `themes?: string[]` — themes to load; the first is the default when `theme` is omitted.
|
|
23
|
+
- `langs?: string[]` — grammars to load; a code block whose language is not listed falls back to plain text.
|
|
24
|
+
- `theme?: string` — theme used for rendering; defaults to the first entry of `themes`.
|
|
25
|
+
|
|
26
|
+
See the [root README](../../README.md) for the full plugin list.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
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 shiki = __toESM(require("shiki"));
|
|
26
|
+
|
|
27
|
+
//#region src/index.ts
|
|
28
|
+
/** Escape a raw string for safe embedding inside `<pre><code>`. */
|
|
29
|
+
function escapeHtml(s) {
|
|
30
|
+
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Code-highlighting plugin backed by Shiki. Overrides the built-in `code` node
|
|
34
|
+
* renderer with an async renderer that lazily creates a single, memoized
|
|
35
|
+
* `Highlighter` (Shiki's `createHighlighter` promise is created at most once) and
|
|
36
|
+
* emits `highlighter.codeToHtml(...)` markup.
|
|
37
|
+
*
|
|
38
|
+
* A node whose `attrs.lang` is not among the loaded `langs` renders as plain
|
|
39
|
+
* `"text"` so Shiki never throws for an unloaded grammar. Any other failure is
|
|
40
|
+
* caught and rendered as an escaped `<pre><code>` block — the renderer never throws.
|
|
41
|
+
*/
|
|
42
|
+
function highlight(opts = {}) {
|
|
43
|
+
const themes = opts.themes ?? ["github-light"];
|
|
44
|
+
const langs = opts.langs ?? [
|
|
45
|
+
"ts",
|
|
46
|
+
"js",
|
|
47
|
+
"json",
|
|
48
|
+
"bash",
|
|
49
|
+
"python",
|
|
50
|
+
"html",
|
|
51
|
+
"css"
|
|
52
|
+
];
|
|
53
|
+
const theme = opts.theme ?? themes[0];
|
|
54
|
+
let highlighterPromise = null;
|
|
55
|
+
const getHighlighter = () => highlighterPromise ??= (0, shiki.createHighlighter)({
|
|
56
|
+
themes,
|
|
57
|
+
langs
|
|
58
|
+
});
|
|
59
|
+
const render = async (node) => {
|
|
60
|
+
const code = node.content ?? "";
|
|
61
|
+
const requested = node.attrs?.lang;
|
|
62
|
+
const lang = requested && langs.includes(requested) ? requested : "text";
|
|
63
|
+
try {
|
|
64
|
+
const highlighter = await getHighlighter();
|
|
65
|
+
return {
|
|
66
|
+
kind: "html",
|
|
67
|
+
html: highlighter.codeToHtml(code, {
|
|
68
|
+
lang,
|
|
69
|
+
theme
|
|
70
|
+
})
|
|
71
|
+
};
|
|
72
|
+
} catch {
|
|
73
|
+
return {
|
|
74
|
+
kind: "html",
|
|
75
|
+
html: `<pre><code>${escapeHtml(code)}</code></pre>`
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
return {
|
|
80
|
+
name: "highlight",
|
|
81
|
+
nodeRenderers: { code: render }
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
86
|
+
exports.highlight = highlight
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { AIGuiPlugin } from "@ai-gui/core";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
/** Options for the Shiki-backed code highlighter plugin. */
|
|
5
|
+
/** Options for the Shiki-backed code highlighter plugin. */
|
|
6
|
+
interface HighlightOptions {
|
|
7
|
+
/** Themes to load. First entry is the default when `theme` is omitted. */
|
|
8
|
+
themes?: string[];
|
|
9
|
+
/** Grammars to load. A node whose `attrs.lang` is not listed falls back to plain text. */
|
|
10
|
+
langs?: string[];
|
|
11
|
+
/** Theme used for rendering. Defaults to the first entry of `themes`. */
|
|
12
|
+
theme?: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Code-highlighting plugin backed by Shiki. Overrides the built-in `code` node
|
|
16
|
+
* renderer with an async renderer that lazily creates a single, memoized
|
|
17
|
+
* `Highlighter` (Shiki's `createHighlighter` promise is created at most once) and
|
|
18
|
+
* emits `highlighter.codeToHtml(...)` markup.
|
|
19
|
+
*
|
|
20
|
+
* A node whose `attrs.lang` is not among the loaded `langs` renders as plain
|
|
21
|
+
* `"text"` so Shiki never throws for an unloaded grammar. Any other failure is
|
|
22
|
+
* caught and rendered as an escaped `<pre><code>` block — the renderer never throws.
|
|
23
|
+
*/
|
|
24
|
+
declare function highlight(opts?: HighlightOptions): AIGuiPlugin;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { HighlightOptions, highlight };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { AIGuiPlugin } from "@ai-gui/core";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
/** Options for the Shiki-backed code highlighter plugin. */
|
|
5
|
+
/** Options for the Shiki-backed code highlighter plugin. */
|
|
6
|
+
interface HighlightOptions {
|
|
7
|
+
/** Themes to load. First entry is the default when `theme` is omitted. */
|
|
8
|
+
themes?: string[];
|
|
9
|
+
/** Grammars to load. A node whose `attrs.lang` is not listed falls back to plain text. */
|
|
10
|
+
langs?: string[];
|
|
11
|
+
/** Theme used for rendering. Defaults to the first entry of `themes`. */
|
|
12
|
+
theme?: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Code-highlighting plugin backed by Shiki. Overrides the built-in `code` node
|
|
16
|
+
* renderer with an async renderer that lazily creates a single, memoized
|
|
17
|
+
* `Highlighter` (Shiki's `createHighlighter` promise is created at most once) and
|
|
18
|
+
* emits `highlighter.codeToHtml(...)` markup.
|
|
19
|
+
*
|
|
20
|
+
* A node whose `attrs.lang` is not among the loaded `langs` renders as plain
|
|
21
|
+
* `"text"` so Shiki never throws for an unloaded grammar. Any other failure is
|
|
22
|
+
* caught and rendered as an escaped `<pre><code>` block — the renderer never throws.
|
|
23
|
+
*/
|
|
24
|
+
declare function highlight(opts?: HighlightOptions): AIGuiPlugin;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { HighlightOptions, highlight };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { createHighlighter } from "shiki";
|
|
2
|
+
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
/** Escape a raw string for safe embedding inside `<pre><code>`. */
|
|
5
|
+
function escapeHtml(s) {
|
|
6
|
+
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Code-highlighting plugin backed by Shiki. Overrides the built-in `code` node
|
|
10
|
+
* renderer with an async renderer that lazily creates a single, memoized
|
|
11
|
+
* `Highlighter` (Shiki's `createHighlighter` promise is created at most once) and
|
|
12
|
+
* emits `highlighter.codeToHtml(...)` markup.
|
|
13
|
+
*
|
|
14
|
+
* A node whose `attrs.lang` is not among the loaded `langs` renders as plain
|
|
15
|
+
* `"text"` so Shiki never throws for an unloaded grammar. Any other failure is
|
|
16
|
+
* caught and rendered as an escaped `<pre><code>` block — the renderer never throws.
|
|
17
|
+
*/
|
|
18
|
+
function highlight(opts = {}) {
|
|
19
|
+
const themes = opts.themes ?? ["github-light"];
|
|
20
|
+
const langs = opts.langs ?? [
|
|
21
|
+
"ts",
|
|
22
|
+
"js",
|
|
23
|
+
"json",
|
|
24
|
+
"bash",
|
|
25
|
+
"python",
|
|
26
|
+
"html",
|
|
27
|
+
"css"
|
|
28
|
+
];
|
|
29
|
+
const theme = opts.theme ?? themes[0];
|
|
30
|
+
let highlighterPromise = null;
|
|
31
|
+
const getHighlighter = () => highlighterPromise ??= createHighlighter({
|
|
32
|
+
themes,
|
|
33
|
+
langs
|
|
34
|
+
});
|
|
35
|
+
const render = async (node) => {
|
|
36
|
+
const code = node.content ?? "";
|
|
37
|
+
const requested = node.attrs?.lang;
|
|
38
|
+
const lang = requested && langs.includes(requested) ? requested : "text";
|
|
39
|
+
try {
|
|
40
|
+
const highlighter = await getHighlighter();
|
|
41
|
+
return {
|
|
42
|
+
kind: "html",
|
|
43
|
+
html: highlighter.codeToHtml(code, {
|
|
44
|
+
lang,
|
|
45
|
+
theme
|
|
46
|
+
})
|
|
47
|
+
};
|
|
48
|
+
} catch {
|
|
49
|
+
return {
|
|
50
|
+
kind: "html",
|
|
51
|
+
html: `<pre><code>${escapeHtml(code)}</code></pre>`
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
name: "highlight",
|
|
57
|
+
nodeRenderers: { code: render }
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
//#endregion
|
|
62
|
+
export { highlight };
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ai-gui/plugin-highlight",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shiki code-highlighting plugin for AIGUI.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"llm",
|
|
7
|
+
"ai",
|
|
8
|
+
"streaming",
|
|
9
|
+
"markdown",
|
|
10
|
+
"shiki",
|
|
11
|
+
"syntax-highlighting",
|
|
12
|
+
"code",
|
|
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-highlight"
|
|
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
|
+
"shiki": "^1.22.0",
|
|
46
|
+
"@ai-gui/core": "0.1.0"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "tsdown",
|
|
50
|
+
"typecheck": "tsc --noEmit"
|
|
51
|
+
}
|
|
52
|
+
}
|