@zombie-mermaid/mermaid-parser 2.2.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 +22 -0
- package/dist/index.cjs +3 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +879 -0
- package/dist/index.d.ts +879 -0
- package/dist/index.js +994 -0
- package/dist/index.js.map +1 -0
- package/package.json +35 -0
- package/src/__tests__/box-color.test.ts +80 -0
- package/src/__tests__/sequence-activation-fix.test.ts +91 -0
- package/src/__tests__/xychart-colors.test.ts +149 -0
- package/src/class/format.ts +15 -0
- package/src/class/parser.ts +593 -0
- package/src/class/types.ts +166 -0
- package/src/er/parser.ts +286 -0
- package/src/er/types.ts +99 -0
- package/src/expanded-shapes.ts +376 -0
- package/src/index.ts +48 -0
- package/src/sequence/activation-check.ts +149 -0
- package/src/sequence/activation-fix.ts +112 -0
- package/src/sequence/box-color.ts +85 -0
- package/src/sequence/parser.ts +613 -0
- package/src/sequence/types.ts +242 -0
- package/src/xychart/colors.ts +177 -0
- package/src/xychart/parser.ts +246 -0
- package/src/xychart/types.ts +150 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// `box <color?> <label?>` header parsing
|
|
3
|
+
//
|
|
4
|
+
// Mermaid's sequenceDb.parseBoxData takes the first word of the header (or a
|
|
5
|
+
// leading `rgb(...)`/`hsl(...)` call), asks the browser whether it is a valid
|
|
6
|
+
// CSS colour, and — if it isn't — treats the whole header as the label. There
|
|
7
|
+
// is no browser here, so "is a colour" is decided by a fixed list of CSS
|
|
8
|
+
// named colours plus the hex / rgb() / hsl() function forms. That list is
|
|
9
|
+
// also what keeps a diagram-supplied string safe to emit inside an SVG
|
|
10
|
+
// attribute: anything not matching it never reaches the renderer as a colour.
|
|
11
|
+
// ============================================================================
|
|
12
|
+
|
|
13
|
+
/** The CSS Color Module Level 4 named colours (plus `transparent`, handled separately). */
|
|
14
|
+
const CSS_NAMED_COLORS = new Set(
|
|
15
|
+
(
|
|
16
|
+
'aliceblue antiquewhite aqua aquamarine azure beige bisque black ' +
|
|
17
|
+
'blanchedalmond blue blueviolet brown burlywood cadetblue chartreuse ' +
|
|
18
|
+
'chocolate coral cornflowerblue cornsilk crimson cyan darkblue darkcyan ' +
|
|
19
|
+
'darkgoldenrod darkgray darkgreen darkgrey darkkhaki darkmagenta ' +
|
|
20
|
+
'darkolivegreen darkorange darkorchid darkred darksalmon darkseagreen ' +
|
|
21
|
+
'darkslateblue darkslategray darkslategrey darkturquoise darkviolet ' +
|
|
22
|
+
'deeppink deepskyblue dimgray dimgrey dodgerblue firebrick floralwhite ' +
|
|
23
|
+
'forestgreen fuchsia gainsboro ghostwhite gold goldenrod gray green ' +
|
|
24
|
+
'greenyellow grey honeydew hotpink indianred indigo ivory khaki lavender ' +
|
|
25
|
+
'lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan ' +
|
|
26
|
+
'lightgoldenrodyellow lightgray lightgreen lightgrey lightpink ' +
|
|
27
|
+
'lightsalmon lightseagreen lightskyblue lightslategray lightslategrey ' +
|
|
28
|
+
'lightsteelblue lightyellow lime limegreen linen magenta maroon ' +
|
|
29
|
+
'mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen ' +
|
|
30
|
+
'mediumslateblue mediumspringgreen mediumturquoise mediumvioletred ' +
|
|
31
|
+
'midnightblue mintcream mistyrose moccasin navajowhite navy oldlace olive ' +
|
|
32
|
+
'olivedrab orange orangered orchid palegoldenrod palegreen paleturquoise ' +
|
|
33
|
+
'palevioletred papayawhip peachpuff peru pink plum powderblue purple ' +
|
|
34
|
+
'rebeccapurple red rosybrown royalblue saddlebrown salmon sandybrown ' +
|
|
35
|
+
'seagreen seashell sienna silver skyblue slateblue slategray slategrey ' +
|
|
36
|
+
'snow springgreen steelblue tan teal thistle tomato turquoise violet ' +
|
|
37
|
+
'wheat white whitesmoke yellow yellowgreen'
|
|
38
|
+
).split(' '),
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
// `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`. Mermaid itself can't accept hex
|
|
42
|
+
// here (upstream's lexer treats `#` as a comment); this parser has no such
|
|
43
|
+
// comment syntax, so hex is accepted as a strict superset.
|
|
44
|
+
const HEX_COLOR_RE = /^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i
|
|
45
|
+
// `rgb(33,66,99)`, `rgba(33,66,99,0.5)`, `hsl(10, 40%, 90%)`, `hsla(...)`,
|
|
46
|
+
// including the modern space-separated / slash-alpha forms. The argument
|
|
47
|
+
// character class is the safety boundary: digits, `.`, `%`, `,`, `/`, and
|
|
48
|
+
// whitespace only.
|
|
49
|
+
const FUNCTION_COLOR_RE =
|
|
50
|
+
/^(?:rgba?|hsla?)\(\s*[\d.%]+(?:[\s,/]+[\d.%]+){2,3}\s*\)$/i
|
|
51
|
+
|
|
52
|
+
/** Whether `value` is a colour this renderer will emit as-is (case-insensitive). */
|
|
53
|
+
export function isCssColor(value: string): boolean {
|
|
54
|
+
return (
|
|
55
|
+
CSS_NAMED_COLORS.has(value.toLowerCase()) ||
|
|
56
|
+
HEX_COLOR_RE.test(value) ||
|
|
57
|
+
FUNCTION_COLOR_RE.test(value)
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Split the text after the `box` keyword into a colour and a label, per
|
|
63
|
+
* Mermaid's `parseBoxData`: the leading word (or function call) is the
|
|
64
|
+
* colour if it is one, otherwise the whole header is the label. An explicit
|
|
65
|
+
* `transparent` is consumed as "no colour" so `box transparent Aqua` yields
|
|
66
|
+
* a colourless box labelled `Aqua`, exactly as upstream documents.
|
|
67
|
+
*/
|
|
68
|
+
export function parseBoxHeader(header: string): {
|
|
69
|
+
color?: string
|
|
70
|
+
label: string
|
|
71
|
+
} {
|
|
72
|
+
const trimmed = header.trim()
|
|
73
|
+
const match = trimmed.match(
|
|
74
|
+
/^((?:rgba?|hsla?)\s*\([^)]*\)|#[0-9a-fA-F]+|\S+)?(.*)$/,
|
|
75
|
+
)
|
|
76
|
+
const candidate = match?.[1] ?? ''
|
|
77
|
+
const rest = match?.[2]?.trim() ?? ''
|
|
78
|
+
if (candidate.toLowerCase() === 'transparent') {
|
|
79
|
+
return { label: rest }
|
|
80
|
+
}
|
|
81
|
+
if (candidate !== '' && isCssColor(candidate)) {
|
|
82
|
+
return { color: candidate, label: rest }
|
|
83
|
+
}
|
|
84
|
+
return { label: trimmed }
|
|
85
|
+
}
|