@ai-gui/plugin-mermaid 0.1.0 → 0.3.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/README.md +3 -1
- package/dist/index.cjs +18 -9
- package/dist/index.js +18 -9
- package/package.json +15 -5
package/README.md
CHANGED
|
@@ -25,6 +25,8 @@ The model emits, e.g.:
|
|
|
25
25
|
|
|
26
26
|
## Options
|
|
27
27
|
|
|
28
|
-
- `theme?: string` — Mermaid theme.
|
|
28
|
+
- `theme?: string` — Mermaid theme. Mermaid has process-global configuration, so the theme of the first render wins across plugin instances; later instances share that initialization.
|
|
29
|
+
|
|
30
|
+
Renders are queued across instances because Mermaid mutates global state while rendering. This also guarantees unique diagram IDs for concurrent renders.
|
|
29
31
|
|
|
30
32
|
See the [root README](../../README.md) for the full plugin list.
|
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
//#region src/index.ts
|
|
4
|
+
let nextId = 0;
|
|
5
|
+
let mermaidPromise = null;
|
|
6
|
+
let initializedTheme;
|
|
7
|
+
let renderQueue = Promise.resolve();
|
|
8
|
+
const loadMermaid = () => mermaidPromise ??= import("mermaid").then(({ default: m }) => m);
|
|
9
|
+
function enqueue(work) {
|
|
10
|
+
const result = renderQueue.then(work, work);
|
|
11
|
+
renderQueue = result.then(() => void 0, () => void 0);
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
4
14
|
function escapeHtml(s) {
|
|
5
15
|
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
6
16
|
}
|
|
@@ -11,19 +21,18 @@ function errorHtml(message) {
|
|
|
11
21
|
};
|
|
12
22
|
}
|
|
13
23
|
function mermaid(opts = {}) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const render = async (node) => {
|
|
24
|
+
const theme = opts.theme ?? "default";
|
|
25
|
+
const render = (node) => enqueue(async () => {
|
|
17
26
|
try {
|
|
18
|
-
const m =
|
|
19
|
-
if (
|
|
27
|
+
const m = await loadMermaid();
|
|
28
|
+
if (initializedTheme === void 0) {
|
|
20
29
|
m.initialize({
|
|
21
30
|
startOnLoad: false,
|
|
22
|
-
theme
|
|
31
|
+
theme
|
|
23
32
|
});
|
|
24
|
-
|
|
33
|
+
initializedTheme = theme;
|
|
25
34
|
}
|
|
26
|
-
const id = `aigui-mermaid-${
|
|
35
|
+
const id = `aigui-mermaid-${nextId++}`;
|
|
27
36
|
const { svg } = await m.render(id, node.content ?? "");
|
|
28
37
|
return {
|
|
29
38
|
kind: "html",
|
|
@@ -32,7 +41,7 @@ function mermaid(opts = {}) {
|
|
|
32
41
|
} catch (e) {
|
|
33
42
|
return errorHtml(String(e?.message ?? e));
|
|
34
43
|
}
|
|
35
|
-
};
|
|
44
|
+
});
|
|
36
45
|
return {
|
|
37
46
|
name: "mermaid",
|
|
38
47
|
nodeRenderers: { mermaid: render }
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
//#region src/index.ts
|
|
2
|
+
let nextId = 0;
|
|
3
|
+
let mermaidPromise = null;
|
|
4
|
+
let initializedTheme;
|
|
5
|
+
let renderQueue = Promise.resolve();
|
|
6
|
+
const loadMermaid = () => mermaidPromise ??= import("mermaid").then(({ default: m }) => m);
|
|
7
|
+
function enqueue(work) {
|
|
8
|
+
const result = renderQueue.then(work, work);
|
|
9
|
+
renderQueue = result.then(() => void 0, () => void 0);
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
2
12
|
function escapeHtml(s) {
|
|
3
13
|
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
4
14
|
}
|
|
@@ -9,19 +19,18 @@ function errorHtml(message) {
|
|
|
9
19
|
};
|
|
10
20
|
}
|
|
11
21
|
function mermaid(opts = {}) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const render = async (node) => {
|
|
22
|
+
const theme = opts.theme ?? "default";
|
|
23
|
+
const render = (node) => enqueue(async () => {
|
|
15
24
|
try {
|
|
16
|
-
const m =
|
|
17
|
-
if (
|
|
25
|
+
const m = await loadMermaid();
|
|
26
|
+
if (initializedTheme === void 0) {
|
|
18
27
|
m.initialize({
|
|
19
28
|
startOnLoad: false,
|
|
20
|
-
theme
|
|
29
|
+
theme
|
|
21
30
|
});
|
|
22
|
-
|
|
31
|
+
initializedTheme = theme;
|
|
23
32
|
}
|
|
24
|
-
const id = `aigui-mermaid-${
|
|
33
|
+
const id = `aigui-mermaid-${nextId++}`;
|
|
25
34
|
const { svg } = await m.render(id, node.content ?? "");
|
|
26
35
|
return {
|
|
27
36
|
kind: "html",
|
|
@@ -30,7 +39,7 @@ function mermaid(opts = {}) {
|
|
|
30
39
|
} catch (e) {
|
|
31
40
|
return errorHtml(String(e?.message ?? e));
|
|
32
41
|
}
|
|
33
|
-
};
|
|
42
|
+
});
|
|
34
43
|
return {
|
|
35
44
|
name: "mermaid",
|
|
36
45
|
nodeRenderers: { mermaid: render }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-gui/plugin-mermaid",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Mermaid diagram plugin for AIGUI.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"llm",
|
|
@@ -23,14 +23,23 @@
|
|
|
23
23
|
"homepage": "https://github.com/liliang-cn/aigui#readme",
|
|
24
24
|
"bugs": "https://github.com/liliang-cn/aigui/issues",
|
|
25
25
|
"type": "module",
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=18"
|
|
29
|
+
},
|
|
26
30
|
"main": "./dist/index.cjs",
|
|
27
31
|
"module": "./dist/index.js",
|
|
28
32
|
"types": "./dist/index.d.ts",
|
|
29
33
|
"exports": {
|
|
30
34
|
".": {
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
"import": {
|
|
36
|
+
"types": "./dist/index.d.ts",
|
|
37
|
+
"default": "./dist/index.js"
|
|
38
|
+
},
|
|
39
|
+
"require": {
|
|
40
|
+
"types": "./dist/index.d.cts",
|
|
41
|
+
"default": "./dist/index.cjs"
|
|
42
|
+
}
|
|
34
43
|
}
|
|
35
44
|
},
|
|
36
45
|
"files": [
|
|
@@ -43,10 +52,11 @@
|
|
|
43
52
|
},
|
|
44
53
|
"dependencies": {
|
|
45
54
|
"mermaid": "^11.4.0",
|
|
46
|
-
"@ai-gui/core": "0.
|
|
55
|
+
"@ai-gui/core": "0.3.0"
|
|
47
56
|
},
|
|
48
57
|
"scripts": {
|
|
49
58
|
"build": "tsdown",
|
|
59
|
+
"test": "pnpm --dir ../.. exec vitest run --project plugin-mermaid",
|
|
50
60
|
"typecheck": "tsc --noEmit"
|
|
51
61
|
}
|
|
52
62
|
}
|