@herbertgao/pi-extensions 2026.9.7 → 2026.9.8
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/node_modules/grok-mermaid/CHANGELOG.md +46 -0
- package/node_modules/grok-mermaid/LICENSE +205 -0
- package/node_modules/grok-mermaid/README.md +191 -0
- package/node_modules/grok-mermaid/dist/ansi.d.ts +16 -0
- package/node_modules/grok-mermaid/dist/ansi.d.ts.map +1 -0
- package/node_modules/grok-mermaid/dist/ansi.js +23 -0
- package/node_modules/grok-mermaid/dist/ansi.js.map +1 -0
- package/node_modules/grok-mermaid/dist/canvas.d.ts +87 -0
- package/node_modules/grok-mermaid/dist/canvas.d.ts.map +1 -0
- package/node_modules/grok-mermaid/dist/canvas.js +366 -0
- package/node_modules/grok-mermaid/dist/canvas.js.map +1 -0
- package/node_modules/grok-mermaid/dist/graph.d.ts +74 -0
- package/node_modules/grok-mermaid/dist/graph.d.ts.map +1 -0
- package/node_modules/grok-mermaid/dist/graph.js +91 -0
- package/node_modules/grok-mermaid/dist/graph.js.map +1 -0
- package/node_modules/grok-mermaid/dist/index.d.ts +32 -0
- package/node_modules/grok-mermaid/dist/index.d.ts.map +1 -0
- package/node_modules/grok-mermaid/dist/index.js +100 -0
- package/node_modules/grok-mermaid/dist/index.js.map +1 -0
- package/node_modules/grok-mermaid/dist/labels.d.ts +62 -0
- package/node_modules/grok-mermaid/dist/labels.d.ts.map +1 -0
- package/node_modules/grok-mermaid/dist/labels.js +324 -0
- package/node_modules/grok-mermaid/dist/labels.js.map +1 -0
- package/node_modules/grok-mermaid/dist/layout-seq.d.ts +12 -0
- package/node_modules/grok-mermaid/dist/layout-seq.d.ts.map +1 -0
- package/node_modules/grok-mermaid/dist/layout-seq.js +194 -0
- package/node_modules/grok-mermaid/dist/layout-seq.js.map +1 -0
- package/node_modules/grok-mermaid/dist/layout.d.ts +87 -0
- package/node_modules/grok-mermaid/dist/layout.d.ts.map +1 -0
- package/node_modules/grok-mermaid/dist/layout.js +881 -0
- package/node_modules/grok-mermaid/dist/layout.js.map +1 -0
- package/node_modules/grok-mermaid/dist/parse.d.ts +83 -0
- package/node_modules/grok-mermaid/dist/parse.d.ts.map +1 -0
- package/node_modules/grok-mermaid/dist/parse.js +1151 -0
- package/node_modules/grok-mermaid/dist/parse.js.map +1 -0
- package/node_modules/grok-mermaid/dist/source-box.d.ts +18 -0
- package/node_modules/grok-mermaid/dist/source-box.d.ts.map +1 -0
- package/node_modules/grok-mermaid/dist/source-box.js +78 -0
- package/node_modules/grok-mermaid/dist/source-box.js.map +1 -0
- package/node_modules/grok-mermaid/dist/types.d.ts +42 -0
- package/node_modules/grok-mermaid/dist/types.d.ts.map +1 -0
- package/node_modules/grok-mermaid/dist/types.js +1 -0
- package/node_modules/grok-mermaid/dist/types.js.map +1 -0
- package/node_modules/grok-mermaid/dist/width-data.d.ts +2 -0
- package/node_modules/grok-mermaid/dist/width-data.d.ts.map +1 -0
- package/node_modules/grok-mermaid/dist/width-data.js +994 -0
- package/node_modules/grok-mermaid/dist/width-data.js.map +1 -0
- package/node_modules/grok-mermaid/dist/width.d.ts +18 -0
- package/node_modules/grok-mermaid/dist/width.d.ts.map +1 -0
- package/node_modules/grok-mermaid/dist/width.js +76 -0
- package/node_modules/grok-mermaid/dist/width.js.map +1 -0
- package/node_modules/grok-mermaid/package.json +49 -0
- package/node_modules/grok-mermaid/src/ansi.ts +34 -0
- package/node_modules/grok-mermaid/src/canvas.ts +373 -0
- package/node_modules/grok-mermaid/src/graph.ts +142 -0
- package/node_modules/grok-mermaid/src/index.ts +104 -0
- package/node_modules/grok-mermaid/src/labels.ts +326 -0
- package/node_modules/grok-mermaid/src/layout-seq.ts +203 -0
- package/node_modules/grok-mermaid/src/layout.ts +1015 -0
- package/node_modules/grok-mermaid/src/parse.ts +1189 -0
- package/node_modules/grok-mermaid/src/source-box.ts +89 -0
- package/node_modules/grok-mermaid/src/types.ts +43 -0
- package/node_modules/grok-mermaid/src/width-data.ts +993 -0
- package/node_modules/grok-mermaid/src/width.ts +74 -0
- package/package.json +2 -1
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [Unreleased]
|
|
4
|
+
|
|
5
|
+
## [0.2.3] - 2026-08-11
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
- Parse and discard `:::class` for flowcharts, state and class diagrams.
|
|
10
|
+
|
|
11
|
+
## [0.2.2] - 2026-08-04
|
|
12
|
+
|
|
13
|
+
### Removed
|
|
14
|
+
|
|
15
|
+
- Removed the `NOTICE` file. Upstream ships none, so Apache-2.0 §4(d) never applied; keeping one only pushed a propagation obligation onto redistributors. Attribution now lives in `README.md` and `LICENSE`.
|
|
16
|
+
|
|
17
|
+
## [0.2.1] - 2026-08-01
|
|
18
|
+
|
|
19
|
+
### Added
|
|
20
|
+
|
|
21
|
+
- Added npm trusted publishing with build provenance. Releases are now built, signed and published by CI from a matching `v*` tag.
|
|
22
|
+
|
|
23
|
+
### Fixed
|
|
24
|
+
|
|
25
|
+
- Fixed leading and trailing empty rows around rendered diagrams.
|
|
26
|
+
|
|
27
|
+
## [0.2.0] - 2026-07-28
|
|
28
|
+
|
|
29
|
+
### Breaking Changes
|
|
30
|
+
|
|
31
|
+
- Changed `render()` to return natural-width art with a reported `width`; removed `maxWidth` and automatic source fallback. Callers can use `sourceBox()` when needed.
|
|
32
|
+
|
|
33
|
+
### Added
|
|
34
|
+
|
|
35
|
+
- Added `diagramKind()` and advisory parse warnings so callers can distinguish unsupported diagrams from malformed or partially rendered input.
|
|
36
|
+
- Added best-effort recovery for strict grammars by dropping and reporting one unreadable final line.
|
|
37
|
+
|
|
38
|
+
## [0.1.1] - 2026-07-28
|
|
39
|
+
|
|
40
|
+
## [0.1.0] - 2026-07-28
|
|
41
|
+
|
|
42
|
+
Initial public release.
|
|
43
|
+
|
|
44
|
+
### Added
|
|
45
|
+
|
|
46
|
+
- Added Unicode terminal rendering for flowchart, state, class, ER, and sequence diagrams with semantic styled spans and ANSI themes.
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
Copyright 2023-2026 SpaceXAI
|
|
2
|
+
Copyright 2026 Alexey Zaytsev
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
Apache License
|
|
6
|
+
Version 2.0, January 2004
|
|
7
|
+
http://www.apache.org/licenses/
|
|
8
|
+
|
|
9
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
10
|
+
|
|
11
|
+
1. Definitions.
|
|
12
|
+
|
|
13
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
14
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
15
|
+
|
|
16
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
17
|
+
the copyright owner that is granting the License.
|
|
18
|
+
|
|
19
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
20
|
+
other entities that control, are controlled by, or are under common
|
|
21
|
+
control with that entity. For the purposes of this definition,
|
|
22
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
23
|
+
direction or management of such entity, whether by contract or
|
|
24
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
25
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
26
|
+
|
|
27
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
28
|
+
exercising permissions granted by this License.
|
|
29
|
+
|
|
30
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
31
|
+
including but not limited to software source code, documentation
|
|
32
|
+
source, and configuration files.
|
|
33
|
+
|
|
34
|
+
"Object" form shall mean any form resulting from mechanical
|
|
35
|
+
transformation or translation of a Source form, including but
|
|
36
|
+
not limited to compiled object code, generated documentation,
|
|
37
|
+
and conversions to other media types.
|
|
38
|
+
|
|
39
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
40
|
+
Object form, made available under the License, as indicated by a
|
|
41
|
+
copyright notice that is included in or attached to the work
|
|
42
|
+
(an example is provided in the Appendix below).
|
|
43
|
+
|
|
44
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
45
|
+
form, that is based on (or derived from) the Work and for which the
|
|
46
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
47
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
48
|
+
of this License, Derivative Works shall not include works that remain
|
|
49
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
50
|
+
the Work and Derivative Works thereof.
|
|
51
|
+
|
|
52
|
+
"Contribution" shall mean any work of authorship, including
|
|
53
|
+
the original version of the Work and any modifications or additions
|
|
54
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
55
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
56
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
57
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
58
|
+
means any form of electronic, verbal, or written communication sent
|
|
59
|
+
to the Licensor or its representatives, including but not limited to
|
|
60
|
+
communication on electronic mailing lists, source code control systems,
|
|
61
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
62
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
63
|
+
excluding communication that is conspicuously marked or otherwise
|
|
64
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
65
|
+
|
|
66
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
67
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
68
|
+
subsequently incorporated within the Work.
|
|
69
|
+
|
|
70
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
73
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
74
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
75
|
+
Work and such Derivative Works in Source or Object form.
|
|
76
|
+
|
|
77
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
78
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
79
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
80
|
+
(except as stated in this section) patent license to make, have made,
|
|
81
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
82
|
+
where such license applies only to those patent claims licensable
|
|
83
|
+
by such Contributor that are necessarily infringed by their
|
|
84
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
85
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
86
|
+
institute patent litigation against any entity (including a
|
|
87
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
88
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
89
|
+
or contributory patent infringement, then any patent licenses
|
|
90
|
+
granted to You under this License for that Work shall terminate
|
|
91
|
+
as of the date such litigation is filed.
|
|
92
|
+
|
|
93
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
94
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
95
|
+
modifications, and in Source or Object form, provided that You
|
|
96
|
+
meet the following conditions:
|
|
97
|
+
|
|
98
|
+
(a) You must give any other recipients of the Work or
|
|
99
|
+
Derivative Works a copy of this License; and
|
|
100
|
+
|
|
101
|
+
(b) You must cause any modified files to carry prominent notices
|
|
102
|
+
stating that You changed the files; and
|
|
103
|
+
|
|
104
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
105
|
+
that You distribute, all copyright, patent, trademark, and
|
|
106
|
+
attribution notices from the Source form of the Work,
|
|
107
|
+
excluding those notices that do not pertain to any part of
|
|
108
|
+
the Derivative Works; and
|
|
109
|
+
|
|
110
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
111
|
+
distribution, then any Derivative Works that You distribute must
|
|
112
|
+
include a readable copy of the attribution notices contained
|
|
113
|
+
within such NOTICE file, excluding those notices that do not
|
|
114
|
+
pertain to any part of the Derivative Works, in at least one
|
|
115
|
+
of the following places: within a NOTICE text file distributed
|
|
116
|
+
as part of the Derivative Works; within the Source form or
|
|
117
|
+
documentation, if provided along with the Derivative Works; or,
|
|
118
|
+
within a display generated by the Derivative Works, if and
|
|
119
|
+
wherever such third-party notices normally appear. The contents
|
|
120
|
+
of the NOTICE file are for informational purposes only and
|
|
121
|
+
do not modify the License. You may add Your own attribution
|
|
122
|
+
notices within Derivative Works that You distribute, alongside
|
|
123
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
124
|
+
that such additional attribution notices cannot be construed
|
|
125
|
+
as modifying the License.
|
|
126
|
+
|
|
127
|
+
You may add Your own copyright statement to Your modifications and
|
|
128
|
+
may provide additional or different license terms and conditions
|
|
129
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
130
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
131
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
132
|
+
the conditions stated in this License.
|
|
133
|
+
|
|
134
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
135
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
136
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
137
|
+
this License, without any additional terms or conditions.
|
|
138
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
139
|
+
the terms of any separate license agreement you may have executed
|
|
140
|
+
with Licensor regarding such Contributions.
|
|
141
|
+
|
|
142
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
143
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
144
|
+
except as required for reasonable and customary use in describing the
|
|
145
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
146
|
+
|
|
147
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
148
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
149
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
150
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
151
|
+
implied, including, without limitation, any warranties or conditions
|
|
152
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
153
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
154
|
+
appropriateness of using or redistributing the Work and assume any
|
|
155
|
+
risks associated with Your exercise of permissions under this License.
|
|
156
|
+
|
|
157
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
158
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
159
|
+
unless required by applicable law (such as deliberate and grossly
|
|
160
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
161
|
+
liable to You for damages, including any direct, indirect, special,
|
|
162
|
+
incidental, or consequential damages of any character arising as a
|
|
163
|
+
result of this License or out of the use or inability to use the
|
|
164
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
165
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
166
|
+
other commercial damages or losses), even if such Contributor
|
|
167
|
+
has been advised of the possibility of such damages.
|
|
168
|
+
|
|
169
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
170
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
171
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
172
|
+
or other liability obligations and/or rights consistent with this
|
|
173
|
+
License. However, in accepting such obligations, You may act only
|
|
174
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
175
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
176
|
+
defend, and hold each Contributor harmless for any liability
|
|
177
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
178
|
+
of your accepting any such warranty or additional liability.
|
|
179
|
+
|
|
180
|
+
END OF TERMS AND CONDITIONS
|
|
181
|
+
|
|
182
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
183
|
+
|
|
184
|
+
To apply the Apache License to your work, attach the following
|
|
185
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
186
|
+
replaced with your own identifying information. (Don't include
|
|
187
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
188
|
+
comment syntax for the file format. We also recommend that a
|
|
189
|
+
file or class name and description of purpose be included on the
|
|
190
|
+
same "printed page" as the copyright notice for easier
|
|
191
|
+
identification within third-party archives.
|
|
192
|
+
|
|
193
|
+
Copyright [yyyy] [name of copyright owner]
|
|
194
|
+
|
|
195
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
196
|
+
you may not use this file except in compliance with the License.
|
|
197
|
+
You may obtain a copy of the License at
|
|
198
|
+
|
|
199
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
200
|
+
|
|
201
|
+
Unless required by applicable law or agreed to in writing, software
|
|
202
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
203
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
204
|
+
See the License for the specific language governing permissions and
|
|
205
|
+
limitations under the License.
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# grok-mermaid
|
|
2
|
+
|
|
3
|
+
Render Mermaid diagrams as Unicode box-drawing art, for terminals.
|
|
4
|
+
|
|
5
|
+
A TypeScript port of the terminal Mermaid renderer in
|
|
6
|
+
[xai-org/grok-build](https://github.com/xai-org/grok-build)
|
|
7
|
+
(`crates/codegen/xai-grok-markdown/src/mermaid.rs`). No browser, no headless
|
|
8
|
+
Chrome, no SVG — a self-contained layout engine that emits text.
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
┌──────────────┐
|
|
12
|
+
│ Parse source │
|
|
13
|
+
└───────┬──────┘
|
|
14
|
+
│
|
|
15
|
+
▼
|
|
16
|
+
╭────────────╮
|
|
17
|
+
│ Supported? │
|
|
18
|
+
╰──────┬─────╯
|
|
19
|
+
┌───────┴────────┐
|
|
20
|
+
▼yes ▼no
|
|
21
|
+
┌─────────┐ ┌───────────────┐
|
|
22
|
+
│ Lay out │ │ Framed source │
|
|
23
|
+
└────┬────┘ └───────┬───────┘
|
|
24
|
+
└───────┬────────┘
|
|
25
|
+
▼
|
|
26
|
+
┌─────────────┐
|
|
27
|
+
│ Unicode art │
|
|
28
|
+
└─────────────┘
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Install
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
npm install grok-mermaid
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { render } from 'grok-mermaid'
|
|
41
|
+
|
|
42
|
+
const art = render('flowchart LR\n A[Start] --> B[Done]')
|
|
43
|
+
if (art) console.log(art.plain.join('\n'))
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`render` draws the diagram at whatever size it needs and reports that as
|
|
47
|
+
`art.width`. It returns `null` when there is no art to show: blank input, a
|
|
48
|
+
syntax error, a diagram type it does not draw, or one large enough that laying
|
|
49
|
+
it out is refused.
|
|
50
|
+
|
|
51
|
+
### Syntax errors
|
|
52
|
+
|
|
53
|
+
Rendering is best-effort: a source that does not fully parse still draws what it
|
|
54
|
+
can, and reports the rest in `art.warnings`.
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
// Flowcharts are lenient, as mermaid.js is: the parseable prefix survives.
|
|
58
|
+
render('graph TD\n A[Start --> B')
|
|
59
|
+
// plain one box labelled `Start --> B` — the edge you wrote is gone
|
|
60
|
+
// warnings ['node "A": label is missing its closing `]`']
|
|
61
|
+
|
|
62
|
+
// The rest fail on any unreadable statement, but retry once without the last
|
|
63
|
+
// line — the one a half-finished source ends on.
|
|
64
|
+
render('stateDiagram-v2\n A --> B\n some garbage line')
|
|
65
|
+
// plain the A --> B transition, drawn
|
|
66
|
+
// warnings ['dropped, unreadable final line: "some garbage line"']
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
An empty `warnings` means the whole source made it into the art.
|
|
70
|
+
|
|
71
|
+
**Warnings are advisory — never gate rendering on them.** The art is the best
|
|
72
|
+
available drawing either way, and a diagram mid-edit warns at nearly every
|
|
73
|
+
intermediate state: a label bracket is unterminated right up until it is typed.
|
|
74
|
+
|
|
75
|
+
`diagramKind(src)` reads the header alone, separating the two `null`s worth
|
|
76
|
+
different messages:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
if (render(src) === null) {
|
|
80
|
+
const kind = diagramKind(src) // 'flowchart' | 'state' | 'class' | 'er' | 'sequence' | null
|
|
81
|
+
console.log(kind ? `${kind} diagram: syntax error` : 'diagram type not supported here')
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Streaming
|
|
86
|
+
|
|
87
|
+
Call `render` on each prefix as it arrives — no special handling, no waiting for
|
|
88
|
+
a complete diagram. Best-effort parsing is what keeps it drawn instead of
|
|
89
|
+
alternating with the source box.
|
|
90
|
+
|
|
91
|
+
<img src="https://raw.githubusercontent.com/xl0/grok-mermaid/master/docs/streaming.gif" width="900" alt="A terminal replaying a stream of Mermaid diagrams — flowcharts and a state machine drawing themselves as box-drawing art, each one growing in place without ever reverting to a block of source text.">
|
|
92
|
+
|
|
93
|
+
### Fitting a viewport
|
|
94
|
+
|
|
95
|
+
The renderer takes no width limit. Nothing about a terminal tells it whether a
|
|
96
|
+
wide diagram should be shrunk, scrolled, linked to an image or just printed, so
|
|
97
|
+
the decision stays with you — compare `art.width` against the space you have:
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import { render, sourceBox } from 'grok-mermaid'
|
|
101
|
+
|
|
102
|
+
const cols = process.stdout.columns
|
|
103
|
+
const art = render(src)
|
|
104
|
+
if (art && art.width <= cols) console.log(art.plain.join('\n'))
|
|
105
|
+
else {
|
|
106
|
+
console.log(sourceBox(src, cols).plain.join('\n'))
|
|
107
|
+
console.log(`(diagram needs ${art?.width ?? '?'} columns)`)
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
`sourceBox(src, maxWidth?)` frames the source in a titled box, hard-wrapping to
|
|
112
|
+
`maxWidth`. It is the usual thing to show when the art does not fit or does not
|
|
113
|
+
exist, but it is yours to choose and yours to caption.
|
|
114
|
+
|
|
115
|
+
### Colour
|
|
116
|
+
|
|
117
|
+
The core is colour-blind. `styled` carries the same rows as `plain`, split into
|
|
118
|
+
runs tagged with a semantic class, so you map classes to your own theme:
|
|
119
|
+
|
|
120
|
+
<img src="https://raw.githubusercontent.com/xl0/grok-mermaid/master/docs/demo.svg" width="330" alt="A flowchart rendered as Unicode box-drawing art on a dark panel: grey box outlines, white node labels, cyan connectors and arrowheads, grey edge labels.">
|
|
121
|
+
|
|
122
|
+
That image is real `render()` output painted through one such theme.
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
import { type Cls, render } from 'grok-mermaid'
|
|
126
|
+
|
|
127
|
+
const art = render(src)!
|
|
128
|
+
|
|
129
|
+
const theme: Partial<Record<Cls, (s: string) => string>> = {
|
|
130
|
+
border: dim, text: white, edge: cyan, edgeLabel: gray,
|
|
131
|
+
}
|
|
132
|
+
const out = art.styled.map((row) =>
|
|
133
|
+
row.map((span) => (theme[span.cls] ?? identity)(span.text)).join(''),
|
|
134
|
+
)
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
| Class | What it covers |
|
|
138
|
+
| --- | --- |
|
|
139
|
+
| `border` | box outlines, subgraph frames, compartment rules |
|
|
140
|
+
| `text` | node, participant and compartment labels |
|
|
141
|
+
| `edge` | connector lines and arrowheads |
|
|
142
|
+
| `edgeLabel` | text sitting on an edge |
|
|
143
|
+
| `title` | the `mermaid: <kind>` header of a source box |
|
|
144
|
+
| `none` | blank filler |
|
|
145
|
+
|
|
146
|
+
`styled[i]` joined is always exactly `plain[i]`, so you can swap between them
|
|
147
|
+
freely. A render is plain JSON: cacheable across theme changes, transferable to
|
|
148
|
+
a worker.
|
|
149
|
+
|
|
150
|
+
For the common case there is a helper:
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
import { render, toAnsi } from 'grok-mermaid'
|
|
154
|
+
|
|
155
|
+
console.log(toAnsi(render(src)!).join('\n'))
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
`toAnsi(art, theme)` takes `Partial<Record<Cls, string>>` of SGR parameters
|
|
159
|
+
(`'2'` dim, `'36'` cyan, `'38;5;244'` for 256-colour), defaulting to a dim
|
|
160
|
+
frame with cyan connectors.
|
|
161
|
+
|
|
162
|
+
## Supported diagrams
|
|
163
|
+
|
|
164
|
+
| Type | Notes |
|
|
165
|
+
| --- | --- |
|
|
166
|
+
| `graph` / `flowchart` | `TD`/`TB`, `BT`, `LR`, `RL`; `subgraph` nesting; node shapes; solid/dotted/thick links; arrow, circle, cross heads; edge labels |
|
|
167
|
+
| `stateDiagram` / `stateDiagram-v2` | states, transitions, `[*]` start/end, `<<choice>>`, descriptions, composite states flattened |
|
|
168
|
+
| `classDiagram` | compartments, annotations, generics, cardinalities, inheritance/realization/composition/aggregation/dependency |
|
|
169
|
+
| `erDiagram` | entities, attributes, crow's-foot cardinalities |
|
|
170
|
+
| `sequenceDiagram` | participants, messages, self-messages, notes, `loop`/`alt`/`opt` dividers, `autonumber` |
|
|
171
|
+
|
|
172
|
+
## Credits
|
|
173
|
+
|
|
174
|
+
Inspired by Simon Willison's
|
|
175
|
+
[grok-mermaid.html](https://tools.simonwillison.net/grok-mermaid)
|
|
176
|
+
([source](https://github.com/simonw/tools/blob/main/grok-mermaid.html)), which
|
|
177
|
+
compiles the original Rust renderer to WebAssembly so it runs in a browser.
|
|
178
|
+
That demo is what made the renderer worth having outside the Grok CLI; this
|
|
179
|
+
port takes the other route and reimplements it in TypeScript, so it needs no
|
|
180
|
+
WASM and runs anywhere JS does.
|
|
181
|
+
|
|
182
|
+
100% of the code written by Opus 5, with a healthy dose of feedback and direction from my side.
|
|
183
|
+
|
|
184
|
+
## License
|
|
185
|
+
|
|
186
|
+
Apache-2.0. See [LICENSE](LICENSE).
|
|
187
|
+
|
|
188
|
+
The Rust original in [xai-org/grok-build](https://github.com/xai-org/grok-build)
|
|
189
|
+
(`crates/codegen/xai-grok-markdown/src/mermaid.rs`) is Apache-2.0, Copyright
|
|
190
|
+
2023-2026 SpaceXAI. Its layout algorithms, glyph tables, parser behaviour and
|
|
191
|
+
test corpus are what this port is derived from.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Cls, MermaidArt } from './types.ts';
|
|
2
|
+
/**
|
|
3
|
+
* SGR parameter per semantic class, e.g. `'2'` for dim, `'36'` for cyan,
|
|
4
|
+
* `'38;5;244'` for a 256-colour index. A class left out is printed unstyled.
|
|
5
|
+
*/
|
|
6
|
+
export type AnsiTheme = Partial<Record<Cls, string>>;
|
|
7
|
+
/** Dim frame, plain labels, cyan connectors. Readable on light and dark. */
|
|
8
|
+
export declare const DEFAULT_THEME: AnsiTheme;
|
|
9
|
+
/**
|
|
10
|
+
* Render art to ANSI-coloured lines.
|
|
11
|
+
*
|
|
12
|
+
* A convenience over mapping `art.styled` yourself — reach for that directly
|
|
13
|
+
* when your TUI has its own styling model.
|
|
14
|
+
*/
|
|
15
|
+
export declare function toAnsi(art: MermaidArt, theme?: AnsiTheme): string[];
|
|
16
|
+
//# sourceMappingURL=ansi.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ansi.d.ts","sourceRoot":"","sources":["../src/ansi.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAIjD;;;GAGG;AACH,MAAM,MAAM,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,CAAA;AAEpD,4EAA4E;AAC5E,eAAO,MAAM,aAAa,EAAE,SAK3B,CAAA;AAED;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,GAAE,SAAyB,GAAG,MAAM,EAAE,CASlF"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const ESC = String.fromCharCode(27);
|
|
2
|
+
/** Dim frame, plain labels, cyan connectors. Readable on light and dark. */
|
|
3
|
+
export const DEFAULT_THEME = {
|
|
4
|
+
border: '2',
|
|
5
|
+
edge: '36',
|
|
6
|
+
edgeLabel: '2;36',
|
|
7
|
+
title: '1',
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Render art to ANSI-coloured lines.
|
|
11
|
+
*
|
|
12
|
+
* A convenience over mapping `art.styled` yourself — reach for that directly
|
|
13
|
+
* when your TUI has its own styling model.
|
|
14
|
+
*/
|
|
15
|
+
export function toAnsi(art, theme = DEFAULT_THEME) {
|
|
16
|
+
return art.styled.map((row) => row
|
|
17
|
+
.map((span) => {
|
|
18
|
+
const sgr = theme[span.cls];
|
|
19
|
+
return sgr === undefined ? span.text : `${ESC}[${sgr}m${span.text}${ESC}[0m`;
|
|
20
|
+
})
|
|
21
|
+
.join(''));
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=ansi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ansi.js","sourceRoot":"","sources":["../src/ansi.ts"],"names":[],"mappings":"AAEA,MAAM,GAAG,GAAG,MAAM,CAAC,YAAY,CAAC,EAAE,CAAC,CAAA;AAQnC,4EAA4E;AAC5E,MAAM,CAAC,MAAM,aAAa,GAAc;IACtC,MAAM,EAAE,GAAG;IACX,IAAI,EAAE,IAAI;IACV,SAAS,EAAE,MAAM;IACjB,KAAK,EAAE,GAAG;CACX,CAAA;AAED;;;;;GAKG;AACH,MAAM,UAAU,MAAM,CAAC,GAAe,EAAE,KAAK,GAAc,aAAa;IACtE,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAC5B,GAAG;SACA,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;QACZ,MAAM,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;QAC3B,OAAO,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,GAAG,IAAI,GAAG,IAAI,IAAI,CAAC,IAAI,GAAG,GAAG,KAAK,CAAA;IAC9E,CAAC,CAAC;SACD,IAAI,CAAC,EAAE,CAAC,CACZ,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { Cls, Span } from './types.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Sentinel occupying the trailing column of a wide glyph. Never emitted: the
|
|
4
|
+
* line builder skips it so a CJK character claims two cells of layout but
|
|
5
|
+
* contributes one character of output.
|
|
6
|
+
*/
|
|
7
|
+
export declare const CONT: string;
|
|
8
|
+
/** Connection direction bits, combined into a box-drawing glyph by `maskChar`. */
|
|
9
|
+
export declare const U = 1;
|
|
10
|
+
export declare const D = 2;
|
|
11
|
+
export declare const L = 4;
|
|
12
|
+
export declare const R = 8;
|
|
13
|
+
/** Line styles, tracked per cell so crossing edges keep their own stroke. */
|
|
14
|
+
export declare const STY_DOT = 1;
|
|
15
|
+
export declare const STY_THICK = 2;
|
|
16
|
+
export declare const STY_SOLID = 4;
|
|
17
|
+
/**
|
|
18
|
+
* A grid of cells. Edges accumulate as direction bits rather than glyphs so
|
|
19
|
+
* that crossings and junctions resolve correctly whatever order they are drawn
|
|
20
|
+
* in; `finalizeMask` turns the accumulated bits into characters at the end.
|
|
21
|
+
*
|
|
22
|
+
* `occupied` marks cells claimed by a box, which edge bits must not overwrite.
|
|
23
|
+
*/
|
|
24
|
+
export declare class Canvas {
|
|
25
|
+
readonly w: number;
|
|
26
|
+
readonly h: number;
|
|
27
|
+
ch: string[];
|
|
28
|
+
cls: Cls[];
|
|
29
|
+
mask: Uint8Array;
|
|
30
|
+
style: Uint8Array;
|
|
31
|
+
occupied: Uint8Array;
|
|
32
|
+
curStyle: number;
|
|
33
|
+
constructor(w: number, h: number);
|
|
34
|
+
idx(x: number, y: number): number;
|
|
35
|
+
set(x: number, y: number, c: string, cls: Cls): void;
|
|
36
|
+
/**
|
|
37
|
+
* Accumulate direction bits on a free cell.
|
|
38
|
+
*
|
|
39
|
+
* `cls` is the class to claim the cell for; `border` cells are never
|
|
40
|
+
* reclassified, so a connector meeting a box keeps the box's styling.
|
|
41
|
+
*/
|
|
42
|
+
addBits(x: number, y: number, bits: number, cls?: Cls): void;
|
|
43
|
+
/** Stamp a finished sub-canvas (a subgraph frame's contents) at an offset. */
|
|
44
|
+
blit(sub: Canvas, ox: number, oy: number): void;
|
|
45
|
+
/** Add direction bits even to an occupied cell, so an edge can meet a border. */
|
|
46
|
+
junction(x: number, y: number, bits: number): void;
|
|
47
|
+
segV(x: number, y0: number, y1: number): void;
|
|
48
|
+
segH(y: number, x0: number, x1: number): void;
|
|
49
|
+
/** Resolve accumulated direction bits into glyphs, honouring line style. */
|
|
50
|
+
finalizeMask(): void;
|
|
51
|
+
/**
|
|
52
|
+
* Mirror top-to-bottom for `BT`. Rows reorder but within-row text does not,
|
|
53
|
+
* so labels stay readable; box-drawing glyphs flip to match.
|
|
54
|
+
*/
|
|
55
|
+
flipVertical(): void;
|
|
56
|
+
/**
|
|
57
|
+
* Mirror left-to-right for `RL`. Mirroring reverses each row, so after
|
|
58
|
+
* flipping glyphs each text/label run is reversed back to reading order.
|
|
59
|
+
*/
|
|
60
|
+
flipHorizontal(): void;
|
|
61
|
+
/** Group each row into runs of one class, dropping wide-glyph continuations. */
|
|
62
|
+
toLines(): {
|
|
63
|
+
plain: string[];
|
|
64
|
+
styled: Span[][];
|
|
65
|
+
width: number;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Paint `text` at `x, y`, one grapheme cluster per cell.
|
|
70
|
+
*
|
|
71
|
+
* A wide cluster claims a second cell, marked with `CONT` so the line builder
|
|
72
|
+
* emits one character for it rather than a stray space.
|
|
73
|
+
*/
|
|
74
|
+
export declare function drawText(canvas: Canvas, text: string, x: number, y: number, cls: Cls): void;
|
|
75
|
+
/**
|
|
76
|
+
* Paint `text` at `x, y`, clearing any edge bits underneath first.
|
|
77
|
+
*
|
|
78
|
+
* Used where text sits on top of a drawn line (sequence messages, dividers,
|
|
79
|
+
* compartment rows) and must win over it.
|
|
80
|
+
*/
|
|
81
|
+
export declare function drawTextOverEdges(canvas: Canvas, text: string, x: number, y: number, cls: Cls): void;
|
|
82
|
+
export declare function maskChar(mask: number): string;
|
|
83
|
+
export declare const dottedChar: (c: string) => string;
|
|
84
|
+
export declare const thickChar: (c: string) => string;
|
|
85
|
+
export declare const flipGlyphV: (c: string) => string;
|
|
86
|
+
export declare const flipGlyphH: (c: string) => string;
|
|
87
|
+
//# sourceMappingURL=canvas.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"canvas.d.ts","sourceRoot":"","sources":["../src/canvas.ts"],"names":[],"mappings":"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"}
|