@@ -53,27 +53,89 @@ function isRecord(value: unknown): value is Record<string, unknown> { |
53 | 53 | return typeof value === "object" && value !== null && !Array.isArray(value); |
54 | 54 | } |
55 | 55 |
|
56 | | -/** The `id@{ ... }` shape data blocks, as mermaid's lexer ends them: at the first `}` outside a "string". */ |
| 56 | +/** |
| 57 | + * Every `@{` with its block up to the first `}` outside a "string", read on its own: lexer state decides |
| 58 | + * whether a `@{` is shape data or text, so blocks may overlap. Blocks without `img` or `\` are skipped. |
| 59 | + */ |
57 | 60 | function shapeDataBlocks(source: string): { start: number; end: number; body: string }[] { |
58 | 61 | const blocks: { start: number; end: number; body: string }[] = []; |
59 | | - let from = 0; |
60 | | - for (;;) { |
61 | | - const open = source.indexOf("@{", from); |
62 | | - if (open < 0) return blocks; |
63 | | - let inString = false; |
64 | | - let close = -1; |
65 | | - for (let index = open + 2; index < source.length; index++) { |
66 | | - const char = source[index]; |
67 | | - if (char === '"') inString = !inString; |
68 | | - else if (char === "}" && !inString) { |
69 | | - close = index; |
70 | | - break; |
| 62 | + if (!source.includes("@{")) return blocks; |
| 63 | + // First `}` outside a string from i, starting outside (closeFrom) or inside (quotedFrom) one: linear. |
| 64 | + const closeFrom = new Int32Array(source.length + 1).fill(-1); |
| 65 | + const quotedFrom = new Int32Array(source.length + 1).fill(-1); |
| 66 | + for (let index = source.length - 1; index >= 0; index--) { |
| 67 | + const char = source[index]; |
| 68 | + if (char === '"') { |
| 69 | + closeFrom[index] = quotedFrom[index + 1]; |
| 70 | + quotedFrom[index] = closeFrom[index + 1]; |
| 71 | + } else { |
| 72 | + closeFrom[index] = char === "}" ? index : closeFrom[index + 1]; |
| 73 | + quotedFrom[index] = quotedFrom[index + 1]; |
| 74 | + } |
| 75 | + } |
| 76 | + const hintEnd = new Int32Array(source.length + 1).fill(-1); |
| 77 | + for (let index = source.length - 1; index >= 0; index--) { |
| 78 | + if (source[index] === "\\") hintEnd[index] = index + 1; |
| 79 | + else if (source.slice(index, index + 3).toLowerCase() === "img") hintEnd[index] = index + 3; |
| 80 | + else hintEnd[index] = hintEnd[index + 1]; |
| 81 | + } |
| 82 | + for (let open = source.indexOf("@{"); open >= 0; open = source.indexOf("@{", open + 2)) { |
| 83 | + const close = closeFrom[open + 2]; |
| 84 | + const hint = hintEnd[open + 2]; |
| 85 | + if (close >= 0 && hint >= 0 && hint <= close) { |
| 86 | + blocks.push({ start: open, end: close + 1, body: source.slice(open + 2, close) }); |
| 87 | + } |
| 88 | + } |
| 89 | + return blocks; |
| 90 | +} |
| 91 | + |
| 92 | +// Mermaid's own entity for `@`: it shows as `@` in text and never opens shape data. |
| 93 | +const INERT_OPENER = "#64;{"; |
| 94 | + |
| 95 | +// Overlapping blocks can sum to the source's square; past this every `@{` is made inert unparsed. |
| 96 | +const PARSE_BUDGET = 1 << 20; |
| 97 | + |
| 98 | +/** |
| 99 | + * Remote images taken out of every block: a lone block is rewritten, a run of overlapping ones has all |
| 100 | + * its openers made inert (which reading wins depends on lexer state). Repeats until stable. |
| 101 | + */ |
| 102 | +function withoutRemoteShapeImages(source: string): string { |
| 103 | + let current = source; |
| 104 | + let budget = PARSE_BUDGET; |
| 105 | + for (let pass = 0; pass < 8; pass++) { |
| 106 | + // Comments are blanked to the same length, so a `@{` or `"` in one cannot hide a later block. |
| 107 | + const scanned = current.replace(COMMENT_LINE, (line) => " ".repeat(line.length)); |
| 108 | + const fetching: { start: number; end: number; replaced: string }[] = []; |
| 109 | + for (const block of shapeDataBlocks(scanned)) { |
| 110 | + budget -= block.body.length; |
| 111 | + if (budget < 0) return current.replaceAll("@{", INERT_OPENER); |
| 112 | + const replaced = withoutRemoteImage(block.body); |
| 113 | + if (replaced !== null) fetching.push({ start: block.start, end: block.end, replaced }); |
| 114 | + } |
| 115 | + if (fetching.length === 0) return current; |
| 116 | + let out = ""; |
| 117 | + let at = 0; |
| 118 | + for (let first = 0; first < fetching.length; ) { |
| 119 | + let last = first; |
| 120 | + let runEnd = fetching[first].end; |
| 121 | + while (last + 1 < fetching.length && fetching[last + 1].start < runEnd) { |
| 122 | + last++; |
| 123 | + runEnd = Math.max(runEnd, fetching[last].end); |
71 | 124 | } |
| 125 | + if (last === first) { |
| 126 | + out += current.slice(at, fetching[first].start) + fetching[first].replaced; |
| 127 | + at = fetching[first].end; |
| 128 | + } else { |
| 129 | + for (let index = first; index <= last; index++) { |
| 130 | + out += current.slice(at, fetching[index].start) + INERT_OPENER; |
| 131 | + at = fetching[index].start + 2; |
| 132 | + } |
| 133 | + } |
| 134 | + first = last + 1; |
72 | 135 | } |
73 | | - if (close < 0) return blocks; |
74 | | - blocks.push({ start: open, end: close + 1, body: source.slice(open + 2, close) }); |
75 | | - from = close + 1; |
| 136 | + current = out + current.slice(at); |
76 | 137 | } |
| 138 | + return current.replaceAll("@{", INERT_OPENER); |
77 | 139 | } |
78 | 140 |
|
79 | 141 | /** A shape data block without a remote `img`, or null when it carries none and stays as written. */ |
@@ -132,17 +194,8 @@ function withoutFetchingStyle(line: string, midLine: boolean): string { |
132 | 194 |
|
133 | 195 | /** The diagram source with every image or style that would load from elsewhere taken out. */ |
134 | 196 | export function stripRemoteDiagramImages(source: string): string { |
135 | | - let out = ""; |
136 | | - let at = 0; |
137 | | - // Comments are blanked to the same length, so a `@{` or `"` in one cannot hide a later block. |
| 197 | + let out = withoutRemoteShapeImages(source); |
138 | 198 | const scanned = source.replace(COMMENT_LINE, (line) => " ".repeat(line.length)); |
139 | | - for (const block of shapeDataBlocks(scanned)) { |
140 | | - const replaced = withoutRemoteImage(block.body); |
141 | | - if (replaced === null) continue; |
142 | | - out += source.slice(at, block.start) + replaced; |
143 | | - at = block.end; |
144 | | - } |
145 | | - out += source.slice(at); |
146 | 199 | if (SEQUENCE.test(scanned)) out = out.replace(DETAILS_LINE, ""); |
147 | 200 | // Mermaid reads the diagram type after dropping directives: `%%{init}%%stateDiagram` counts. |
148 | 201 | const midLine = MIDLINE_STYLE.test(scanned.replace(DIRECTIVE, "")); |
|