paseo-plugin-mermaid 0.3.1 → 0.4.1
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/OVERVIEW.md +4 -2
- package/README.md +68 -23
- package/client/compat.ts +20 -0
- package/client/diagram.tsx +49 -16
- package/client/file-panel.tsx +36 -0
- package/client/item.tsx +7 -0
- package/client/viewer.tsx +3 -9
- package/index.client.tsx +16 -2
- package/package.json +9 -7
- package/shared/drawable.ts +51 -0
- package/shared/file-view.ts +19 -0
- package/shared/flowchart.ts +31 -2
- package/shared/layout.ts +18 -1
- package/shared/segment.ts +19 -14
- package/shared/sequence.ts +11 -1
- package/shared/transform.ts +40 -2
package/OVERVIEW.md
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
Mermaid draws `mermaid` code blocks in agent and user messages as diagrams in the chat timeline
|
|
1
|
+
Mermaid draws `mermaid` code blocks in agent and user messages as diagrams in the chat timeline, at the width of the message and from the first streamed line. It draws flowcharts and sequence diagrams. A message with any other diagram type, or with lines the plugin's parsers do not read, such as subgraphs, other node shapes, `style` and `classDef` lines, sequence blocks like `loop`, or `%%{init}%%` directives, is left to Paseo's own Mermaid renderer rather than drawn with parts missing.
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A message that contains a diagram the plugin draws is rendered by the plugin. On Paseo 0.13 and newer, the text around the diagram uses Paseo's own Markdown, so it looks like the rest of the chat. On Paseo 0.11 and 0.12 it uses a limited renderer (headings, lists, emphasis and code). Messages without a diagram are left to Paseo.
|
|
4
4
|
|
|
5
5
|
Each diagram has a toolbar to zoom, fit to view, pop out to a larger view, and show the code. Zoom matches Paseo's own diagram viewer: 100% is the drawing fitted to the row.
|
|
6
6
|
|
|
7
|
+
On Paseo 0.13 and newer, `.mmd` and `.mermaid` files open in the file tab as a diagram, which redraws when the file changes on disk.
|
|
8
|
+
|
|
7
9
|
The plugin runs entirely in the Paseo app on desktop and mobile, works with Paseo 0.11 to 0.14, has no settings, and needs no additional setup.
|
package/README.md
CHANGED
|
@@ -27,14 +27,16 @@ Listed in the [Paseo plugin directory](https://paseo.sh/plugins/dutchakdev/merma
|
|
|
27
27
|
width of the chat while the agent is still writing it.
|
|
28
28
|
- **Flowcharts and sequence diagrams**, with the usual node shapes, edge styles and labels.
|
|
29
29
|
- **A toolbar on every diagram:** zoom, fit to view, pop out, and the source on demand.
|
|
30
|
+
- **`.mmd` files in the file tab** on Paseo 0.13 and newer, redrawn when the file changes.
|
|
30
31
|
- **Nothing leaves the app.** The plugin has no server half, no settings and no network
|
|
31
32
|
access; it reads the message text it is given and draws it with React Native views.
|
|
32
33
|
|
|
33
34
|
Paseo draws Mermaid itself too, in an iframe inside a zoomable box with a
|
|
34
|
-
fullscreen viewer
|
|
35
|
-
|
|
36
|
-
the
|
|
37
|
-
source on demand.
|
|
35
|
+
fullscreen viewer, and it draws every diagram type. This plugin draws flowcharts
|
|
36
|
+
and sequence diagrams inline instead: readable at the width of the message, from
|
|
37
|
+
the first streamed line, with the same controls above it for the times the fitted
|
|
38
|
+
drawing is not enough: zoom, fit to view, pop out, and the source on demand. A
|
|
39
|
+
diagram it cannot draw in full stays with Paseo's renderer.
|
|
38
40
|
|
|
39
41
|
## Install
|
|
40
42
|
|
|
@@ -62,7 +64,9 @@ Paseo also checks a prerelease's stable version core, so `0.14.0-beta.1` satisfi
|
|
|
62
64
|
this range. The 0.11.0 betas pass that check but predate the `icon` field, so they
|
|
63
65
|
refuse the manifest as well and need the release named in the install table; see
|
|
64
66
|
[version requirements](https://paseo.sh/docs/plugins/reference#requirements).
|
|
65
|
-
The development SDK
|
|
67
|
+
The development SDK is pinned to 0.13.2 for its types; Paseo supplies the runtime
|
|
68
|
+
modules. Paseo's Markdown component and file panels arrived in 0.13, so the plugin
|
|
69
|
+
checks for them at runtime and works without them on 0.11 and 0.12.
|
|
66
70
|
|
|
67
71
|
To work on it locally instead:
|
|
68
72
|
|
|
@@ -82,13 +86,23 @@ paseo plugin install "$PWD"
|
|
|
82
86
|
| Node shapes | `[rect]`, `(round)`, `([stadium])`, `[[subroutine]]`, `{diamond}`, `((circle))`, `>asymmetric]` |
|
|
83
87
|
| Edges | `-->`, `---`, `-.->`, `-.-`, `==>`, `===` |
|
|
84
88
|
| Edge labels | `-->|both|` and `-- forms -->` |
|
|
85
|
-
| `sequenceDiagram` | participants, actors, notes |
|
|
89
|
+
| `sequenceDiagram` | participants, actors, notes, messages to oneself |
|
|
86
90
|
| Message arrows | `->`, `->>`, `-->`, `-->>`, `-x`, `--x`, `-)`, `--)` |
|
|
87
91
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
+
Everything else is left to Paseo, which renders it with Mermaid: other diagram
|
|
93
|
+
types, subgraphs, other node shapes (cylinders, hexagons, parallelograms, double
|
|
94
|
+
circles), Markdown string labels, `style` and `classDef` lines, `&` and `;` that
|
|
95
|
+
join several nodes or statements on one line, sequence blocks such as `loop` and
|
|
96
|
+
`alt`, `%%{init}%%` directives, and front matter. The plugin takes a message only
|
|
97
|
+
when its parsers read every line of every diagram in it. A message whose text
|
|
98
|
+
defines reference-style links or footnotes goes to Paseo as well: split into
|
|
99
|
+
pieces around the diagram, a definition in one piece would not reach the link in
|
|
100
|
+
another.
|
|
101
|
+
|
|
102
|
+
When the plugin draws a message, the text around the diagram goes through Paseo's
|
|
103
|
+
own Markdown on Paseo 0.13 and newer, so it looks like the rest of the chat. On
|
|
104
|
+
0.11 and 0.12 it goes through the plugin's renderer, which covers headings, lists,
|
|
105
|
+
emphasis and code.
|
|
92
106
|
|
|
93
107
|
## Controls
|
|
94
108
|
|
|
@@ -109,25 +123,35 @@ sideways; the transcript scrolls vertically as it always does, so nothing is
|
|
|
109
123
|
clipped. On phones and narrow windows the button labels drop and the icons and
|
|
110
124
|
the percent stay.
|
|
111
125
|
|
|
126
|
+
## Mermaid files
|
|
127
|
+
|
|
128
|
+
On Paseo 0.13 and newer, `.mmd` and `.mermaid` files can open in the file tab as
|
|
129
|
+
**Mermaid diagram**. Paseo offers it beside its Source editor the first time such a
|
|
130
|
+
file opens, and **Open with…** in the tab menu switches between them. The drawing
|
|
131
|
+
has the same toolbar as in the chat and redraws when the file changes on disk, so
|
|
132
|
+
a diagram an agent is editing updates in place. A diagram the plugin does not draw
|
|
133
|
+
in full is rendered in the same tab by Paseo's Markdown, which uses Mermaid.
|
|
134
|
+
|
|
112
135
|
## How it works
|
|
113
136
|
|
|
114
137
|
```mermaid
|
|
115
138
|
flowchart TD
|
|
116
139
|
Msg([Message arrives]) --> Check{Has mermaid?}
|
|
117
140
|
Check -->|no| Paseo[Paseo renders it]
|
|
118
|
-
Check -->|yes|
|
|
141
|
+
Check -->|yes| Full{Every diagram drawn in full?}
|
|
142
|
+
Full -->|no| Paseo
|
|
143
|
+
Full -->|yes| Split[[Split into segments]]
|
|
119
144
|
Split --> Text(Prose)
|
|
120
145
|
Split --> Src(Diagram source)
|
|
121
|
-
Text --> Md[
|
|
146
|
+
Text --> Md[Paseo Markdown from 0.13]
|
|
122
147
|
Src --> Parse{Which parser?}
|
|
123
148
|
Parse -->|flowchart| Layout[Layered layout]
|
|
124
149
|
Parse -->|sequence| Columns[Column layout]
|
|
125
|
-
Parse -.->|neither| Raw[Show source]
|
|
126
150
|
Layout --> Draw((Views))
|
|
127
151
|
Columns --> Draw
|
|
128
152
|
```
|
|
129
153
|
|
|
130
|
-
|
|
154
|
+
These constraints shaped this plugin.
|
|
131
155
|
|
|
132
156
|
**Mermaid itself cannot run here.** A Paseo client bundle may import only the modules the
|
|
133
157
|
app provides: `react`, `react-native`, `@tanstack/react-query`, `zod` and the
|
|
@@ -140,9 +164,18 @@ with three transparent borders.
|
|
|
140
164
|
**A transformer replaces the whole timeline entry.** Once a message contains a
|
|
141
165
|
diagram, the plugin owns the message. It is split into prose and diagram
|
|
142
166
|
segments, and each becomes its own item, so the text around the diagram keeps its
|
|
143
|
-
place.
|
|
144
|
-
|
|
145
|
-
diagram are left alone entirely.
|
|
167
|
+
place. From Paseo 0.13 that prose goes through Paseo's own Markdown component;
|
|
168
|
+
before it, through a small renderer covering headings, lists, emphasis and code.
|
|
169
|
+
Messages without a diagram are left alone entirely.
|
|
170
|
+
|
|
171
|
+
**A diagram is drawn in full or not at all.** Paseo's renderer runs Mermaid and
|
|
172
|
+
draws everything, so a drawing with lines missing would be worse than leaving the
|
|
173
|
+
message to it. The parsers report every line they skip, and one skipped line in
|
|
174
|
+
any diagram leaves the whole message to Paseo (`shared/drawable.ts`). That is
|
|
175
|
+
decided from what the parsers read, not from the first word of the source, so the
|
|
176
|
+
parsers refuse what they would draw wrong rather than accept it loosely: a label
|
|
177
|
+
with a stray square bracket, a label wrapped whole in brackets that names another
|
|
178
|
+
shape, a directive.
|
|
146
179
|
|
|
147
180
|
**Items store the source, not a parsed graph.** Parsing happens at render time.
|
|
148
181
|
The stored data stays small, and a later fix to the parser improves diagrams that
|
|
@@ -153,7 +186,11 @@ streaming update, and it renders `mermaid` fences itself as soon as one opens. A
|
|
|
153
186
|
transformer that waited for the closing ``` would leave the host drawing the
|
|
154
187
|
diagram for the whole stream and then swap renderers at the last line. An
|
|
155
188
|
unfinished block is therefore taken over at once and drawn from whatever has
|
|
156
|
-
parsed so far.
|
|
189
|
+
parsed so far. While the message streams and the fence is open, the last line
|
|
190
|
+
counts only once its newline has arrived: until then it may be half typed.
|
|
191
|
+
Finished lines never change, so a diagram that does need Paseo, say one that opens
|
|
192
|
+
a subgraph, is handed over once, at the first line that shows it. A finished
|
|
193
|
+
message is judged on every line, even when its fence never closed.
|
|
157
194
|
|
|
158
195
|
**The drawing is fitted to the row.** A diagram's natural size is whatever its
|
|
159
196
|
labels happened to add up to, which is no basis for how big it should appear. The
|
|
@@ -175,9 +212,13 @@ The parsers, the layout, the zoom model and the transform are pure functions
|
|
|
175
212
|
with no runtime dependencies, and that is where the tests are: bracket shapes,
|
|
176
213
|
operator precedence (`-.->` must never read as `-.-` plus a stray `>`), cycles in
|
|
177
214
|
a flowchart, node overlap, arrow direction, zoom steps that land back on 100%,
|
|
178
|
-
which toolbar controls are live,
|
|
179
|
-
|
|
180
|
-
and
|
|
215
|
+
which toolbar controls are live, which diagrams go to Paseo (checked on diagrams
|
|
216
|
+
from this owner's own docs, in `tests/fixtures/`), that a message streamed one
|
|
217
|
+
character at a time changes renderer at most once and only towards Paseo, and the
|
|
218
|
+
JSON round trip Paseo performs on item data before rendering. The Views themselves are checked by
|
|
219
|
+
`npm run typecheck` and by reloading the plugin. `npm run check:bundle` compiles
|
|
220
|
+
the client entry with the installed daemon's compiler and loads it into a stand-in
|
|
221
|
+
0.11 app and a 0.13 app; it needs the `paseo` CLI, so it runs locally, not in CI.
|
|
181
222
|
|
|
182
223
|
| File | Role |
|
|
183
224
|
| ---- | ---- |
|
|
@@ -187,13 +228,17 @@ and by reloading the plugin.
|
|
|
187
228
|
| `shared/layout.ts` | layered layout, orthogonal edge routing, column layout |
|
|
188
229
|
| `shared/markdown.ts` | the Markdown subset used for prose |
|
|
189
230
|
| `shared/zoom.ts` | zoom steps, fit to view, and which controls are live |
|
|
231
|
+
| `shared/drawable.ts` | whether the plugin draws a diagram in full, or leaves the message to Paseo |
|
|
190
232
|
| `shared/transform.ts` | the timeline transform itself |
|
|
233
|
+
| `shared/file-view.ts` | what the file panel shows for a `.mmd` file |
|
|
191
234
|
| `client/diagram.tsx` | drawing, in Views, at natural size |
|
|
192
235
|
| `client/viewer.tsx` | fitting, zoom and the source toggle, in the row and in the pop-out |
|
|
193
236
|
| `client/toolbar.tsx` | the toolbar |
|
|
194
|
-
| `client/markdown.tsx` | prose rendering |
|
|
237
|
+
| `client/markdown.tsx` | prose rendering before Paseo 0.13 |
|
|
195
238
|
| `client/item.tsx` | the two timeline renderers |
|
|
196
|
-
| `
|
|
239
|
+
| `client/file-panel.tsx` | the `.mmd` file panel |
|
|
240
|
+
| `client/compat.ts` | runtime checks for what apps before 0.13 lack |
|
|
241
|
+
| `index.client.tsx` | registers the transformers, renderers and file panel; the only entry |
|
|
197
242
|
|
|
198
243
|
Releases are cut from tags by GitHub Actions; see [CONTRIBUTING.md](CONTRIBUTING.md)
|
|
199
244
|
for the flow and [CHANGELOG.md](CHANGELOG.md) for what changed.
|
package/client/compat.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ComponentType } from "react";
|
|
2
|
+
import type { PluginClientContext, PluginFilePanelContribution } from "@getpaseo/plugin/client";
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* The plugin supports apps from Paseo 0.11 while its SDK types are 0.13's, so the
|
|
6
|
+
* types promise members that older apps do not have. The app maps a missing export
|
|
7
|
+
* or method to `undefined` rather than failing to load, which is what these checks
|
|
8
|
+
* read. Keep every 0.13-only call behind one of them.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Paseo's Markdown component from `@getpaseo/plugin/client/ui`; apps before 0.13 have none. */
|
|
12
|
+
export function hostMarkdown(ui: object): ComponentType<{ children: string }> | null {
|
|
13
|
+
return (ui as { Markdown?: ComponentType<{ children: string }> }).Markdown ?? null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Registers a file panel where the app has file panels (0.13 and later); returns its cleanup, or null. */
|
|
17
|
+
export function addFilePanel(client: PluginClientContext, contribution: PluginFilePanelContribution): (() => void) | null {
|
|
18
|
+
const add = (client as Partial<Pick<PluginClientContext, "addFilePanel">>).addFilePanel;
|
|
19
|
+
return typeof add === "function" ? add.call(client, contribution) : null;
|
|
20
|
+
}
|
package/client/diagram.tsx
CHANGED
|
@@ -2,10 +2,11 @@ import type { PluginTheme } from "@getpaseo/plugin";
|
|
|
2
2
|
import React from "react";
|
|
3
3
|
import { StyleSheet, Text, View } from "react-native";
|
|
4
4
|
import { parseFlowchart, type Flowchart, type NodeShape } from "../shared/flowchart";
|
|
5
|
-
import { namesADiagram } from "../shared/segment";
|
|
6
5
|
import {
|
|
7
6
|
layoutFlowchart,
|
|
8
7
|
layoutSequence,
|
|
8
|
+
SELF_LABEL_GAP,
|
|
9
|
+
SELF_LOOP_WIDTH,
|
|
9
10
|
type FlowLayout,
|
|
10
11
|
type LaidOutEdge,
|
|
11
12
|
type LaidOutNode,
|
|
@@ -41,7 +42,7 @@ export function parseDiagram(source: string): ParsedDiagram {
|
|
|
41
42
|
if (flow && flow.nodes.length > 0) return { kind: "flow", layout: layoutFlowchart(flow), skipped: flow.skipped };
|
|
42
43
|
const sequence = parseSequence(source);
|
|
43
44
|
if (sequence && sequence.participants.length > 0) {
|
|
44
|
-
const box = layoutSequence(sequence.participants, sequence.events.length);
|
|
45
|
+
const box = layoutSequence(sequence.participants, sequence.events.length, sequence.events);
|
|
45
46
|
return { kind: "sequence", diagram: sequence, size: { width: box.width, height: box.height } };
|
|
46
47
|
}
|
|
47
48
|
return { kind: "unsupported" };
|
|
@@ -61,12 +62,12 @@ export function Drawing({ parsed, theme }: { parsed: DrawableDiagram; theme: Plu
|
|
|
61
62
|
);
|
|
62
63
|
}
|
|
63
64
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Source that names no diagram yet. The transform hands every diagram this plugin
|
|
67
|
+
* cannot draw to Paseo, so what reaches here is a diagram still arriving, shown as
|
|
68
|
+
* it is typed until its header settles.
|
|
69
|
+
*/
|
|
70
|
+
export function Arriving({ source, theme }: { source: string; theme: PluginTheme }) {
|
|
70
71
|
return (
|
|
71
72
|
<View
|
|
72
73
|
style={{
|
|
@@ -74,14 +75,8 @@ export function Unsupported({ source, theme }: { source: string; theme: PluginTh
|
|
|
74
75
|
borderWidth: StyleSheet.hairlineWidth,
|
|
75
76
|
borderColor: theme.colors.foregroundMuted,
|
|
76
77
|
padding: 10,
|
|
77
|
-
gap: 6,
|
|
78
78
|
}}
|
|
79
79
|
>
|
|
80
|
-
{arriving ? null : (
|
|
81
|
-
<Text style={{ color: theme.colors.foregroundMuted, fontSize: 11 }}>
|
|
82
|
-
This plugin draws flowchart and sequenceDiagram. Showing the source instead.
|
|
83
|
-
</Text>
|
|
84
|
-
)}
|
|
85
80
|
<Text selectable style={{ color: theme.colors.foreground, fontFamily: "Menlo", fontSize: 12 }}>
|
|
86
81
|
{source}
|
|
87
82
|
</Text>
|
|
@@ -273,7 +268,7 @@ function SequenceView({
|
|
|
273
268
|
diagram: SequenceDiagram;
|
|
274
269
|
theme: PluginTheme;
|
|
275
270
|
}) {
|
|
276
|
-
const layout = layoutSequence(diagram.participants, diagram.events.length);
|
|
271
|
+
const layout = layoutSequence(diagram.participants, diagram.events.length, diagram.events);
|
|
277
272
|
const centers = new Map(layout.columns.map((column) => [column.id, column.centerX]));
|
|
278
273
|
|
|
279
274
|
return (
|
|
@@ -355,12 +350,50 @@ function EventView({
|
|
|
355
350
|
);
|
|
356
351
|
}
|
|
357
352
|
|
|
353
|
+
const color = theme.colors.foregroundMuted;
|
|
354
|
+
|
|
355
|
+
if (event.from === event.to) {
|
|
356
|
+
// A message to oneself: a loop out of the lifeline and back, its label beside it.
|
|
357
|
+
// The layout widened the drawing for that label (`layoutSequence`).
|
|
358
|
+
const x = centers.get(event.from) ?? 0;
|
|
359
|
+
return (
|
|
360
|
+
<>
|
|
361
|
+
<View
|
|
362
|
+
style={{
|
|
363
|
+
position: "absolute",
|
|
364
|
+
left: x,
|
|
365
|
+
top: y + 12,
|
|
366
|
+
width: SELF_LOOP_WIDTH,
|
|
367
|
+
height: 18,
|
|
368
|
+
borderTopWidth: 1.5,
|
|
369
|
+
borderRightWidth: 1.5,
|
|
370
|
+
borderBottomWidth: 1.5,
|
|
371
|
+
borderColor: color,
|
|
372
|
+
opacity: event.style === "dashed" ? 0.45 : 1,
|
|
373
|
+
}}
|
|
374
|
+
/>
|
|
375
|
+
<Arrow at={{ x, y: y + 29.25, direction: "left" }} color={color} />
|
|
376
|
+
<Text
|
|
377
|
+
numberOfLines={1}
|
|
378
|
+
style={{
|
|
379
|
+
position: "absolute",
|
|
380
|
+
left: x + SELF_LOOP_WIDTH + SELF_LABEL_GAP,
|
|
381
|
+
top: y + 14,
|
|
382
|
+
fontSize: 11,
|
|
383
|
+
color: theme.colors.foreground,
|
|
384
|
+
}}
|
|
385
|
+
>
|
|
386
|
+
{event.text}
|
|
387
|
+
</Text>
|
|
388
|
+
</>
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
|
|
358
392
|
const from = centers.get(event.from) ?? 0;
|
|
359
393
|
const to = centers.get(event.to) ?? 0;
|
|
360
394
|
const forward = to >= from;
|
|
361
395
|
const left = Math.min(from, to);
|
|
362
396
|
const width = Math.max(Math.abs(to - from), 1);
|
|
363
|
-
const color = theme.colors.foregroundMuted;
|
|
364
397
|
|
|
365
398
|
return (
|
|
366
399
|
<>
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type PluginFilePanelProps, useFileText } from "@getpaseo/plugin/client";
|
|
2
|
+
import { Loading, Markdown } from "@getpaseo/plugin/client/ui";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import { ScrollView, Text } from "react-native";
|
|
5
|
+
import { fileView } from "../shared/file-view";
|
|
6
|
+
import { DiagramViewer } from "./viewer";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Draws a `.mmd` file in its file tab and redraws it when the file changes on disk,
|
|
10
|
+
* so a diagram an agent is editing updates in place. A diagram this plugin does not
|
|
11
|
+
* draw in full goes to Paseo's Markdown, which renders it with Mermaid.
|
|
12
|
+
*
|
|
13
|
+
* Registered only on apps with file panels (Paseo 0.13 and later, see `compat`),
|
|
14
|
+
* which also have `useFileText`, `Loading` and `Markdown`.
|
|
15
|
+
*/
|
|
16
|
+
export function MermaidFilePanel({ file, theme, layout }: PluginFilePanelProps) {
|
|
17
|
+
const text = useFileText(file);
|
|
18
|
+
const padding = layout.compact ? 12 : 20;
|
|
19
|
+
if (text.status === "loading") return <Loading />;
|
|
20
|
+
if (text.status === "error") {
|
|
21
|
+
return <Text style={{ color: theme.colors.statusDanger, padding }}>{text.error.message}</Text>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const view = fileView(text.text);
|
|
25
|
+
return (
|
|
26
|
+
<ScrollView style={{ flex: 1, backgroundColor: theme.colors.surface0 }} contentContainerStyle={{ padding }}>
|
|
27
|
+
{view.kind === "diagram" ? (
|
|
28
|
+
<DiagramViewer source={view.source} theme={theme} compact={layout.compact} />
|
|
29
|
+
) : view.kind === "markdown" ? (
|
|
30
|
+
<Markdown>{view.markdown}</Markdown>
|
|
31
|
+
) : (
|
|
32
|
+
<Text style={{ color: theme.colors.foregroundMuted }}>This file is empty.</Text>
|
|
33
|
+
)}
|
|
34
|
+
</ScrollView>
|
|
35
|
+
);
|
|
36
|
+
}
|
package/client/item.tsx
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import type { PluginTimelineItemProps } from "@getpaseo/plugin/client";
|
|
2
|
+
import * as ui from "@getpaseo/plugin/client/ui";
|
|
2
3
|
import React from "react";
|
|
3
4
|
import { View } from "react-native";
|
|
5
|
+
import { hostMarkdown } from "./compat";
|
|
4
6
|
import { Markdown } from "./markdown";
|
|
5
7
|
import { DiagramViewer } from "./viewer";
|
|
6
8
|
|
|
9
|
+
/** Paseo's own Markdown on apps from 0.13; older apps get the plugin's limited renderer. */
|
|
10
|
+
const HostMarkdown = hostMarkdown(ui);
|
|
11
|
+
|
|
7
12
|
export interface TextData {
|
|
8
13
|
text: string;
|
|
9
14
|
}
|
|
@@ -13,6 +18,8 @@ export interface DiagramData {
|
|
|
13
18
|
}
|
|
14
19
|
|
|
15
20
|
export function TextItem({ item, theme, layout }: PluginTimelineItemProps<TextData>) {
|
|
21
|
+
// The prose around a diagram then reads like the rest of the chat: links, tables, highlighted code.
|
|
22
|
+
if (HostMarkdown) return <HostMarkdown>{item.data.text}</HostMarkdown>;
|
|
16
23
|
return <Markdown text={item.data.text} theme={theme} compact={layout.compact} />;
|
|
17
24
|
}
|
|
18
25
|
|
package/client/viewer.tsx
CHANGED
|
@@ -4,7 +4,7 @@ import React, { useMemo, useState } from "react";
|
|
|
4
4
|
import { LayoutChangeEvent, ScrollView, StyleSheet, Text, View } from "react-native";
|
|
5
5
|
import { fitScale, overflowsAfterFit } from "../shared/layout";
|
|
6
6
|
import { fitBothScale, fitRowZoom, zoomIn, zoomOut, type Size } from "../shared/zoom";
|
|
7
|
-
import { Drawing, naturalSize, parseDiagram,
|
|
7
|
+
import { Arriving, Drawing, naturalSize, parseDiagram, type DrawableDiagram } from "./diagram";
|
|
8
8
|
import { Toolbar } from "./toolbar";
|
|
9
9
|
|
|
10
10
|
/**
|
|
@@ -27,19 +27,13 @@ export function DiagramViewer({ source, theme, compact }: { source: string; them
|
|
|
27
27
|
const [popped, setPopped] = useState(false);
|
|
28
28
|
|
|
29
29
|
if (parsed.kind === "unsupported") {
|
|
30
|
-
//
|
|
31
|
-
return <
|
|
30
|
+
// Only a diagram whose header is still arriving gets here; see `drawable`.
|
|
31
|
+
return <Arriving source={source} theme={theme} />;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
return (
|
|
35
35
|
<View style={{ gap: 6 }}>
|
|
36
36
|
<RowViewer parsed={parsed} source={source} theme={theme} compact={compact} onPopOut={() => setPopped(true)} />
|
|
37
|
-
{parsed.kind === "flow" && parsed.skipped.length > 0 ? (
|
|
38
|
-
<Text style={{ color: theme.colors.foregroundMuted, fontSize: 11 }}>
|
|
39
|
-
{parsed.skipped.length} line{parsed.skipped.length === 1 ? "" : "s"} not drawn: subgraphs and styling are
|
|
40
|
-
not supported yet.
|
|
41
|
-
</Text>
|
|
42
|
-
) : null}
|
|
43
37
|
{popped ? (
|
|
44
38
|
<PopOut parsed={parsed} source={source} theme={theme} compact={compact} onClose={() => setPopped(false)} />
|
|
45
39
|
) : null}
|
package/index.client.tsx
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { PluginClientContext } from "@getpaseo/plugin/client";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
+
import { addFilePanel } from "./client/compat";
|
|
4
|
+
import { MermaidFilePanel } from "./client/file-panel";
|
|
3
5
|
import { DiagramItem, TextItem } from "./client/item";
|
|
4
6
|
import { DIAGRAM_KIND, split, TEXT_KIND } from "./shared/transform";
|
|
5
7
|
|
|
@@ -22,6 +24,9 @@ const diagramSchema = z.object({ source: z.string() });
|
|
|
22
24
|
* at render time, which keeps the stored data small and lets a later fix to the
|
|
23
25
|
* parser improve diagrams that were sent months ago.
|
|
24
26
|
*
|
|
27
|
+
* A message with a diagram this plugin cannot draw in full is left to Paseo, whose
|
|
28
|
+
* own renderer runs Mermaid; see `shared/drawable.ts`.
|
|
29
|
+
*
|
|
25
30
|
* Everything here runs in the app. The plugin has no server entry and starts no
|
|
26
31
|
* daemon process.
|
|
27
32
|
*/
|
|
@@ -31,12 +36,12 @@ export default function contribute(client: PluginClientContext) {
|
|
|
31
36
|
client.addTimelineTransformer({
|
|
32
37
|
id: "mermaid-assistant",
|
|
33
38
|
query: { itemType: "assistant_message" },
|
|
34
|
-
transform: ({ item }) => split(item.text),
|
|
39
|
+
transform: ({ item, phase }) => split(item.text, phase),
|
|
35
40
|
});
|
|
36
41
|
client.addTimelineTransformer({
|
|
37
42
|
id: "mermaid-user",
|
|
38
43
|
query: { itemType: "user_message" },
|
|
39
|
-
transform: ({ item }) => split(item.text),
|
|
44
|
+
transform: ({ item, phase }) => split(item.text, phase),
|
|
40
45
|
});
|
|
41
46
|
|
|
42
47
|
client.addTimelineRenderer({
|
|
@@ -52,5 +57,14 @@ export default function contribute(client: PluginClientContext) {
|
|
|
52
57
|
Component: TextItem,
|
|
53
58
|
});
|
|
54
59
|
|
|
60
|
+
// Paseo 0.13 and later; older apps skip it.
|
|
61
|
+
addFilePanel(client, {
|
|
62
|
+
id: "mermaid-file",
|
|
63
|
+
title: "Mermaid diagram",
|
|
64
|
+
icon: "Workflow",
|
|
65
|
+
files: { extensions: ["mmd", "mermaid"] },
|
|
66
|
+
Component: MermaidFilePanel,
|
|
67
|
+
});
|
|
68
|
+
|
|
55
69
|
return () => {};
|
|
56
70
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "paseo-plugin-mermaid",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Render Mermaid diagrams inside the Paseo chat timeline",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "dutchakdev",
|
|
@@ -21,12 +21,13 @@
|
|
|
21
21
|
"typecheck": "tsc --noEmit",
|
|
22
22
|
"test": "vitest run",
|
|
23
23
|
"preview": "tsx scripts/preview.ts",
|
|
24
|
-
"check": "npm run typecheck && npm test"
|
|
24
|
+
"check": "npm run typecheck && npm test",
|
|
25
|
+
"check:bundle": "node scripts/check-bundle.mjs"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
|
-
"@getpaseo/client": "0.
|
|
28
|
-
"@getpaseo/plugin": "0.
|
|
29
|
-
"@getpaseo/protocol": "0.
|
|
28
|
+
"@getpaseo/client": "0.13.2",
|
|
29
|
+
"@getpaseo/plugin": "0.13.2",
|
|
30
|
+
"@getpaseo/protocol": "0.13.2",
|
|
30
31
|
"@tanstack/react-query": "^5.90.11",
|
|
31
32
|
"@types/node": "22.20.5",
|
|
32
33
|
"@types/react": "~19.2.0",
|
|
@@ -34,8 +35,9 @@
|
|
|
34
35
|
"react-native": "0.81.5",
|
|
35
36
|
"semver": "7.8.5",
|
|
36
37
|
"tsx": "^4.23.13",
|
|
37
|
-
"typescript": "^
|
|
38
|
-
"
|
|
38
|
+
"typescript": "^7.0.2",
|
|
39
|
+
"vite": "^8.3.5",
|
|
40
|
+
"vitest": "^5.0.5",
|
|
39
41
|
"zod": "^4.4.3"
|
|
40
42
|
},
|
|
41
43
|
"files": [
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { parseFlowchart } from "./flowchart";
|
|
2
|
+
import { parseSequence } from "./sequence";
|
|
3
|
+
|
|
4
|
+
/** Diagram keywords whose diagrams this plugin draws, in Mermaid's spelling. */
|
|
5
|
+
const DRAWN = ["flowchart", "graph", "sequenceDiagram"];
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Whether this plugin draws everything in a diagram.
|
|
9
|
+
*
|
|
10
|
+
* When it does not, the message is left to Paseo, whose own renderer runs Mermaid
|
|
11
|
+
* and draws every diagram type, so a diagram is not shown with lines missing or as
|
|
12
|
+
* bare source. The parsers decide, not a guess from the first word: a line either
|
|
13
|
+
* parser reports as skipped is a line this plugin would not draw.
|
|
14
|
+
*
|
|
15
|
+
* `pending` is the line being typed while the diagram can still grow, and null once
|
|
16
|
+
* it cannot. Only `finished` lines are judged while it grows, so a half-typed edge
|
|
17
|
+
* never hands the message over and takes it back a moment later, and a diagram with
|
|
18
|
+
* nothing to draw yet is kept. Finished lines never change, so for one diagram the
|
|
19
|
+
* answer only ever moves from this plugin to Paseo, and the renderer swaps at most
|
|
20
|
+
* once.
|
|
21
|
+
*/
|
|
22
|
+
export function drawable(finished: string, pending: string | null): boolean {
|
|
23
|
+
const growing = pending !== null;
|
|
24
|
+
const flow = parseFlowchart(finished);
|
|
25
|
+
if (flow) return flow.skipped.length === 0 && (growing || flow.nodes.length > 0);
|
|
26
|
+
const sequence = parseSequence(finished);
|
|
27
|
+
if (sequence) return sequence.skipped.length === 0 && (growing || sequence.participants.length > 0);
|
|
28
|
+
|
|
29
|
+
// No parser takes the finished lines. That is final unless nothing has finished yet.
|
|
30
|
+
if (!growing || meaningful(finished)) return false;
|
|
31
|
+
return couldStillName(uncommented(pending));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function uncommented(line: string): string {
|
|
35
|
+
return line.replace(/%%.*$/, "").trim();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function meaningful(text: string): boolean {
|
|
39
|
+
return text.split("\n").some((line) => uncommented(line).length > 0);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** True while the first line, still arriving, can become a header this plugin draws. */
|
|
43
|
+
function couldStillName(line: string): boolean {
|
|
44
|
+
// A lone `%` may be the start of a `%%` comment, which `uncommented` empties.
|
|
45
|
+
if ("%%".startsWith(line)) return true;
|
|
46
|
+
const typed = line.toLowerCase();
|
|
47
|
+
return DRAWN.some((keyword) => {
|
|
48
|
+
const known = keyword.toLowerCase();
|
|
49
|
+
return known.startsWith(typed) || typed.startsWith(known);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { drawable } from "./drawable";
|
|
2
|
+
|
|
3
|
+
export type FileView = { kind: "empty" } | { kind: "diagram"; source: string } | { kind: "markdown"; markdown: string };
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* What the file panel shows for a `.mmd` file: the plugin's drawing when it draws
|
|
7
|
+
* the whole diagram, otherwise a mermaid fence for Paseo's Markdown, which renders
|
|
8
|
+
* the diagram with Mermaid itself. The file is complete, so nothing is still arriving.
|
|
9
|
+
*/
|
|
10
|
+
export function fileView(text: string): FileView {
|
|
11
|
+
const source = text.trim();
|
|
12
|
+
if (source.length === 0) return { kind: "empty" };
|
|
13
|
+
if (drawable(source, null)) return { kind: "diagram", source };
|
|
14
|
+
// A fence closes on a run of backticks at least as long as its own, so it must
|
|
15
|
+
// be longer than any run inside the source.
|
|
16
|
+
const longest = Math.max(2, ...(source.match(/`+/g) ?? []).map((run) => run.length));
|
|
17
|
+
const fence = "`".repeat(longest + 1);
|
|
18
|
+
return { kind: "markdown", markdown: `${fence}mermaid\n${source}\n${fence}` };
|
|
19
|
+
}
|
package/shared/flowchart.ts
CHANGED
|
@@ -34,6 +34,7 @@ export interface Flowchart {
|
|
|
34
34
|
skipped: string[];
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
const DIRECTIVE = /%%\{/;
|
|
37
38
|
const HEADER = /^\s*(?:flowchart|graph)(?:\s+(TD|TB|BT|LR|RL))?\s*$/i;
|
|
38
39
|
|
|
39
40
|
/** Longest first: `-.->` must win over `-.-`. */
|
|
@@ -50,7 +51,6 @@ const OPENERS: { open: string; close: string; shape: NodeShape }[] = [
|
|
|
50
51
|
{ open: "([", close: "])", shape: "stadium" },
|
|
51
52
|
{ open: "[[", close: "]]", shape: "subroutine" },
|
|
52
53
|
{ open: "((", close: "))", shape: "circle" },
|
|
53
|
-
{ open: "{{", close: "}}", shape: "diamond" },
|
|
54
54
|
{ open: "[", close: "]", shape: "rect" },
|
|
55
55
|
{ open: "(", close: ")", shape: "round" },
|
|
56
56
|
{ open: "{", close: "}", shape: "diamond" },
|
|
@@ -74,6 +74,28 @@ function stripQuotes(value: string): string {
|
|
|
74
74
|
return (quoted ? quoted[1] : trimmed).replace(/<br\s*\/?>/gi, "\n").trim();
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
/**
|
|
78
|
+
* The label inside a shape's brackets, or null when the brackets hold syntax this
|
|
79
|
+
* plugin would draw wrong. Refusing it puts the line among the skipped ones, which
|
|
80
|
+
* leaves the diagram to Paseo.
|
|
81
|
+
*
|
|
82
|
+
* A quoted label may hold anything but its own quote and a Markdown string. Unquoted,
|
|
83
|
+
* a square bracket or the shape's own closing bracket means a second node run into
|
|
84
|
+
* this one (`A[x] & B[y]`, `A(x); B(y)`), a label wrapped whole in brackets is another
|
|
85
|
+
* shape (`[(db)]`, `{{hex}}`, `(((x)))`), and slashes at both ends are a parallelogram
|
|
86
|
+
* or a trapezoid. Other brackets inside the text, as in `A[Reload (beta)]`, stay part
|
|
87
|
+
* of the label.
|
|
88
|
+
*/
|
|
89
|
+
function labelOf(inner: string, close: string): string | null {
|
|
90
|
+
const trimmed = inner.trim();
|
|
91
|
+
const quoted = /^"([^"]*)"$/.exec(trimmed) ?? /^'([^']*)'$/.exec(trimmed);
|
|
92
|
+
if (quoted) return /^`[\s\S]*`$/.test(quoted[1].trim()) ? null : stripQuotes(trimmed);
|
|
93
|
+
if (/[[\]]/.test(trimmed) || [...close].some((char) => trimmed.includes(char))) return null;
|
|
94
|
+
if (/^[([{][\s\S]*[)\]}]$/.test(trimmed)) return null;
|
|
95
|
+
if (/^[/\\][\s\S]*[/\\]$/.test(trimmed)) return null;
|
|
96
|
+
return stripQuotes(trimmed);
|
|
97
|
+
}
|
|
98
|
+
|
|
77
99
|
/** Reads `id`, `id[Label]`, `id{Decision}`, `id((Circle))` and friends. */
|
|
78
100
|
export function parseNodeExpression(raw: string): FlowNode | null {
|
|
79
101
|
const text = raw.trim();
|
|
@@ -85,7 +107,9 @@ export function parseNodeExpression(raw: string): FlowNode | null {
|
|
|
85
107
|
if (!text.endsWith(close)) continue;
|
|
86
108
|
const id = text.slice(0, start).trim();
|
|
87
109
|
if (!/^[\w.-]+$/.test(id)) continue;
|
|
88
|
-
|
|
110
|
+
// The longest opener that fits is the shape meant; a label it cannot read is final.
|
|
111
|
+
const label = labelOf(text.slice(start + open.length, text.length - close.length), close);
|
|
112
|
+
if (label === null) return null;
|
|
89
113
|
return { id, label: label.length > 0 ? label : id, shape };
|
|
90
114
|
}
|
|
91
115
|
|
|
@@ -150,6 +174,11 @@ export function parseFlowchart(source: string): Flowchart | null {
|
|
|
150
174
|
};
|
|
151
175
|
|
|
152
176
|
for (const original of lines) {
|
|
177
|
+
// `%%{init: …}%%`, anywhere on a line, is a directive, not a comment: it configures the diagram, which this plugin does not do.
|
|
178
|
+
if (DIRECTIVE.test(original)) {
|
|
179
|
+
skipped.push(original.trim());
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
153
182
|
const line = original.replace(/%%.*$/, "").trim();
|
|
154
183
|
if (line.length === 0) continue;
|
|
155
184
|
|
package/shared/layout.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Direction, Flowchart, FlowEdge, FlowNode } from "./flowchart";
|
|
2
|
+
import type { SeqEvent } from "./sequence";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Layered layout for a flowchart, computed here rather than in the renderer so
|
|
@@ -377,13 +378,21 @@ const SEQ_HEADER = 40;
|
|
|
377
378
|
const SEQ_GAP = 40;
|
|
378
379
|
const SEQ_MIN_COLUMN = 96;
|
|
379
380
|
|
|
381
|
+
/** A message to oneself: a loop this wide right of the lifeline, its label this far past the loop. */
|
|
382
|
+
export const SELF_LOOP_WIDTH = 28;
|
|
383
|
+
export const SELF_LABEL_GAP = 6;
|
|
384
|
+
|
|
380
385
|
/**
|
|
381
386
|
* Columns are sized to their own label, not to a shared width: one participant
|
|
382
387
|
* called "Payment gateway" should not push every other column that wide.
|
|
388
|
+
*
|
|
389
|
+
* `events` widens the drawing where a message to oneself would run past the right
|
|
390
|
+
* edge: its loop and label sit to the right of the lifeline.
|
|
383
391
|
*/
|
|
384
392
|
export function layoutSequence(
|
|
385
393
|
participants: { id: string; label: string; actor: boolean }[],
|
|
386
394
|
eventCount: number,
|
|
395
|
+
events: readonly SeqEvent[] = [],
|
|
387
396
|
): SequenceLayout {
|
|
388
397
|
const columns: SequenceColumn[] = [];
|
|
389
398
|
let cursor = 0;
|
|
@@ -399,11 +408,19 @@ export function layoutSequence(
|
|
|
399
408
|
height: SEQ_ROW,
|
|
400
409
|
}));
|
|
401
410
|
|
|
411
|
+
const centers = new Map(columns.map((column) => [column.id, column.centerX]));
|
|
412
|
+
let width = Math.max(0, cursor - SEQ_GAP);
|
|
413
|
+
for (const event of events) {
|
|
414
|
+
if (event.kind !== "message" || event.from !== event.to) continue;
|
|
415
|
+
const labelEnd = (centers.get(event.from) ?? 0) + SELF_LOOP_WIDTH + SELF_LABEL_GAP + Math.round(event.text.length * CHAR_WIDTH);
|
|
416
|
+
width = Math.max(width, labelEnd);
|
|
417
|
+
}
|
|
418
|
+
|
|
402
419
|
return {
|
|
403
420
|
columns,
|
|
404
421
|
rows,
|
|
405
422
|
headerHeight: SEQ_HEADER,
|
|
406
|
-
width
|
|
423
|
+
width,
|
|
407
424
|
height: SEQ_HEADER + eventCount * SEQ_ROW + 12,
|
|
408
425
|
};
|
|
409
426
|
}
|
package/shared/segment.ts
CHANGED
|
@@ -8,7 +8,15 @@
|
|
|
8
8
|
|
|
9
9
|
export type Segment =
|
|
10
10
|
| { kind: "text"; text: string }
|
|
11
|
-
| {
|
|
11
|
+
| {
|
|
12
|
+
kind: "mermaid";
|
|
13
|
+
/** The whole diagram, trimmed: what gets drawn. */
|
|
14
|
+
source: string;
|
|
15
|
+
/** The lines that ended in a newline, trimmed. All of `source` once the fence has closed. */
|
|
16
|
+
finished: string;
|
|
17
|
+
/** While the fence is open, the last line as typed so far (possibly empty); null once it has closed. */
|
|
18
|
+
pending: string | null;
|
|
19
|
+
};
|
|
12
20
|
|
|
13
21
|
const FENCE = /^[ \t]*(`{3,}|~{3,})[ \t]*([^\s`~]*)[ \t]*$/;
|
|
14
22
|
|
|
@@ -50,7 +58,8 @@ export function segmentMessage(text: string): Segment[] {
|
|
|
50
58
|
if (match && match[1][0] === fence[0] && match[1].length >= fence.length && match[2] === "") {
|
|
51
59
|
if (language === "mermaid") {
|
|
52
60
|
flushProse();
|
|
53
|
-
|
|
61
|
+
const source = body.join("\n").trim();
|
|
62
|
+
segments.push({ kind: "mermaid", source, finished: source, pending: null });
|
|
54
63
|
} else {
|
|
55
64
|
prose.push(`${fence}${language}`, ...body, fence);
|
|
56
65
|
}
|
|
@@ -71,7 +80,14 @@ export function segmentMessage(text: string): Segment[] {
|
|
|
71
80
|
if (fence !== null) {
|
|
72
81
|
if (language === "mermaid") {
|
|
73
82
|
flushProse();
|
|
74
|
-
|
|
83
|
+
// Split before trimming: the raw last line is the one still being typed, and
|
|
84
|
+
// trimming first would make a finished line that is followed by a space look pending.
|
|
85
|
+
segments.push({
|
|
86
|
+
kind: "mermaid",
|
|
87
|
+
source: body.join("\n").trim(),
|
|
88
|
+
finished: body.slice(0, -1).join("\n").trim(),
|
|
89
|
+
pending: body[body.length - 1] ?? "",
|
|
90
|
+
});
|
|
75
91
|
} else {
|
|
76
92
|
prose.push(`${fence}${language}`, ...body);
|
|
77
93
|
}
|
|
@@ -80,17 +96,6 @@ export function segmentMessage(text: string): Segment[] {
|
|
|
80
96
|
return segments;
|
|
81
97
|
}
|
|
82
98
|
|
|
83
|
-
/**
|
|
84
|
-
* True when the source already names a diagram this plugin draws.
|
|
85
|
-
*
|
|
86
|
-
* Half-arrived source parses to nothing, and complaining that it is unsupported
|
|
87
|
-
* would be wrong: it is unfinished, not unsupported.
|
|
88
|
-
*/
|
|
89
|
-
export function namesADiagram(source: string): boolean {
|
|
90
|
-
const first = source.split("\n").find((line) => line.trim().length > 0) ?? "";
|
|
91
|
-
return /^(flowchart|graph|sequenceDiagram)\b/.test(first.trim());
|
|
92
|
-
}
|
|
93
|
-
|
|
94
99
|
/** True when the message is worth taking over at all. */
|
|
95
100
|
export function hasMermaid(text: string): boolean {
|
|
96
101
|
return segmentMessage(text).some((segment) => segment.kind === "mermaid");
|
package/shared/sequence.ts
CHANGED
|
@@ -22,6 +22,7 @@ export interface SequenceDiagram {
|
|
|
22
22
|
skipped: string[];
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
const DIRECTIVE = /%%\{/;
|
|
25
26
|
const HEADER = /^\s*sequenceDiagram\s*$/i;
|
|
26
27
|
const PARTICIPANT = /^(participant|actor)\s+([\w.-]+)(?:\s+as\s+(.+))?$/i;
|
|
27
28
|
const NOTE = /^note\s+(?:(over)\s+([\w.,\s-]+)|(?:left|right)\s+of\s+([\w.-]+))\s*:\s*(.*)$/i;
|
|
@@ -55,13 +56,22 @@ export function parseSequence(source: string): SequenceDiagram | null {
|
|
|
55
56
|
};
|
|
56
57
|
|
|
57
58
|
for (const original of lines) {
|
|
59
|
+
// `%%{init: …}%%`, anywhere on a line, is a directive, not a comment: it configures the diagram, which this plugin does not do.
|
|
60
|
+
if (DIRECTIVE.test(original)) {
|
|
61
|
+
skipped.push(original.trim());
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
58
64
|
const line = original.replace(/%%.*$/, "").trim();
|
|
59
65
|
if (line.length === 0) continue;
|
|
60
66
|
if (HEADER.test(line)) {
|
|
61
67
|
started = true;
|
|
62
68
|
continue;
|
|
63
69
|
}
|
|
64
|
-
|
|
70
|
+
// Front matter and anything else before the header is not drawn, so it counts as skipped.
|
|
71
|
+
if (!started) {
|
|
72
|
+
skipped.push(original.trim());
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
65
75
|
|
|
66
76
|
const declared = PARTICIPANT.exec(line);
|
|
67
77
|
if (declared) {
|
package/shared/transform.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { PluginTimelineItem } from "@getpaseo/plugin";
|
|
2
|
+
import { drawable } from "./drawable";
|
|
2
3
|
import { segmentMessage } from "./segment";
|
|
3
4
|
|
|
4
5
|
export const DIAGRAM_KIND = "mermaid-diagram";
|
|
@@ -10,11 +11,23 @@ export const TEXT_KIND = "mermaid-text";
|
|
|
10
11
|
* Pure and synchronous because Paseo reruns transformers while reconciling
|
|
11
12
|
* projected history, and because it is the part worth testing.
|
|
12
13
|
*/
|
|
13
|
-
export function split(text: string): { items: PluginTimelineItem[] } | undefined {
|
|
14
|
+
export function split(text: string, phase: "streaming" | "complete"): { items: PluginTimelineItem[] } | undefined {
|
|
14
15
|
const segments = segmentMessage(text);
|
|
15
16
|
// Leave the entry alone unless there is a diagram to draw: Paseo renders
|
|
16
17
|
// ordinary Markdown better than this plugin does.
|
|
17
|
-
|
|
18
|
+
const diagrams = segments.filter((segment) => segment.kind === "mermaid");
|
|
19
|
+
if (diagrams.length === 0) return undefined;
|
|
20
|
+
// One diagram the plugin cannot draw in full leaves the whole message to Paseo,
|
|
21
|
+
// which draws every diagram in it with Mermaid itself.
|
|
22
|
+
// While the message streams, an open fence can still grow; once it is complete,
|
|
23
|
+
// every line is final, including the last line of a fence that never closed.
|
|
24
|
+
const growing = phase === "streaming";
|
|
25
|
+
const judged = diagrams.every((diagram) =>
|
|
26
|
+
growing && diagram.pending !== null ? drawable(diagram.finished, diagram.pending) : drawable(diagram.source, null),
|
|
27
|
+
);
|
|
28
|
+
if (!judged) return undefined;
|
|
29
|
+
// Reference links and footnotes resolve across the whole message; split, they would not.
|
|
30
|
+
if (segments.some((segment) => segment.kind === "text" && hasDefinition(segment.text))) return undefined;
|
|
18
31
|
|
|
19
32
|
// Pushed one branch at a time rather than mapped through a ternary: a ternary
|
|
20
33
|
// produces a union of the two shapes, and TypeScript gives the absent members
|
|
@@ -30,3 +43,28 @@ export function split(text: string): { items: PluginTimelineItem[] } | undefined
|
|
|
30
43
|
}
|
|
31
44
|
return { items };
|
|
32
45
|
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* True when the text holds anything shaped like a link reference definition or a
|
|
49
|
+
* footnote: a bracketed label, then a colon, anywhere. CommonMark allows definitions
|
|
50
|
+
* in list items and blockquotes and labels that span lines, so matching only
|
|
51
|
+
* well-formed ones at a line start misses real ones. Splitting the message would put
|
|
52
|
+
* a definition in a different Markdown document from its link; a false match, such
|
|
53
|
+
* as `a[i]:` in code, only costs the plugin's drawing, since Paseo renders the message.
|
|
54
|
+
*
|
|
55
|
+
* A scan rather than `/\[[^\]]+\]:/`: that regex backtracks quadratically on a long
|
|
56
|
+
* run of `[`, and this runs in the app on every streaming update. Each `]:` looks
|
|
57
|
+
* back only to the nearest bracket, so the work stays linear.
|
|
58
|
+
*/
|
|
59
|
+
function hasDefinition(text: string): boolean {
|
|
60
|
+
for (let end = text.indexOf("]:"); end >= 0; end = text.indexOf("]:", end + 2)) {
|
|
61
|
+
for (let at = end - 1; at >= 0; at -= 1) {
|
|
62
|
+
if (text[at] === "]") break;
|
|
63
|
+
if (text[at] === "[") {
|
|
64
|
+
if (at < end - 1) return true;
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
}
|