paseo-plugin-mermaid 0.4.2 → 0.5.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/OVERVIEW.md +14 -2
- package/README.md +78 -30
- package/client/diagram.tsx +263 -2
- package/client/file-panel.tsx +2 -1
- package/index.client.tsx +17 -6
- package/package.json +1 -1
- package/shared/classic-shapes.ts +126 -0
- package/shared/drawable.ts +30 -10
- package/shared/fence.ts +10 -0
- package/shared/file-view.ts +6 -8
- package/shared/flowchart.ts +298 -41
- package/shared/layout.ts +47 -7
- package/shared/segment.ts +104 -42
- package/shared/transform.ts +55 -12
package/shared/segment.ts
CHANGED
|
@@ -20,82 +20,144 @@ export type Segment =
|
|
|
20
20
|
|
|
21
21
|
const FENCE = /^[ \t]*(`{3,}|~{3,})[ \t]*([^\s`~]*)[ \t]*$/;
|
|
22
22
|
|
|
23
|
+
/** A run of the message: lines outside any fence, or one fenced block, with its lines as written. */
|
|
24
|
+
type Block =
|
|
25
|
+
| { kind: "lines"; lines: string[] }
|
|
26
|
+
| {
|
|
27
|
+
kind: "fence";
|
|
28
|
+
/** The opening marker: three or more backticks or tildes. */
|
|
29
|
+
marker: string;
|
|
30
|
+
/** Lower case; empty when the fence names none. */
|
|
31
|
+
language: string;
|
|
32
|
+
/** The opening line as written. */
|
|
33
|
+
open: string;
|
|
34
|
+
body: string[];
|
|
35
|
+
/** The closing line as written; null while the fence is still open. */
|
|
36
|
+
close: string | null;
|
|
37
|
+
};
|
|
38
|
+
|
|
23
39
|
/**
|
|
24
40
|
* Walks fenced blocks rather than reaching for one regex: a message can hold a
|
|
25
41
|
* shell block whose contents look like a fence, and only tracking the opening
|
|
26
|
-
* marker gets that right.
|
|
42
|
+
* marker gets that right. Every line lands in exactly one block, as written, so the
|
|
43
|
+
* blocks put back together give the message.
|
|
27
44
|
*/
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
let
|
|
32
|
-
let fence: string | null = null;
|
|
33
|
-
let language = "";
|
|
34
|
-
let body: string[] = [];
|
|
35
|
-
|
|
36
|
-
const flushProse = () => {
|
|
37
|
-
const joined = prose.join("\n").trim();
|
|
38
|
-
if (joined.length > 0) segments.push({ kind: "text", text: joined });
|
|
39
|
-
prose = [];
|
|
40
|
-
};
|
|
45
|
+
function fenceBlocks(text: string): Block[] {
|
|
46
|
+
const blocks: Block[] = [];
|
|
47
|
+
let lines: string[] = [];
|
|
48
|
+
let fence: Extract<Block, { kind: "fence" }> | null = null;
|
|
41
49
|
|
|
42
|
-
for (const line of
|
|
50
|
+
for (const line of text.split("\n")) {
|
|
43
51
|
const match = FENCE.exec(line);
|
|
44
52
|
|
|
45
53
|
if (fence === null) {
|
|
46
54
|
if (match) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
55
|
+
if (lines.length > 0) blocks.push({ kind: "lines", lines });
|
|
56
|
+
lines = [];
|
|
57
|
+
fence = { kind: "fence", marker: match[1], language: match[2].toLowerCase(), open: line, body: [], close: null };
|
|
50
58
|
} else {
|
|
51
|
-
|
|
59
|
+
lines.push(line);
|
|
52
60
|
}
|
|
53
61
|
continue;
|
|
54
62
|
}
|
|
55
63
|
|
|
56
64
|
// A closing fence must be at least as long as the opening one and carry no
|
|
57
65
|
// language, which is how nested fences inside a block stay part of the body.
|
|
58
|
-
if (match && match[1][0] === fence[0] && match[1].length >= fence.length && match[2] === "") {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const source = body.join("\n").trim();
|
|
62
|
-
segments.push({ kind: "mermaid", source, finished: source, pending: null });
|
|
63
|
-
} else {
|
|
64
|
-
prose.push(`${fence}${language}`, ...body, fence);
|
|
65
|
-
}
|
|
66
|
+
if (match && match[1][0] === fence.marker[0] && match[1].length >= fence.marker.length && match[2] === "") {
|
|
67
|
+
fence.close = line;
|
|
68
|
+
blocks.push(fence);
|
|
66
69
|
fence = null;
|
|
67
|
-
body = [];
|
|
68
70
|
continue;
|
|
69
71
|
}
|
|
70
72
|
|
|
71
|
-
body.push(line);
|
|
73
|
+
fence.body.push(line);
|
|
72
74
|
}
|
|
73
75
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
76
|
+
if (fence !== null) blocks.push(fence);
|
|
77
|
+
else if (lines.length > 0) blocks.push({ kind: "lines", lines });
|
|
78
|
+
return blocks;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** The parts of a message this plugin renders: prose and mermaid diagrams, in order. Blocks of other languages stay inside the prose. */
|
|
82
|
+
export function segmentMessage(text: string): Segment[] {
|
|
83
|
+
const segments: Segment[] = [];
|
|
84
|
+
let prose: string[] = [];
|
|
85
|
+
|
|
86
|
+
const flushProse = () => {
|
|
87
|
+
const joined = prose.join("\n").trim();
|
|
88
|
+
if (joined.length > 0) segments.push({ kind: "text", text: joined });
|
|
89
|
+
prose = [];
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
for (const block of fenceBlocks(text)) {
|
|
93
|
+
if (block.kind === "lines") {
|
|
94
|
+
prose.push(...block.lines);
|
|
95
|
+
} else if (block.language !== "mermaid") {
|
|
96
|
+
// Another language's block is prose, and an unterminated one stays prose.
|
|
97
|
+
prose.push(`${block.marker}${block.language}`, ...block.body, ...(block.close === null ? [] : [block.marker]));
|
|
98
|
+
} else if (block.close !== null) {
|
|
99
|
+
flushProse();
|
|
100
|
+
const source = block.body.join("\n").trim();
|
|
101
|
+
segments.push({ kind: "mermaid", source, finished: source, pending: null });
|
|
102
|
+
} else {
|
|
103
|
+
// An unterminated mermaid fence is claimed anyway, while the diagram is still
|
|
104
|
+
// arriving. Paseo renders ```mermaid fences itself, and it starts the moment the
|
|
105
|
+
// fence opens, so a transformer that politely waits for the closing fence has
|
|
106
|
+
// already lost the item to the host and only wins it back on the next
|
|
107
|
+
// reprojection. That is the whole reason a fresh diagram used to look different
|
|
108
|
+
// from the same diagram after a reload.
|
|
82
109
|
flushProse();
|
|
83
110
|
// Split before trimming: the raw last line is the one still being typed, and
|
|
84
111
|
// trimming first would make a finished line that is followed by a space look pending.
|
|
85
112
|
segments.push({
|
|
86
113
|
kind: "mermaid",
|
|
87
|
-
source: body.join("\n").trim(),
|
|
88
|
-
finished: body.slice(0, -1).join("\n").trim(),
|
|
89
|
-
pending: body[body.length - 1] ?? "",
|
|
114
|
+
source: block.body.join("\n").trim(),
|
|
115
|
+
finished: block.body.slice(0, -1).join("\n").trim(),
|
|
116
|
+
pending: block.body[block.body.length - 1] ?? "",
|
|
90
117
|
});
|
|
91
|
-
} else {
|
|
92
|
-
prose.push(`${fence}${language}`, ...body);
|
|
93
118
|
}
|
|
94
119
|
}
|
|
95
120
|
flushProse();
|
|
96
121
|
return segments;
|
|
97
122
|
}
|
|
98
123
|
|
|
124
|
+
export interface MapOptions {
|
|
125
|
+
/**
|
|
126
|
+
* Whether the message can still grow. Then the last line of a mermaid fence that is still
|
|
127
|
+
* open is the line being typed, and it is left out: the message ends at the newline before
|
|
128
|
+
* it. Paseo's Markdown draws every update as if it were final, so a half-typed `B --`
|
|
129
|
+
* would turn the diagram to code until its newline arrived. False by default.
|
|
130
|
+
*/
|
|
131
|
+
growing?: boolean;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The message with the body of each mermaid fence, closed or still open, passed through
|
|
136
|
+
* `rewrite`, and every other byte as it was: prose, other fences, the fence lines
|
|
137
|
+
* themselves, indentation and blank lines. Found by the same walk as `segmentMessage`,
|
|
138
|
+
* so what it calls a mermaid fence is what the plugin draws. `rewrite` gets the body as
|
|
139
|
+
* written, untrimmed, and an empty body is left alone.
|
|
140
|
+
*/
|
|
141
|
+
export function mapMermaidSources(text: string, rewrite: (source: string) => string, options: MapOptions = {}): string {
|
|
142
|
+
const lines: string[] = [];
|
|
143
|
+
for (const block of fenceBlocks(text)) {
|
|
144
|
+
if (block.kind === "lines") {
|
|
145
|
+
lines.push(...block.lines);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
lines.push(block.open);
|
|
149
|
+
let body = block.body;
|
|
150
|
+
if (block.language === "mermaid") {
|
|
151
|
+
// An open fence is the end of the message, so its last line is the one being typed.
|
|
152
|
+
if (options.growing && block.close === null && body.length > 0) body = [...body.slice(0, -1), ""];
|
|
153
|
+
if (body.length > 0) body = rewrite(body.join("\n")).split("\n");
|
|
154
|
+
}
|
|
155
|
+
lines.push(...body);
|
|
156
|
+
if (block.close !== null) lines.push(block.close);
|
|
157
|
+
}
|
|
158
|
+
return lines.join("\n");
|
|
159
|
+
}
|
|
160
|
+
|
|
99
161
|
/** True when the message is worth taking over at all. */
|
|
100
162
|
export function hasMermaid(text: string): boolean {
|
|
101
163
|
return segmentMessage(text).some((segment) => segment.kind === "mermaid");
|
package/shared/transform.ts
CHANGED
|
@@ -1,33 +1,69 @@
|
|
|
1
1
|
import type { PluginTimelineItem } from "@getpaseo/plugin";
|
|
2
|
+
import { classicShapes } from "./classic-shapes";
|
|
2
3
|
import { drawable } from "./drawable";
|
|
3
|
-
import {
|
|
4
|
+
import { mermaidFence } from "./fence";
|
|
5
|
+
import { mapMermaidSources, type Segment, segmentMessage } from "./segment";
|
|
6
|
+
|
|
7
|
+
type MermaidSegment = Extract<Segment, { kind: "mermaid" }>;
|
|
4
8
|
|
|
5
9
|
export const DIAGRAM_KIND = "mermaid-diagram";
|
|
6
10
|
export const TEXT_KIND = "mermaid-text";
|
|
7
11
|
|
|
12
|
+
export interface SplitOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Whether the app has Paseo's own Markdown (0.13 and later), which draws every
|
|
15
|
+
* Mermaid diagram type. Then no message with a diagram is left to Paseo: what this
|
|
16
|
+
* plugin cannot draw goes to that Markdown as a mermaid fence inside the plugin's
|
|
17
|
+
* own text item. Off by default, which leaves such a message to Paseo.
|
|
18
|
+
*/
|
|
19
|
+
hostMarkdown?: boolean;
|
|
20
|
+
/** Whether the app can shear a View; see `DrawableOptions`. True by default. */
|
|
21
|
+
skew?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
8
24
|
/**
|
|
9
25
|
* Turns one message into the items this plugin renders, or leaves it alone.
|
|
10
26
|
*
|
|
11
27
|
* Pure and synchronous because Paseo reruns transformers while reconciling
|
|
12
28
|
* projected history, and because it is the part worth testing.
|
|
13
29
|
*/
|
|
14
|
-
export function split(
|
|
30
|
+
export function split(
|
|
31
|
+
text: string,
|
|
32
|
+
phase: "streaming" | "complete",
|
|
33
|
+
options: SplitOptions = {},
|
|
34
|
+
): { items: PluginTimelineItem[] } | undefined {
|
|
35
|
+
const hostMarkdown = options.hostMarkdown ?? false;
|
|
36
|
+
const judge = { skew: options.skew ?? true };
|
|
15
37
|
const segments = segmentMessage(text);
|
|
16
38
|
// Leave the entry alone unless there is a diagram to draw: Paseo renders
|
|
17
39
|
// ordinary Markdown better than this plugin does.
|
|
18
40
|
const diagrams = segments.filter((segment) => segment.kind === "mermaid");
|
|
19
41
|
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
42
|
// While the message streams, an open fence can still grow; once it is complete,
|
|
23
43
|
// every line is final, including the last line of a fence that never closed.
|
|
24
44
|
const growing = phase === "streaming";
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
);
|
|
28
|
-
|
|
45
|
+
const open = (diagram: MermaidSegment) => growing && diagram.pending !== null;
|
|
46
|
+
// The lines of a diagram that will not change: while it grows, those that have ended in a newline.
|
|
47
|
+
const final = (diagram: MermaidSegment) => (open(diagram) ? diagram.finished : diagram.source);
|
|
48
|
+
const drawn = (diagram: MermaidSegment) => drawable(final(diagram), open(diagram) ? diagram.pending : null, judge);
|
|
29
49
|
// Reference links and footnotes resolve across the whole message; split, they would not.
|
|
30
|
-
|
|
50
|
+
const defined = segments.some((segment) => segment.kind === "text" && hasDefinition(segment.text));
|
|
51
|
+
|
|
52
|
+
if (hostMarkdown) {
|
|
53
|
+
// Paseo's Markdown reads the message in one piece, so the definition finds its link
|
|
54
|
+
// and it draws the message's diagrams itself, every one of them, so each is written
|
|
55
|
+
// the way its Mermaid accepts. Nothing but the mermaid fences' bodies changes, and
|
|
56
|
+
// while the message streams, the line an open fence is still typing waits for its newline.
|
|
57
|
+
if (defined) {
|
|
58
|
+
const whole = mapMermaidSources(text, classicShapes, { growing });
|
|
59
|
+
return { items: [{ type: "plugin", kind: TEXT_KIND, version: 1, data: { text: whole } }] };
|
|
60
|
+
}
|
|
61
|
+
} else {
|
|
62
|
+
// One diagram the plugin cannot draw in full leaves the whole message to Paseo,
|
|
63
|
+
// which draws every diagram in it with Mermaid itself.
|
|
64
|
+
if (!diagrams.every(drawn)) return undefined;
|
|
65
|
+
if (defined) return undefined;
|
|
66
|
+
}
|
|
31
67
|
|
|
32
68
|
// Pushed one branch at a time rather than mapped through a ternary: a ternary
|
|
33
69
|
// produces a union of the two shapes, and TypeScript gives the absent members
|
|
@@ -35,10 +71,15 @@ export function split(text: string, phase: "streaming" | "complete"): { items: P
|
|
|
35
71
|
// each literal on its own.
|
|
36
72
|
const items: PluginTimelineItem[] = [];
|
|
37
73
|
for (const segment of segments) {
|
|
38
|
-
if (segment.kind === "
|
|
74
|
+
if (segment.kind === "text") {
|
|
75
|
+
items.push({ type: "plugin", kind: TEXT_KIND, version: 1, data: { text: segment.text } });
|
|
76
|
+
} else if (!hostMarkdown || drawn(segment)) {
|
|
77
|
+
// Without Paseo's Markdown every diagram was judged drawable above.
|
|
39
78
|
items.push({ type: "plugin", kind: DIAGRAM_KIND, version: 1, data: { source: segment.source } });
|
|
40
79
|
} else {
|
|
41
|
-
|
|
80
|
+
// Only final lines: Paseo's Markdown draws every update as if it were the last, and a
|
|
81
|
+
// half-typed line would turn the diagram to code until its newline arrived.
|
|
82
|
+
items.push({ type: "plugin", kind: TEXT_KIND, version: 1, data: { text: mermaidFence(classicShapes(final(segment))) } });
|
|
42
83
|
}
|
|
43
84
|
}
|
|
44
85
|
return { items };
|
|
@@ -50,7 +91,9 @@ export function split(text: string, phase: "streaming" | "complete"): { items: P
|
|
|
50
91
|
* in list items and blockquotes and labels that span lines, so matching only
|
|
51
92
|
* well-formed ones at a line start misses real ones. Splitting the message would put
|
|
52
93
|
* 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
|
|
94
|
+
* as `a[i]:` in code, only costs the plugin's drawing: the message goes whole to
|
|
95
|
+
* Paseo's Markdown on 0.13 and newer, or to Paseo on 0.11 and 0.12, and Mermaid
|
|
96
|
+
* draws its diagrams either way.
|
|
54
97
|
*
|
|
55
98
|
* A scan rather than `/\[[^\]]+\]:/`: that regex backtracks quadratically on a long
|
|
56
99
|
* run of `[`, and this runs in the app on every streaming update. Each `]:` looks
|