@pairdraw/mermaid 0.0.0-stage → 0.16.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/LICENSE +21 -0
- package/README.md +4 -2
- package/dist/chunks/detect.js +171 -0
- package/dist/chunks/detect.js.map +1 -0
- package/dist/chunks/flowchart/builder.js +538 -0
- package/dist/chunks/flowchart/builder.js.map +1 -0
- package/dist/chunks/flowchart/parse.js +3763 -0
- package/dist/chunks/flowchart/parse.js.map +1 -0
- package/dist/index.d.ts +155 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/package.json +42 -4
- package/src/detect.ts +14 -0
- package/src/flowchart/builder.ts +418 -0
- package/src/flowchart/model.ts +72 -0
- package/src/flowchart/parse.ts +19 -0
- package/src/flowchart/shapes.ts +105 -0
- package/src/index.ts +13 -0
- package/src/label.ts +37 -0
- package/src/source.ts +51 -0
- package/src/upstream/MERMAID-LICENSE.txt +21 -0
- package/src/upstream/comments.ts +13 -0
- package/src/upstream/flow.d.ts +8 -0
- package/src/upstream/flow.jison +635 -0
- package/src/upstream/flow.js +1099 -0
- package/src/upstream/preprocess.ts +62 -0
- package/src/upstream/regexes.ts +199 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
// Copied by `bun run generate:mermaid-sources` from mermaid@12.2.0
|
|
3
|
+
// (https://raw.githubusercontent.com/mermaid-js/mermaid/1fb7869866dd619168d840291c9c54b4f129758b/packages/mermaid/src/preprocess.ts)
|
|
4
|
+
// (https://raw.githubusercontent.com/mermaid-js/mermaid/1fb7869866dd619168d840291c9c54b4f129758b/packages/mermaid/src/utils.ts)
|
|
5
|
+
// Copyright (c) 2014 - 2022 Knut Sveidqvist. MIT License; see MERMAID-LICENSE.txt.
|
|
6
|
+
// Do not edit: change the pinned release in tools/generate/mermaid-sources and regenerate.
|
|
7
|
+
import { directiveRegex } from './regexes'
|
|
8
|
+
|
|
9
|
+
export const cleanupText = (code: string) => {
|
|
10
|
+
return (
|
|
11
|
+
code
|
|
12
|
+
// parser problems on CRLF ignore all CR and leave LF;;
|
|
13
|
+
.replace(/\r\n?/g, '\n')
|
|
14
|
+
// clean up html tags so that all attributes use single quotes, parser throws error on double quotes
|
|
15
|
+
.replace(
|
|
16
|
+
/<(\w+)([^>]*)>/g,
|
|
17
|
+
(match, tag, attributes) => '<' + tag + attributes.replace(/="([^"]*)"/g, "='$1'") + '>'
|
|
18
|
+
)
|
|
19
|
+
);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const removeDirectives = function (text: string): string {
|
|
23
|
+
return text.replace(directiveRegex, '');
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param text - text to be encoded
|
|
28
|
+
* @returns
|
|
29
|
+
*/
|
|
30
|
+
export const encodeEntities = function (text: string): string {
|
|
31
|
+
let txt = text;
|
|
32
|
+
|
|
33
|
+
txt = txt.replace(/style.*:\S*#.*;/g, function (s): string {
|
|
34
|
+
return s.substring(0, s.length - 1);
|
|
35
|
+
});
|
|
36
|
+
txt = txt.replace(/classDef.*:\S*#.*;/g, function (s): string {
|
|
37
|
+
return s.substring(0, s.length - 1);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
txt = txt.replace(/#\w+;/g, function (s) {
|
|
41
|
+
const innerTxt = s.substring(1, s.length - 1);
|
|
42
|
+
|
|
43
|
+
const isInt = /^\+?\d+$/.test(innerTxt);
|
|
44
|
+
if (isInt) {
|
|
45
|
+
return 'fl°°' + innerTxt + '¶ß';
|
|
46
|
+
} else {
|
|
47
|
+
return 'fl°' + innerTxt + '¶ß';
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return txt;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Partially reverts encoding done via {@link encodeEntities}
|
|
56
|
+
*
|
|
57
|
+
* @param text - text to be decoded
|
|
58
|
+
* @returns
|
|
59
|
+
*/
|
|
60
|
+
export const decodeEntities = function (text: string): string {
|
|
61
|
+
return text.replace(/fl°°/g, '&#').replace(/fl°/g, '&').replace(/¶ß/g, ';');
|
|
62
|
+
};
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
// Copied by `bun run generate:mermaid-sources` from mermaid@12.2.0
|
|
3
|
+
// (https://raw.githubusercontent.com/mermaid-js/mermaid/1fb7869866dd619168d840291c9c54b4f129758b/packages/mermaid/src/diagram-api/regexes.ts)
|
|
4
|
+
// Copyright (c) 2014 - 2022 Knut Sveidqvist. MIT License; see MERMAID-LICENSE.txt.
|
|
5
|
+
// Do not edit: change the pinned release in tools/generate/mermaid-sources and regenerate.
|
|
6
|
+
// Match Jekyll-style front matter blocks (https://jekyllrb.com/docs/front-matter/).
|
|
7
|
+
// Based on regex used by Jekyll: https://github.com/jekyll/jekyll/blob/6dd3cc21c40b98054851846425af06c64f9fb466/lib/jekyll/document.rb#L10
|
|
8
|
+
// Note that JS doesn't support the "\A" anchor, which means we can't use
|
|
9
|
+
// multiline mode.
|
|
10
|
+
// Relevant YAML spec: https://yaml.org/spec/1.2.2/#914-explicit-documents
|
|
11
|
+
// The \1 backreference anchors closing `---` to the same indent as the opening one, guards against indented `---` inside multi-line YAML scalars (#7613).
|
|
12
|
+
// Kept byte-for-byte as released: it is part of the published surface. It backtracks quadratically
|
|
13
|
+
// (see `matchFrontMatter` below), so internal hot paths must use `matchFrontMatter` instead of
|
|
14
|
+
// matching with this regex directly.
|
|
15
|
+
export const frontMatterRegex = /^([^\S\n\r]*)-{3}\s*[\n\r](.*?)[\n\r]\1-{3}\s*[\n\r]+/s;
|
|
16
|
+
|
|
17
|
+
export const directiveRegex =
|
|
18
|
+
/%{2}{\s*(?:(\w+)\s*:|(\w+))\s*(?:(\w+)|((?:(?!}%{2}).|\r?\n)*))?\s*(?:}%{2})?/gi;
|
|
19
|
+
|
|
20
|
+
// Kept byte-for-byte as released: it is part of the published surface, and consumers replace with
|
|
21
|
+
// `'\n'` — a pattern change (capture groups, lookbehind) would silently change their output or throw
|
|
22
|
+
// at module load on older engines. It backtracks quadratically on deep indents, so internal hot
|
|
23
|
+
// paths must use `stripAnyComments` below instead of replacing with this regex directly.
|
|
24
|
+
export const anyCommentRegex = /\s*%%.*\n/gm;
|
|
25
|
+
|
|
26
|
+
/** One character, tested against the same class `\s` means inside `anyCommentRegex`. */
|
|
27
|
+
const whitespace = /\s/;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Strip `%%` comment runs exactly like `text.replace(anyCommentRegex, '\n')`, in linear time.
|
|
31
|
+
*
|
|
32
|
+
* A scanner rather than a regex, because no variant of this pattern is linear. Every regex form
|
|
33
|
+
* carries a `\s*` that can cross newlines, and `/m` gives the engine a candidate start at each
|
|
34
|
+
* line, so an all-whitespace document has the match attempt rescan the remaining run once per
|
|
35
|
+
* line. Two shapes are quadratic in the released pattern and in the guarded `(^|\S)\s*%%.*\n`
|
|
36
|
+
* that replaced it, both inside the default 50k `maxTextSize`:
|
|
37
|
+
*
|
|
38
|
+
* ```
|
|
39
|
+
* ('\n' + ' '.repeat(4)).repeat(10_000) all whitespace, many lines 256ms
|
|
40
|
+
* '%%' + 'x%%'.repeat(16_000) no terminating newline 339ms
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* against 0.1ms for ordinary diagram text of the same size. The guard cut the constant roughly
|
|
44
|
+
* 40x but left the exponent alone, which is what CodeQL and the CWE-1333 review both caught.
|
|
45
|
+
*
|
|
46
|
+
* The scan walks forward once. For each `%%` it takes the line's terminating newline as the end
|
|
47
|
+
* of the match — `.` never matches a newline, so the regex ends at that same character — and
|
|
48
|
+
* extends left over the preceding whitespace run, never past the previous match. Each character
|
|
49
|
+
* is visited at most twice, so the work is linear in the input and independent of how the
|
|
50
|
+
* whitespace is arranged.
|
|
51
|
+
*
|
|
52
|
+
* A `%%` with no newline after it is left alone, because `%%.*\n` cannot match without one.
|
|
53
|
+
*/
|
|
54
|
+
export const stripAnyComments = (text: string): string => {
|
|
55
|
+
let out = '';
|
|
56
|
+
// Everything before this index has been emitted or dropped; also the floor for the leftward
|
|
57
|
+
// whitespace scan, which is what `lastIndex` does for the global regex.
|
|
58
|
+
let consumed = 0;
|
|
59
|
+
let searchFrom = 0;
|
|
60
|
+
|
|
61
|
+
while (searchFrom < text.length) {
|
|
62
|
+
const marker = text.indexOf('%%', searchFrom);
|
|
63
|
+
if (marker === -1) {
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
const lineEnd = text.indexOf('\n', marker);
|
|
67
|
+
if (lineEnd === -1) {
|
|
68
|
+
// No newline left in the string, so this `%%` and every later one cannot match.
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
let start = marker;
|
|
72
|
+
while (start > consumed && whitespace.test(text[start - 1])) {
|
|
73
|
+
start--;
|
|
74
|
+
}
|
|
75
|
+
out += text.slice(consumed, start) + '\n';
|
|
76
|
+
consumed = lineEnd + 1;
|
|
77
|
+
searchFrom = consumed;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return out + text.slice(consumed);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/** True for the characters `[\n\r]` means in `frontMatterRegex`. */
|
|
84
|
+
const isLineBreak = (char: string | undefined): boolean => char === '\n' || char === '\r';
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Index of the last `[\n\r]` in the whitespace run starting at `from`, or `-1` if the run holds
|
|
88
|
+
* none. This is where a greedy `\s*[\n\r]+` stops: `\s*` takes the whole run, then gives
|
|
89
|
+
* characters back until the next one is a line break.
|
|
90
|
+
*/
|
|
91
|
+
const lastLineBreakInWhitespaceRun = (text: string, from: number): number => {
|
|
92
|
+
let index = -1;
|
|
93
|
+
for (let i = from; i < text.length && whitespace.test(text[i]); i++) {
|
|
94
|
+
if (isLineBreak(text[i])) {
|
|
95
|
+
index = i;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return index;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export interface FrontMatterMatch {
|
|
102
|
+
/** The horizontal indent of the opening fence — `frontMatterRegex`'s capture group 1. */
|
|
103
|
+
indent: string;
|
|
104
|
+
/** The YAML body between the fences — capture group 2. */
|
|
105
|
+
body: string;
|
|
106
|
+
/** Length of the whole match, so callers can slice the front matter off. */
|
|
107
|
+
length: number;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Locate a front matter block exactly like `text.match(frontMatterRegex)`, in linear time.
|
|
112
|
+
*
|
|
113
|
+
* A scanner rather than a regex, because `frontMatterRegex` is ambiguous: the opening `\s*[\n\r]`
|
|
114
|
+
* lets `\s*` consume line breaks as well, so every failed search for a closing fence backtracks
|
|
115
|
+
* into it. Each of the O(n) split points then rescans the lazy body, which is quadratic overall —
|
|
116
|
+
* on `'---\n' + ' \n'.repeat(n)`, well inside the default 50k `maxTextSize`:
|
|
117
|
+
*
|
|
118
|
+
* ```
|
|
119
|
+
* n = 4_000 38ms
|
|
120
|
+
* n = 8_000 161ms
|
|
121
|
+
* n = 16_000 619ms
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* Removing the ambiguity is not behaviour-preserving, so the scanner reproduces the
|
|
125
|
+
* backtracking result rather than a tidier reading of it. The engine tries the *longest* opening
|
|
126
|
+
* first and shortens it one line break at a time until the rest matches, which is equivalent to:
|
|
127
|
+
*
|
|
128
|
+
* 1. Take the closing fences that could terminate a block — a line break, `indent---`, then at
|
|
129
|
+
* least one more line break in the following whitespace run.
|
|
130
|
+
* 2. The opening ends at the **last** line break of the run after `---` that still leaves a
|
|
131
|
+
* closing fence after it, since a longer opening is preferred but must leave one behind.
|
|
132
|
+
* 3. The body ends at the **first** closing fence after that, because the body is lazy.
|
|
133
|
+
*
|
|
134
|
+
* This is why `'---\n\n---\n\nMORE\n---\n'` is stripped whole: the opening swallows both leading
|
|
135
|
+
* line breaks, so the second `---` lands inside the body and only the third one can close.
|
|
136
|
+
*
|
|
137
|
+
* Two forward passes, each visiting a character a bounded number of times: the trailing
|
|
138
|
+
* whitespace runs of two closing fences cannot overlap, since `---` separates them.
|
|
139
|
+
*/
|
|
140
|
+
export const matchFrontMatter = (text: string): FrontMatterMatch | undefined => {
|
|
141
|
+
let cursor = 0;
|
|
142
|
+
while (cursor < text.length && whitespace.test(text[cursor]) && !isLineBreak(text[cursor])) {
|
|
143
|
+
cursor++;
|
|
144
|
+
}
|
|
145
|
+
const indent = text.slice(0, cursor);
|
|
146
|
+
|
|
147
|
+
if (!text.startsWith('---', cursor)) {
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Candidate opening ends: the line breaks of the whitespace run after the opening `---`.
|
|
152
|
+
const openingEnds: number[] = [];
|
|
153
|
+
for (let i = cursor + 3; i < text.length && whitespace.test(text[i]); i++) {
|
|
154
|
+
if (isLineBreak(text[i])) {
|
|
155
|
+
openingEnds.push(i);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if (openingEnds.length === 0) {
|
|
159
|
+
return undefined;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const closingFence = `${indent}---`;
|
|
163
|
+
// A closing fence is only usable if the trailing `\s*[\n\r]+` matches too.
|
|
164
|
+
const closingEnd = (i: number): number =>
|
|
165
|
+
isLineBreak(text[i]) && text.startsWith(closingFence, i + 1)
|
|
166
|
+
? lastLineBreakInWhitespaceRun(text, i + 1 + closingFence.length)
|
|
167
|
+
: -1;
|
|
168
|
+
|
|
169
|
+
const earliestBodyStart = openingEnds[0] + 1;
|
|
170
|
+
|
|
171
|
+
let lastClosingFence = -1;
|
|
172
|
+
for (let i = earliestBodyStart; i < text.length; i++) {
|
|
173
|
+
if (closingEnd(i) !== -1) {
|
|
174
|
+
lastClosingFence = i;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (lastClosingFence === -1) {
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Longest opening that still leaves a closing fence behind. One always exists: every closing
|
|
182
|
+
// fence sits at or after `earliestBodyStart`, which is past `openingEnds[0]`.
|
|
183
|
+
let openingEnd = openingEnds[0];
|
|
184
|
+
for (const candidate of openingEnds) {
|
|
185
|
+
if (candidate < lastClosingFence) {
|
|
186
|
+
openingEnd = candidate;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const bodyStart = openingEnd + 1;
|
|
191
|
+
for (let i = bodyStart; i <= lastClosingFence; i++) {
|
|
192
|
+
const matchEnd = closingEnd(i);
|
|
193
|
+
if (matchEnd !== -1) {
|
|
194
|
+
return { indent, body: text.slice(bodyStart, i), length: matchEnd + 1 };
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return undefined;
|
|
199
|
+
};
|