@yassimba/pi-loom-mermaid 0.2.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 +28 -0
- package/index.ts +1 -0
- package/package.json +57 -0
- package/src/index.ts +103 -0
- package/src/loom-mermaid/ansi.ts +65 -0
- package/src/loom-mermaid/canvas.ts +466 -0
- package/src/loom-mermaid/class-style.ts +65 -0
- package/src/loom-mermaid/css-colors.ts +153 -0
- package/src/loom-mermaid/diagrams/class.ts +297 -0
- package/src/loom-mermaid/diagrams/er.ts +189 -0
- package/src/loom-mermaid/diagrams/flowchart.ts +519 -0
- package/src/loom-mermaid/diagrams/gitgraph.ts +230 -0
- package/src/loom-mermaid/diagrams/mindmap.ts +107 -0
- package/src/loom-mermaid/diagrams/pie.ts +121 -0
- package/src/loom-mermaid/diagrams/sequence.ts +305 -0
- package/src/loom-mermaid/diagrams/state.ts +279 -0
- package/src/loom-mermaid/diagrams/timeline.ts +117 -0
- package/src/loom-mermaid/graph-render.ts +195 -0
- package/src/loom-mermaid/graph.ts +205 -0
- package/src/loom-mermaid/index.ts +78 -0
- package/src/loom-mermaid/labels.ts +353 -0
- package/src/loom-mermaid/layout-seq.ts +234 -0
- package/src/loom-mermaid/layout.ts +1749 -0
- package/src/loom-mermaid/paint.ts +325 -0
- package/src/loom-mermaid/placement.ts +255 -0
- package/src/loom-mermaid/registry.ts +88 -0
- package/src/loom-mermaid/source-box.ts +111 -0
- package/src/loom-mermaid/statements.ts +228 -0
- package/src/loom-mermaid/types.ts +64 -0
- package/src/loom-mermaid/width-data.ts +993 -0
- package/src/loom-mermaid/width.ts +69 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yassin Chibrani
|
|
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,28 @@
|
|
|
1
|
+
# Colored Mermaid diagrams for Pi
|
|
2
|
+
|
|
3
|
+
Render Mermaid blocks as Unicode terminal diagrams with per-node colors.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pi install npm:@yassimba/pi-loom-mermaid
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Disable Pi's built-in Mermaid transformer in `~/.pi/agent/settings.json` so this extension receives the original Mermaid source:
|
|
12
|
+
|
|
13
|
+
```json
|
|
14
|
+
{
|
|
15
|
+
"markdown": {
|
|
16
|
+
"mermaid": "off"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Run `/reload`, then mark diff nodes with the built-in `red`, `orange`, and `green` classes:
|
|
22
|
+
|
|
23
|
+
```mermaid
|
|
24
|
+
flowchart LR
|
|
25
|
+
A[Removed]:::red --> B[Changed]:::orange --> C[Added]:::green
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The built-in classes dim and color only the box border; backgrounds and text keep Pi's normal theme. Standard Mermaid `classDef` declarations can override these defaults or add other colors. Diagrams wider than the terminal remain Mermaid source blocks.
|
package/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "./src/index.ts";
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yassimba/pi-loom-mermaid",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Loom's advanced Mermaid renderer for Pi, with better routing and color.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=22.6"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"pi-package",
|
|
12
|
+
"pi",
|
|
13
|
+
"pi-coding-agent",
|
|
14
|
+
"mermaid",
|
|
15
|
+
"terminal"
|
|
16
|
+
],
|
|
17
|
+
"files": [
|
|
18
|
+
"index.ts",
|
|
19
|
+
"src",
|
|
20
|
+
"README.md",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"LOVELY-MERMAID-LICENSE",
|
|
23
|
+
"UPSTREAM.md"
|
|
24
|
+
],
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/Yassimba/loom.git",
|
|
28
|
+
"directory": "plugins/pi-loom-mermaid"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"pi": {
|
|
34
|
+
"extensions": [
|
|
35
|
+
"./index.ts"
|
|
36
|
+
]
|
|
37
|
+
},
|
|
38
|
+
"loom": {
|
|
39
|
+
"catalog": {
|
|
40
|
+
"label": "Colored Mermaid diagrams",
|
|
41
|
+
"description": "Render Mermaid diagrams with per-node colors in Pi using classDef styles.",
|
|
42
|
+
"nextAction": "Set markdown.mermaid to off in Pi settings, run /reload, then use classDef styles in a Mermaid block."
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
47
|
+
"@earendil-works/pi-tui": "*"
|
|
48
|
+
},
|
|
49
|
+
"peerDependenciesMeta": {
|
|
50
|
+
"@earendil-works/pi-coding-agent": {
|
|
51
|
+
"optional": true
|
|
52
|
+
},
|
|
53
|
+
"@earendil-works/pi-tui": {
|
|
54
|
+
"optional": true
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Marked, type Token } from "@earendil-works/pi-tui";
|
|
3
|
+
import { render, toAnsi } from "./loom-mermaid/index.ts";
|
|
4
|
+
|
|
5
|
+
const markdownParser = new Marked();
|
|
6
|
+
const diffClasses = {
|
|
7
|
+
red: { definition: "classDef red stroke:#9f5555", sgr: "38;2;159;85;85" },
|
|
8
|
+
orange: { definition: "classDef orange stroke:#9a7438", sgr: "38;2;154;116;56" },
|
|
9
|
+
green: { definition: "classDef green stroke:#4f8560", sgr: "38;2;79;133;96" },
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type TransformContext = {
|
|
13
|
+
messageType: "user" | "assistant" | "assistant-thinking";
|
|
14
|
+
availableWidth: number;
|
|
15
|
+
/** The message is still arriving; an unclosed fence shows as source until it closes. */
|
|
16
|
+
isStreaming?: boolean;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Rendered blocks by source and width. Pi runs the transformer on the whole
|
|
21
|
+
* message for every streamed chunk and every redraw, so a diagram would be
|
|
22
|
+
* laid out again for each token after it. Layout is deterministic, so the
|
|
23
|
+
* first render is the only one needed.
|
|
24
|
+
*/
|
|
25
|
+
const rendered = new Map<string, string>();
|
|
26
|
+
const CACHE_SIZE = 64;
|
|
27
|
+
|
|
28
|
+
function renderBlock(text: string, availableWidth: number): string | null {
|
|
29
|
+
const key = `${availableWidth}\0${text}`;
|
|
30
|
+
const hit = rendered.get(key);
|
|
31
|
+
if (hit !== undefined) {
|
|
32
|
+
rendered.delete(key);
|
|
33
|
+
rendered.set(key, hit);
|
|
34
|
+
return hit;
|
|
35
|
+
}
|
|
36
|
+
const styledSource = withDiffClasses(text);
|
|
37
|
+
const art = render(styledSource.source, { maxWidth: availableWidth });
|
|
38
|
+
if (!art || art.width > availableWidth) return null;
|
|
39
|
+
const out = `${dimDefaultBorders(toAnsi(art), styledSource.dimSgr).map(codeSpan).join(" \n")}\n`;
|
|
40
|
+
rendered.set(key, out);
|
|
41
|
+
if (rendered.size > CACHE_SIZE) rendered.delete(rendered.keys().next().value as string);
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** A fenced block whose closing fence has arrived. */
|
|
46
|
+
const isClosed = (token: { raw: string }): boolean => /\n\s*(`{3,}|~{3,})\s*$/.test(token.raw);
|
|
47
|
+
|
|
48
|
+
function isMermaid(token: Token): token is Token & { type: "code"; text: string; lang?: string } {
|
|
49
|
+
return (
|
|
50
|
+
token.type === "code" && token.lang?.trim().split(/\s+/, 1)[0]?.toLowerCase() === "mermaid"
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function withDiffClasses(source: string): { source: string; dimSgr: string[] } {
|
|
55
|
+
const defaults = Object.entries(diffClasses).filter(([name]) => {
|
|
56
|
+
const used = new RegExp(`:::\\s*${name}\\b|\\bclass\\s+[^\\n]+\\s+${name}\\b`).test(source);
|
|
57
|
+
return used && !new RegExp(`\\bclassDef\\s+${name}\\b`).test(source);
|
|
58
|
+
});
|
|
59
|
+
return {
|
|
60
|
+
source:
|
|
61
|
+
defaults.length > 0
|
|
62
|
+
? `${source}\n${defaults.map(([, style]) => style.definition).join("\n")}`
|
|
63
|
+
: source,
|
|
64
|
+
dimSgr: defaults.map(([, style]) => style.sgr),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function dimDefaultBorders(lines: string[], sgrValues: string[]): string[] {
|
|
69
|
+
return lines.map((line) =>
|
|
70
|
+
sgrValues.reduce(
|
|
71
|
+
(result, sgr) => result.replaceAll(`\u001b[${sgr}m`, `\u001b[2;${sgr}m`),
|
|
72
|
+
line,
|
|
73
|
+
),
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function codeSpan(line: string): string {
|
|
78
|
+
const content = line || "\u00a0";
|
|
79
|
+
const longestRun = Math.max(
|
|
80
|
+
0,
|
|
81
|
+
...Array.from(content.matchAll(/`+/g), (match) => match[0].length),
|
|
82
|
+
);
|
|
83
|
+
const fence = "`".repeat(longestRun + 1);
|
|
84
|
+
const padding = content.startsWith("`") || content.endsWith("`") ? " " : "";
|
|
85
|
+
return `${fence}${padding}${content}${padding}${fence}`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function transformMermaidMarkdown(markdown: string, context: TransformContext): string {
|
|
89
|
+
if (context.messageType === "assistant-thinking") return markdown;
|
|
90
|
+
|
|
91
|
+
return markdownParser
|
|
92
|
+
.lexer(markdown)
|
|
93
|
+
.map((token) => {
|
|
94
|
+
if (!isMermaid(token)) return token.raw;
|
|
95
|
+
if (context.isStreaming === true && !isClosed(token)) return token.raw;
|
|
96
|
+
return renderBlock(token.text, context.availableWidth) ?? token.raw;
|
|
97
|
+
})
|
|
98
|
+
.join("");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export default function piLovelyMermaid(pi: ExtensionAPI): void {
|
|
102
|
+
pi.registerMarkdownTransformer(transformMermaidMarkdown);
|
|
103
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { type ClassStyle, contrastOn, resolveClassStyle } from './class-style.ts'
|
|
2
|
+
import type { MermaidArt, Role } from './types.ts'
|
|
3
|
+
|
|
4
|
+
const ESC = String.fromCharCode(27)
|
|
5
|
+
const OSC8 = `${ESC}]8;;`
|
|
6
|
+
const ST = `${ESC}\\`
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* SGR parameter per role, e.g. `'2'` for dim, `'36'` for cyan,
|
|
10
|
+
* `'38;5;244'` for a 256-colour index. A role left out is printed unstyled.
|
|
11
|
+
*/
|
|
12
|
+
export type AnsiTheme = Partial<Record<Role, string>>
|
|
13
|
+
|
|
14
|
+
/** Dim frame, plain labels, cyan connectors. Readable on light and dark. */
|
|
15
|
+
export const DEFAULT_THEME: AnsiTheme = {
|
|
16
|
+
border: '2',
|
|
17
|
+
edge: '36',
|
|
18
|
+
edgeLabel: '2;36',
|
|
19
|
+
title: '1',
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const rgb = (hex: string, sgr: 38 | 48): string =>
|
|
23
|
+
`${sgr};2;${[1, 3, 5].map((i) => Number.parseInt(hex.slice(i, i + 2), 16)).join(';')}`
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The truecolor SGR a class style gives a span of the given role, or
|
|
27
|
+
* undefined when the style says nothing about it (fall back to the theme).
|
|
28
|
+
* `stroke` colors borders, `color` text; `fill` backs every painted cell,
|
|
29
|
+
* with a black/white foreground picked for contrast when none was declared.
|
|
30
|
+
* A style that colors nothing for this role keeps `fallback` (the theme's
|
|
31
|
+
* SGR), so a bold-only class bolds the themed look instead of replacing it.
|
|
32
|
+
*/
|
|
33
|
+
export function classSgr(st: ClassStyle, role: Role, fallback?: string): string | undefined {
|
|
34
|
+
const p: string[] = []
|
|
35
|
+
const fg = role === 'border' ? (st.stroke ?? st.color) : role === 'edge' ? undefined : st.color
|
|
36
|
+
const backed = fg ?? (st.fill === undefined ? undefined : contrastOn(st.fill))
|
|
37
|
+
if (backed !== undefined) p.push(rgb(backed, 38))
|
|
38
|
+
if (st.fill !== undefined) p.push(rgb(st.fill, 48))
|
|
39
|
+
if (p.length === 0 && fallback !== undefined) p.push(fallback)
|
|
40
|
+
if (st.bold === true) p.unshift('1')
|
|
41
|
+
return p.length > 0 ? p.join(';') : undefined
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Render art to ANSI-coloured lines. Spans that carry author classes are
|
|
46
|
+
* styled from `art.classDefs` (best effort — see `resolveClassStyle`),
|
|
47
|
+
* overriding the role theme; everything else follows `theme`.
|
|
48
|
+
*
|
|
49
|
+
* A convenience over mapping `art.styled` yourself — reach for that directly
|
|
50
|
+
* when your TUI has its own styling model.
|
|
51
|
+
*/
|
|
52
|
+
export function toAnsi(art: MermaidArt, theme: AnsiTheme = DEFAULT_THEME): string[] {
|
|
53
|
+
return art.styled.map((row) =>
|
|
54
|
+
row
|
|
55
|
+
.map((span) => {
|
|
56
|
+
const cls = resolveClassStyle(span.classes, art.classDefs)
|
|
57
|
+
const sgr = cls !== null ? classSgr(cls, span.role, theme[span.role]) : theme[span.role]
|
|
58
|
+
const text = sgr === undefined ? span.text : `${ESC}[${sgr}m${span.text}${ESC}[0m`
|
|
59
|
+
// OSC 8 hyperlink around the whole run; terminals without support
|
|
60
|
+
// ignore the sequences and print the text unchanged.
|
|
61
|
+
return span.href === undefined ? text : `${OSC8}${span.href}${ST}${text}${OSC8}${ST}`
|
|
62
|
+
})
|
|
63
|
+
.join(''),
|
|
64
|
+
)
|
|
65
|
+
}
|