nerdgram1337 1.4.6 → 1.4.7
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/dist/chunk-54BVSMKR.js +87 -0
- package/dist/chunk-5C6VO4LC.js +90 -0
- package/dist/{chunk-KO565WWM.js → chunk-AFGGIQ22.js} +252 -80
- package/dist/chunk-ECAEFMIC.js +956 -0
- package/dist/{chunk-BOMYBBJ6.js → chunk-GQKUFF4P.js} +8 -102
- package/dist/chunk-IQ4Z2ZHX.js +1494 -0
- package/dist/chunk-KAKG6DWJ.js +943 -0
- package/dist/chunk-ULY5YEBR.js +490 -0
- package/dist/{chunk-EGA2UGTF.js → chunk-XJTF2GUE.js} +102 -104
- package/dist/chunk-ZVKNTVWH.js +492 -0
- package/dist/cli.js +46 -3
- package/package.json +1 -1
|
@@ -0,0 +1,943 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildTree,
|
|
3
|
+
readFile
|
|
4
|
+
} from "./chunk-IQ4Z2ZHX.js";
|
|
5
|
+
import {
|
|
6
|
+
atomicWriteJson,
|
|
7
|
+
getDataRoot,
|
|
8
|
+
getMetaPath,
|
|
9
|
+
httpFail,
|
|
10
|
+
readJsonFile,
|
|
11
|
+
rewriteMovedPath
|
|
12
|
+
} from "./chunk-ULY5YEBR.js";
|
|
13
|
+
import "./chunk-5C6VO4LC.js";
|
|
14
|
+
import {
|
|
15
|
+
literalLocator,
|
|
16
|
+
matchDiagramSearch,
|
|
17
|
+
parseDiagram,
|
|
18
|
+
searchMatchLabel
|
|
19
|
+
} from "./chunk-AFGGIQ22.js";
|
|
20
|
+
|
|
21
|
+
// server/src/search.ts
|
|
22
|
+
import path from "node:path";
|
|
23
|
+
|
|
24
|
+
// server/src/textElements.ts
|
|
25
|
+
var MAX_EXTRACT_CHARS = 512 * 1024;
|
|
26
|
+
var MAX_ELEMENTS = 800;
|
|
27
|
+
var MAX_ELEMENT_CHARS = 400;
|
|
28
|
+
var MAX_LINE_CHARS = 4e3;
|
|
29
|
+
var SYNTAX = /* @__PURE__ */ new Set([
|
|
30
|
+
"flowchart",
|
|
31
|
+
"graph",
|
|
32
|
+
"td",
|
|
33
|
+
"lr",
|
|
34
|
+
"tb",
|
|
35
|
+
"rl",
|
|
36
|
+
"bt",
|
|
37
|
+
"sequencediagram",
|
|
38
|
+
"classdiagram",
|
|
39
|
+
"statediagram",
|
|
40
|
+
"statediagram-v2",
|
|
41
|
+
"erdiagram",
|
|
42
|
+
"journey",
|
|
43
|
+
"gantt",
|
|
44
|
+
"pie",
|
|
45
|
+
"mindmap",
|
|
46
|
+
"timeline",
|
|
47
|
+
"gitgraph",
|
|
48
|
+
"quadrantchart",
|
|
49
|
+
"xychart-beta",
|
|
50
|
+
"block-beta",
|
|
51
|
+
"subgraph",
|
|
52
|
+
"end",
|
|
53
|
+
"participant",
|
|
54
|
+
"actor",
|
|
55
|
+
"note",
|
|
56
|
+
"style",
|
|
57
|
+
"classdef",
|
|
58
|
+
"class",
|
|
59
|
+
"click",
|
|
60
|
+
"direction",
|
|
61
|
+
"linkstyle",
|
|
62
|
+
"left",
|
|
63
|
+
"right",
|
|
64
|
+
"over",
|
|
65
|
+
"of",
|
|
66
|
+
"as",
|
|
67
|
+
"and",
|
|
68
|
+
"loop",
|
|
69
|
+
"alt",
|
|
70
|
+
"else",
|
|
71
|
+
"opt",
|
|
72
|
+
"par",
|
|
73
|
+
"critical",
|
|
74
|
+
"break",
|
|
75
|
+
"rect",
|
|
76
|
+
"autonumber",
|
|
77
|
+
"activate",
|
|
78
|
+
"deactivate",
|
|
79
|
+
"title",
|
|
80
|
+
"section",
|
|
81
|
+
"dateformat",
|
|
82
|
+
"axisformat",
|
|
83
|
+
"excludes",
|
|
84
|
+
"includes",
|
|
85
|
+
"todaymarker",
|
|
86
|
+
"showdata"
|
|
87
|
+
]);
|
|
88
|
+
var OPENERS = [
|
|
89
|
+
{ open: "([", close: "])" },
|
|
90
|
+
{ open: "[[", close: "]]" },
|
|
91
|
+
{ open: "[(", close: ")]" },
|
|
92
|
+
{ open: "[/", close: "/]" },
|
|
93
|
+
{ open: "[\\", close: "\\]" },
|
|
94
|
+
{ open: "((", close: "))" },
|
|
95
|
+
{ open: "{{", close: "}}" },
|
|
96
|
+
{ open: "[", close: "]" },
|
|
97
|
+
{ open: "(", close: ")" },
|
|
98
|
+
{ open: "{", close: "}" },
|
|
99
|
+
{ open: ">", close: "]" }
|
|
100
|
+
];
|
|
101
|
+
function unquote(value) {
|
|
102
|
+
const trimmed = value.trim();
|
|
103
|
+
if (trimmed.length < 2) return trimmed;
|
|
104
|
+
const open = trimmed[0];
|
|
105
|
+
const close = trimmed[trimmed.length - 1];
|
|
106
|
+
if ((open === '"' || open === "'" || open === "`") && close === open) {
|
|
107
|
+
return trimmed.slice(1, -1).trim();
|
|
108
|
+
}
|
|
109
|
+
return trimmed;
|
|
110
|
+
}
|
|
111
|
+
function isSyntaxOnly(value) {
|
|
112
|
+
return SYNTAX.has(value.trim().toLowerCase());
|
|
113
|
+
}
|
|
114
|
+
function clip(value) {
|
|
115
|
+
const cleaned = value.replace(/\s+/g, " ").trim();
|
|
116
|
+
if (cleaned.length <= MAX_ELEMENT_CHARS) return cleaned;
|
|
117
|
+
return cleaned.slice(0, MAX_ELEMENT_CHARS);
|
|
118
|
+
}
|
|
119
|
+
function lineOfBody(source, doc) {
|
|
120
|
+
if (!doc.hasFrontMatter) return 1;
|
|
121
|
+
const index = source.indexOf(doc.body);
|
|
122
|
+
if (index <= 0) return 1;
|
|
123
|
+
let line = 1;
|
|
124
|
+
for (let i = 0; i < index; i += 1) {
|
|
125
|
+
if (source[i] === "\n") line += 1;
|
|
126
|
+
}
|
|
127
|
+
return line;
|
|
128
|
+
}
|
|
129
|
+
function detectType(line) {
|
|
130
|
+
const match = /^\s*(flowchart|graph|sequenceDiagram|classDiagram|stateDiagram(?:-v2)?|erDiagram|journey|gantt|pie|mindmap|timeline|gitGraph|quadrantChart|xychart(?:-beta)?|block(?:-beta)?)\b/i.exec(
|
|
131
|
+
line
|
|
132
|
+
);
|
|
133
|
+
if (!match) return null;
|
|
134
|
+
const word = match[1].toLowerCase();
|
|
135
|
+
if (word === "flowchart" || word === "graph") return "flowchart";
|
|
136
|
+
if (word === "sequencediagram") return "sequence";
|
|
137
|
+
if (word === "classdiagram") return "class";
|
|
138
|
+
if (word.startsWith("statediagram")) return "state";
|
|
139
|
+
if (word === "erdiagram") return "er";
|
|
140
|
+
if (word === "journey") return "journey";
|
|
141
|
+
if (word === "gantt") return "gantt";
|
|
142
|
+
if (word === "pie") return "pie";
|
|
143
|
+
if (word === "mindmap") return "mindmap";
|
|
144
|
+
if (word === "timeline") return "timeline";
|
|
145
|
+
if (word === "gitgraph") return "git";
|
|
146
|
+
if (word === "quadrantchart") return "quadrant";
|
|
147
|
+
if (word.startsWith("xychart")) return "xy";
|
|
148
|
+
if (word.startsWith("block")) return "block";
|
|
149
|
+
return "unknown";
|
|
150
|
+
}
|
|
151
|
+
function findClose(line, from, close) {
|
|
152
|
+
let quote = null;
|
|
153
|
+
for (let i = from; i < line.length; i += 1) {
|
|
154
|
+
const ch = line[i] ?? "";
|
|
155
|
+
if (quote) {
|
|
156
|
+
if (ch === quote) quote = null;
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (ch === '"' || ch === "'" || ch === "`") {
|
|
160
|
+
quote = ch;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (line.startsWith(close, i)) return i;
|
|
164
|
+
}
|
|
165
|
+
return -1;
|
|
166
|
+
}
|
|
167
|
+
function readShapes(line) {
|
|
168
|
+
const found = [];
|
|
169
|
+
let i = 0;
|
|
170
|
+
while (i < line.length) {
|
|
171
|
+
const idMatch = /^[A-Za-z_][\w-]*/.exec(line.slice(i));
|
|
172
|
+
if (!idMatch || i > 0 && /[\w-]/.test(line[i - 1] ?? "")) {
|
|
173
|
+
i += 1;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
const id = idMatch[0];
|
|
177
|
+
const cursor = i + id.length;
|
|
178
|
+
const opener = OPENERS.find((item) => line.startsWith(item.open, cursor));
|
|
179
|
+
if (!opener) {
|
|
180
|
+
i += id.length;
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
const contentStart = cursor + opener.open.length;
|
|
184
|
+
const closeAt = findClose(line, contentStart, opener.close);
|
|
185
|
+
if (closeAt < 0) {
|
|
186
|
+
i += id.length;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
found.push({
|
|
190
|
+
id,
|
|
191
|
+
label: unquote(line.slice(contentStart, closeAt)),
|
|
192
|
+
start: i,
|
|
193
|
+
end: closeAt + opener.close.length
|
|
194
|
+
});
|
|
195
|
+
i = closeAt + opener.close.length;
|
|
196
|
+
}
|
|
197
|
+
return found;
|
|
198
|
+
}
|
|
199
|
+
function blankSpans(line, spans) {
|
|
200
|
+
const chars = [...line];
|
|
201
|
+
for (const span of spans) {
|
|
202
|
+
for (let i = span.start; i < span.end && i < chars.length; i += 1) chars[i] = " ";
|
|
203
|
+
}
|
|
204
|
+
return chars.join("");
|
|
205
|
+
}
|
|
206
|
+
function takePipeLabels(line, lineNo, push) {
|
|
207
|
+
let out = "";
|
|
208
|
+
let i = 0;
|
|
209
|
+
while (i < line.length) {
|
|
210
|
+
if (line[i] !== "|") {
|
|
211
|
+
out += line[i];
|
|
212
|
+
i += 1;
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
const end = line.indexOf("|", i + 1);
|
|
216
|
+
if (end < 0) {
|
|
217
|
+
out += line.slice(i);
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
push("edge", line.slice(i + 1, end), lineNo);
|
|
221
|
+
out += " ";
|
|
222
|
+
i = end + 1;
|
|
223
|
+
}
|
|
224
|
+
return out;
|
|
225
|
+
}
|
|
226
|
+
function takeDashLabels(line, lineNo, push) {
|
|
227
|
+
let out = "";
|
|
228
|
+
let i = 0;
|
|
229
|
+
while (i < line.length) {
|
|
230
|
+
const dash = /^(?:--+|==+|\.\-+)/.exec(line.slice(i));
|
|
231
|
+
if (!dash) {
|
|
232
|
+
out += line[i];
|
|
233
|
+
i += 1;
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
const after = i + dash[0].length;
|
|
237
|
+
const labeled = /^[ \t]+([^>|=\n][^>|=\n]{0,200}?)[ \t]+(?:--+>?|==+>|\.\-+>)/.exec(
|
|
238
|
+
line.slice(after)
|
|
239
|
+
);
|
|
240
|
+
const label = labeled?.[1]?.trim() ?? "";
|
|
241
|
+
if (labeled && label && !/^[-.=<>]+$/.test(label) && !isSyntaxOnly(label)) {
|
|
242
|
+
push("edge", label, lineNo);
|
|
243
|
+
i = after + labeled[0].length;
|
|
244
|
+
out += " ";
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
out += dash[0];
|
|
248
|
+
i = after;
|
|
249
|
+
}
|
|
250
|
+
return out;
|
|
251
|
+
}
|
|
252
|
+
function pushBareIds(line, lineNo, labeled, push, kind) {
|
|
253
|
+
const stripped = line.replace(/<?(?:--+|==+|\.\-+|-\.-)(?:>|o|x)?/g, " ");
|
|
254
|
+
for (const token of stripped.split(/[\s&;,]+/)) {
|
|
255
|
+
const id = token.trim();
|
|
256
|
+
if (!/^[A-Za-z_][\w.-]*$/.test(id)) continue;
|
|
257
|
+
if (labeled.has(id) || isSyntaxOnly(id)) continue;
|
|
258
|
+
push(kind, id, lineNo);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
function subgraphTitle(rest) {
|
|
262
|
+
const trimmed = rest.trim();
|
|
263
|
+
const shaped = /^[A-Za-z_][\w-]*\s*\[([\s\S]*)\]$/.exec(trimmed);
|
|
264
|
+
if (shaped) return unquote(shaped[1] ?? "");
|
|
265
|
+
return unquote(trimmed);
|
|
266
|
+
}
|
|
267
|
+
function extractFlowLine(line, lineNo, labeled, push) {
|
|
268
|
+
if (/^\s*end\s*$/i.test(line)) return true;
|
|
269
|
+
if (/^(?:style|classDef|linkStyle|click|direction|interpolate)\b/i.test(line)) return true;
|
|
270
|
+
const sub = /^\s*subgraph\b(.*)$/i.exec(line);
|
|
271
|
+
if (sub) {
|
|
272
|
+
push("subgraph", subgraphTitle(sub[1] ?? ""), lineNo);
|
|
273
|
+
return true;
|
|
274
|
+
}
|
|
275
|
+
if (/^\s*class\b/i.test(line)) return true;
|
|
276
|
+
const shapes = readShapes(line);
|
|
277
|
+
for (const shape of shapes) {
|
|
278
|
+
labeled.add(shape.id);
|
|
279
|
+
push("node", shape.label || shape.id, lineNo);
|
|
280
|
+
}
|
|
281
|
+
let rest = blankSpans(line, shapes);
|
|
282
|
+
rest = takePipeLabels(rest, lineNo, push);
|
|
283
|
+
rest = takeDashLabels(rest, lineNo, push);
|
|
284
|
+
pushBareIds(rest, lineNo, labeled, push, "node");
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
function extractSequenceLine(line, lineNo, push) {
|
|
288
|
+
const trimmed = line.trim();
|
|
289
|
+
const declared = /^(?:participant|actor)\s+(\S+)(?:\s+as\s+(.+))?$/i.exec(trimmed);
|
|
290
|
+
if (declared) {
|
|
291
|
+
const alias = declared[2]?.trim();
|
|
292
|
+
push("participant", alias ? unquote(alias) : declared[1] ?? "", lineNo);
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
if (/^(?:autonumber|activate|deactivate)\b/i.test(trimmed) || /^end\s*$/i.test(trimmed)) return true;
|
|
296
|
+
if (/^rect\b/i.test(trimmed)) return true;
|
|
297
|
+
const note = /^note\b[^:]*:\s*(.+)$/i.exec(trimmed);
|
|
298
|
+
if (note) {
|
|
299
|
+
push("note", note[1] ?? "", lineNo);
|
|
300
|
+
return true;
|
|
301
|
+
}
|
|
302
|
+
const block = /^(?:loop|alt|else|opt|par|critical|break)\b(?:\s+(.+))?$/i.exec(trimmed);
|
|
303
|
+
if (block) {
|
|
304
|
+
if (block[1]?.trim()) push("text", block[1], lineNo);
|
|
305
|
+
return true;
|
|
306
|
+
}
|
|
307
|
+
const colon = trimmed.lastIndexOf(":");
|
|
308
|
+
if (colon > 0) {
|
|
309
|
+
const left = trimmed.slice(0, colon);
|
|
310
|
+
if (/--+|>>|->/.test(left)) {
|
|
311
|
+
push("message", trimmed.slice(colon + 1), lineNo);
|
|
312
|
+
const names = left.split(/<?--+>?|--+>>|->>|-->>|-->|->|-\)|--x|\s+/);
|
|
313
|
+
for (const name of names) {
|
|
314
|
+
const id = name.trim();
|
|
315
|
+
if (!/^[\w.]+$/.test(id) || isSyntaxOnly(id)) continue;
|
|
316
|
+
push("participant", id, lineNo);
|
|
317
|
+
}
|
|
318
|
+
return true;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
function extractClassLine(line, lineNo, push, state) {
|
|
324
|
+
const trimmed = line.trim();
|
|
325
|
+
if (trimmed === "{") {
|
|
326
|
+
state.inBlock = true;
|
|
327
|
+
return true;
|
|
328
|
+
}
|
|
329
|
+
if (trimmed === "}" || trimmed === "}") {
|
|
330
|
+
state.inBlock = false;
|
|
331
|
+
return true;
|
|
332
|
+
}
|
|
333
|
+
const cls = /^class\s+([A-Za-z_][\w]*)(?:\s*\[\s*([^\]]+)\])?/.exec(trimmed);
|
|
334
|
+
if (cls) {
|
|
335
|
+
push("class", unquote((cls[2] || cls[1] || "").trim()), lineNo);
|
|
336
|
+
if (trimmed.includes("{") && !trimmed.includes("}")) state.inBlock = true;
|
|
337
|
+
if (trimmed.includes("}")) state.inBlock = false;
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
if (state.inBlock) {
|
|
341
|
+
if (trimmed === "}" || trimmed.endsWith("}")) state.inBlock = false;
|
|
342
|
+
const member = trimmed.replace(/^[+\-#~]\s*/, "").replace(/\}\s*$/, "").trim();
|
|
343
|
+
if (member && member !== "}") push("member", member, lineNo);
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
if (/<\|--|o--|<\|\.\.|--\|>|\*--|o\.\.|--/.test(trimmed)) {
|
|
347
|
+
const label = /:\s*(.+)$/.exec(trimmed);
|
|
348
|
+
const left = label ? trimmed.slice(0, label.index) : trimmed;
|
|
349
|
+
if (label?.[1]) push("text", label[1], lineNo);
|
|
350
|
+
for (const name of left.split(/[^A-Za-z0-9_]+/)) {
|
|
351
|
+
if (/^[A-Za-z_][\w]*$/.test(name) && !isSyntaxOnly(name)) push("class", name, lineNo);
|
|
352
|
+
}
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
355
|
+
const annotation = /^([A-Za-z_][\w]*)\s*:\s*(.+)$/.exec(trimmed);
|
|
356
|
+
if (annotation) {
|
|
357
|
+
push("class", annotation[1] ?? "", lineNo);
|
|
358
|
+
push("member", annotation[2] ?? "", lineNo);
|
|
359
|
+
return true;
|
|
360
|
+
}
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
function extractErLine(line, lineNo, push, state) {
|
|
364
|
+
const trimmed = line.trim();
|
|
365
|
+
if (state.inBlock) {
|
|
366
|
+
if (trimmed === "}" || trimmed.startsWith("}")) {
|
|
367
|
+
state.inBlock = false;
|
|
368
|
+
return true;
|
|
369
|
+
}
|
|
370
|
+
const parts = trimmed.split(/\s+/);
|
|
371
|
+
const name = (parts.length >= 2 ? parts[1] : parts[0]) ?? "";
|
|
372
|
+
const attr = name.replace(/[^\w].*$/, "");
|
|
373
|
+
if (attr && !isSyntaxOnly(attr)) push("attribute", attr, lineNo);
|
|
374
|
+
return true;
|
|
375
|
+
}
|
|
376
|
+
if (trimmed.endsWith("{")) {
|
|
377
|
+
const entity = trimmed.slice(0, -1).trim();
|
|
378
|
+
if (entity) push("entity", entity, lineNo);
|
|
379
|
+
state.inBlock = true;
|
|
380
|
+
return true;
|
|
381
|
+
}
|
|
382
|
+
const rel = /^([A-Za-z_][\w]*)\s+\S+\s+([A-Za-z_][\w]*)\s*:\s*(.+)$/.exec(trimmed);
|
|
383
|
+
if (rel) {
|
|
384
|
+
push("entity", rel[1] ?? "", lineNo);
|
|
385
|
+
push("entity", rel[2] ?? "", lineNo);
|
|
386
|
+
push("text", rel[3] ?? "", lineNo);
|
|
387
|
+
return true;
|
|
388
|
+
}
|
|
389
|
+
return false;
|
|
390
|
+
}
|
|
391
|
+
function extractStateLine(line, lineNo, push) {
|
|
392
|
+
const trimmed = line.trim();
|
|
393
|
+
if (/^\[\*\]\s*$/.test(trimmed) || /^direction\b/i.test(trimmed)) return true;
|
|
394
|
+
const named = /^state\s+"([^"]+)"(?:\s+as\s+\S+)?/i.exec(trimmed);
|
|
395
|
+
if (named) {
|
|
396
|
+
push("state", named[1] ?? "", lineNo);
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
const note = /^note\b[^:]*:\s*(.+)$/i.exec(trimmed);
|
|
400
|
+
if (note) {
|
|
401
|
+
push("note", note[1] ?? "", lineNo);
|
|
402
|
+
return true;
|
|
403
|
+
}
|
|
404
|
+
if (/^state\s+([A-Za-z_][\w]*)\s*\{/.test(trimmed)) {
|
|
405
|
+
const id = /^state\s+([A-Za-z_][\w]*)/.exec(trimmed);
|
|
406
|
+
if (id?.[1]) push("state", id[1], lineNo);
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
if (trimmed === "}" || trimmed === "--" || trimmed === "end") return true;
|
|
410
|
+
const colon = trimmed.indexOf(":");
|
|
411
|
+
if (colon > 0 && /-->/.test(trimmed.slice(0, colon))) {
|
|
412
|
+
push("edge", trimmed.slice(colon + 1), lineNo);
|
|
413
|
+
} else if (colon > 0) {
|
|
414
|
+
push("text", trimmed.slice(colon + 1), lineNo);
|
|
415
|
+
}
|
|
416
|
+
const without = trimmed.replace(/\[\*\]/g, " ").replace(/-->/g, " ").replace(/:/g, " ");
|
|
417
|
+
let any = false;
|
|
418
|
+
for (const token of without.split(/\s+/)) {
|
|
419
|
+
if (/^[A-Za-z_][\w]*$/.test(token) && !isSyntaxOnly(token)) {
|
|
420
|
+
push("state", token, lineNo);
|
|
421
|
+
any = true;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
return any || colon > 0;
|
|
425
|
+
}
|
|
426
|
+
function extractGanttLine(line, lineNo, push) {
|
|
427
|
+
const trimmed = line.trim();
|
|
428
|
+
if (/^(?:dateFormat|axisFormat|excludes|includes|todayMarker)\b/i.test(trimmed)) return true;
|
|
429
|
+
const section = /^section\s+(.+)$/i.exec(trimmed);
|
|
430
|
+
if (section) {
|
|
431
|
+
push("section", section[1] ?? "", lineNo);
|
|
432
|
+
return true;
|
|
433
|
+
}
|
|
434
|
+
const task = /^([^:]+):/.exec(trimmed);
|
|
435
|
+
if (task && !/^(?:title|section)$/i.test((task[1] ?? "").trim())) {
|
|
436
|
+
push("task", task[1] ?? "", lineNo);
|
|
437
|
+
return true;
|
|
438
|
+
}
|
|
439
|
+
return false;
|
|
440
|
+
}
|
|
441
|
+
function extractPieLine(line, lineNo, push) {
|
|
442
|
+
const trimmed = line.trim();
|
|
443
|
+
if (/^showData\b/i.test(trimmed) || /^pie\b/i.test(trimmed)) return true;
|
|
444
|
+
const quoted = /^"([^"]+)"\s*:/.exec(trimmed);
|
|
445
|
+
if (quoted) {
|
|
446
|
+
push("slice", quoted[1] ?? "", lineNo);
|
|
447
|
+
return true;
|
|
448
|
+
}
|
|
449
|
+
const plain = /^([^:]+)\s*:\s*\d/.exec(trimmed);
|
|
450
|
+
if (plain) {
|
|
451
|
+
push("slice", plain[1] ?? "", lineNo);
|
|
452
|
+
return true;
|
|
453
|
+
}
|
|
454
|
+
return false;
|
|
455
|
+
}
|
|
456
|
+
function extractMindmapLine(line, lineNo, push) {
|
|
457
|
+
const trimmed = line.trim().replace(/::icon\([^)]*\)/g, "").trim();
|
|
458
|
+
if (!trimmed) return true;
|
|
459
|
+
const shapes = readShapes(trimmed);
|
|
460
|
+
if (shapes.length > 0) {
|
|
461
|
+
for (const shape of shapes) push("node", shape.label || shape.id, lineNo);
|
|
462
|
+
return true;
|
|
463
|
+
}
|
|
464
|
+
if (!isSyntaxOnly(trimmed)) push("node", trimmed, lineNo);
|
|
465
|
+
return true;
|
|
466
|
+
}
|
|
467
|
+
function extractJourneyLine(line, lineNo, push) {
|
|
468
|
+
const trimmed = line.trim();
|
|
469
|
+
const section = /^section\s+(.+)$/i.exec(trimmed);
|
|
470
|
+
if (section) {
|
|
471
|
+
push("section", section[1] ?? "", lineNo);
|
|
472
|
+
return true;
|
|
473
|
+
}
|
|
474
|
+
const parts = trimmed.split(":");
|
|
475
|
+
if (parts.length >= 2) {
|
|
476
|
+
push("task", parts[0] ?? "", lineNo);
|
|
477
|
+
const actor = parts[parts.length - 1]?.trim() ?? "";
|
|
478
|
+
if (actor && !/^\d+(\.\d+)?$/.test(actor)) push("text", actor, lineNo);
|
|
479
|
+
return true;
|
|
480
|
+
}
|
|
481
|
+
return false;
|
|
482
|
+
}
|
|
483
|
+
function extractTimelineLine(line, lineNo, push) {
|
|
484
|
+
const trimmed = line.trim();
|
|
485
|
+
const section = /^section\s+(.+)$/i.exec(trimmed);
|
|
486
|
+
if (section) {
|
|
487
|
+
push("section", section[1] ?? "", lineNo);
|
|
488
|
+
return true;
|
|
489
|
+
}
|
|
490
|
+
const parts = trimmed.split(":");
|
|
491
|
+
if (parts.length >= 2) {
|
|
492
|
+
push("text", parts[0] ?? "", lineNo);
|
|
493
|
+
push("text", parts.slice(1).join(":"), lineNo);
|
|
494
|
+
return true;
|
|
495
|
+
}
|
|
496
|
+
if (!isSyntaxOnly(trimmed)) push("text", trimmed, lineNo);
|
|
497
|
+
return true;
|
|
498
|
+
}
|
|
499
|
+
function extractQuadrantLine(line, lineNo, push) {
|
|
500
|
+
const trimmed = line.trim();
|
|
501
|
+
const axis = /^(?:x-axis|y-axis)\s+(.+)$/i.exec(trimmed);
|
|
502
|
+
if (axis) {
|
|
503
|
+
for (const part of (axis[1] ?? "").split("-->")) push("text", part, lineNo);
|
|
504
|
+
return true;
|
|
505
|
+
}
|
|
506
|
+
const point = /^([^:[\]]+):\s*\[/.exec(trimmed);
|
|
507
|
+
if (point) {
|
|
508
|
+
push("text", point[1] ?? "", lineNo);
|
|
509
|
+
return true;
|
|
510
|
+
}
|
|
511
|
+
return false;
|
|
512
|
+
}
|
|
513
|
+
function extractBlockLine(line, lineNo, labeled, push) {
|
|
514
|
+
if (/^(?:columns|block-beta)\b/i.test(line.trim())) return true;
|
|
515
|
+
return extractFlowLine(line, lineNo, labeled, push);
|
|
516
|
+
}
|
|
517
|
+
function extractFallback(line, lineNo, push) {
|
|
518
|
+
const stripped = line.replace(/<[^>\n]{0,80}>/g, " ").replace(/<?(?:--+|==+|\.\-+)(?:>|o|x)?/g, " ").replace(/[|;{}()[\]"]/g, " ");
|
|
519
|
+
const words = stripped.split(/\s+/).map((word) => word.trim()).filter((word) => word.length >= 2 && !isSyntaxOnly(word) && !/^[%#@]/.test(word));
|
|
520
|
+
if (words.length === 0) return;
|
|
521
|
+
push("text", words.join(" "), lineNo);
|
|
522
|
+
}
|
|
523
|
+
function extractTextElements(source, parsed = parseDiagram(source)) {
|
|
524
|
+
const limited = parsed.body.length > MAX_EXTRACT_CHARS ? parsed.body.slice(0, MAX_EXTRACT_CHARS) : parsed.body;
|
|
525
|
+
const startLine = lineOfBody(source, parsed);
|
|
526
|
+
const lines = limited.split(/\r?\n/);
|
|
527
|
+
const elements = [];
|
|
528
|
+
const seen = /* @__PURE__ */ new Set();
|
|
529
|
+
const labeled = /* @__PURE__ */ new Set();
|
|
530
|
+
const block = { inBlock: false };
|
|
531
|
+
const push = (kind, text, line) => {
|
|
532
|
+
if (elements.length >= MAX_ELEMENTS) return;
|
|
533
|
+
const value = clip(text);
|
|
534
|
+
if (!value) return;
|
|
535
|
+
const key = `${kind}|${line}|${value.toLowerCase()}`;
|
|
536
|
+
if (seen.has(key)) return;
|
|
537
|
+
seen.add(key);
|
|
538
|
+
elements.push({ kind, text: value, line });
|
|
539
|
+
};
|
|
540
|
+
let type = "unknown";
|
|
541
|
+
let inAccDescr = false;
|
|
542
|
+
let accDescr = [];
|
|
543
|
+
let accDescrLine = 0;
|
|
544
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
545
|
+
if (elements.length >= MAX_ELEMENTS) break;
|
|
546
|
+
const lineNo = startLine + index;
|
|
547
|
+
const raw = (lines[index] ?? "").slice(0, MAX_LINE_CHARS);
|
|
548
|
+
const trimmed = raw.trim();
|
|
549
|
+
if (!trimmed || trimmed.startsWith("%%")) continue;
|
|
550
|
+
if (inAccDescr) {
|
|
551
|
+
if (trimmed === "}") {
|
|
552
|
+
push("accDescr", accDescr.join(" "), accDescrLine);
|
|
553
|
+
inAccDescr = false;
|
|
554
|
+
accDescr = [];
|
|
555
|
+
} else {
|
|
556
|
+
accDescr.push(trimmed.replace(/}\s*$/, ""));
|
|
557
|
+
if (trimmed.endsWith("}")) {
|
|
558
|
+
push("accDescr", accDescr.join(" "), accDescrLine);
|
|
559
|
+
inAccDescr = false;
|
|
560
|
+
accDescr = [];
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
continue;
|
|
564
|
+
}
|
|
565
|
+
const accTitle = /^\s*accTitle\s*:\s*(.*)$/i.exec(raw);
|
|
566
|
+
if (accTitle) {
|
|
567
|
+
push("accTitle", accTitle[1] ?? "", lineNo);
|
|
568
|
+
continue;
|
|
569
|
+
}
|
|
570
|
+
const accDescrOne = /^\s*accDescr\s*:\s*(.*)$/i.exec(raw);
|
|
571
|
+
if (accDescrOne) {
|
|
572
|
+
push("accDescr", accDescrOne[1] ?? "", lineNo);
|
|
573
|
+
continue;
|
|
574
|
+
}
|
|
575
|
+
if (/^\s*accDescr\s*\{/i.test(raw)) {
|
|
576
|
+
inAccDescr = true;
|
|
577
|
+
accDescrLine = lineNo;
|
|
578
|
+
const rest = raw.replace(/^\s*accDescr\s*\{/i, "").trim();
|
|
579
|
+
if (rest && rest !== "}") accDescr.push(rest.replace(/}\s*$/, ""));
|
|
580
|
+
if (raw.includes("}")) {
|
|
581
|
+
push("accDescr", accDescr.join(" "), lineNo);
|
|
582
|
+
inAccDescr = false;
|
|
583
|
+
accDescr = [];
|
|
584
|
+
}
|
|
585
|
+
continue;
|
|
586
|
+
}
|
|
587
|
+
if (/^\s*title\s+\S/i.test(trimmed) && !trimmed.includes("-->")) {
|
|
588
|
+
const title = /^\s*title\s+(.+)$/i.exec(trimmed);
|
|
589
|
+
if (title) push("title", unquote(title[1] ?? ""), lineNo);
|
|
590
|
+
continue;
|
|
591
|
+
}
|
|
592
|
+
if (type === "unknown") {
|
|
593
|
+
const detected = detectType(trimmed);
|
|
594
|
+
if (detected) {
|
|
595
|
+
type = detected;
|
|
596
|
+
const pieTitle = /^\s*pie(?:\s+showData)?\s+title\s+(.+)$/i.exec(trimmed);
|
|
597
|
+
if (pieTitle) push("title", unquote(pieTitle[1] ?? ""), lineNo);
|
|
598
|
+
continue;
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
let handled = false;
|
|
602
|
+
if (type === "flowchart" || type === "block") {
|
|
603
|
+
handled = type === "block" ? extractBlockLine(trimmed, lineNo, labeled, push) : extractFlowLine(trimmed, lineNo, labeled, push);
|
|
604
|
+
} else if (type === "sequence") handled = extractSequenceLine(trimmed, lineNo, push);
|
|
605
|
+
else if (type === "class") handled = extractClassLine(trimmed, lineNo, push, block);
|
|
606
|
+
else if (type === "er") handled = extractErLine(trimmed, lineNo, push, block);
|
|
607
|
+
else if (type === "state") handled = extractStateLine(trimmed, lineNo, push);
|
|
608
|
+
else if (type === "gantt") handled = extractGanttLine(trimmed, lineNo, push);
|
|
609
|
+
else if (type === "pie") handled = extractPieLine(trimmed, lineNo, push);
|
|
610
|
+
else if (type === "mindmap") handled = extractMindmapLine(trimmed, lineNo, push);
|
|
611
|
+
else if (type === "journey") handled = extractJourneyLine(trimmed, lineNo, push);
|
|
612
|
+
else if (type === "timeline") handled = extractTimelineLine(trimmed, lineNo, push);
|
|
613
|
+
else if (type === "quadrant" || type === "xy") handled = extractQuadrantLine(trimmed, lineNo, push);
|
|
614
|
+
if (!handled) extractFallback(trimmed, lineNo, push);
|
|
615
|
+
}
|
|
616
|
+
return elements;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// server/src/search.ts
|
|
620
|
+
var MAX_SEARCH_QUERY_CHARS = 200;
|
|
621
|
+
var MAX_SEARCH_REGEX_CHARS = 64;
|
|
622
|
+
var MAX_SEARCH_RESULTS = 40;
|
|
623
|
+
var MAX_SNIPPET_CHARS = 140;
|
|
624
|
+
var INDEX_VERSION = 1;
|
|
625
|
+
var cache = null;
|
|
626
|
+
var chain = Promise.resolve();
|
|
627
|
+
function queued(fn) {
|
|
628
|
+
const run = chain.then(fn, fn);
|
|
629
|
+
chain = run.then(
|
|
630
|
+
() => void 0,
|
|
631
|
+
() => void 0
|
|
632
|
+
);
|
|
633
|
+
return run;
|
|
634
|
+
}
|
|
635
|
+
function indexFile() {
|
|
636
|
+
return path.join(getMetaPath(), "search", "index.json");
|
|
637
|
+
}
|
|
638
|
+
function cacheFor(root) {
|
|
639
|
+
if (!cache || cache.root !== root) {
|
|
640
|
+
cache = { root, entries: /* @__PURE__ */ new Map(), loaded: false, dirty: false };
|
|
641
|
+
}
|
|
642
|
+
return cache;
|
|
643
|
+
}
|
|
644
|
+
function isPublicDiagram(relativePath) {
|
|
645
|
+
if (!relativePath.toLowerCase().endsWith(".mmd")) return false;
|
|
646
|
+
return !relativePath.split("/").some((part) => part.startsWith("."));
|
|
647
|
+
}
|
|
648
|
+
function isEntry(value) {
|
|
649
|
+
if (!value || typeof value !== "object") return false;
|
|
650
|
+
const entry = value;
|
|
651
|
+
return typeof entry.mtimeMs === "number" && typeof entry.size === "number" && Array.isArray(entry.elements) && Array.isArray(entry.tags);
|
|
652
|
+
}
|
|
653
|
+
async function ensureLoaded() {
|
|
654
|
+
const current = cacheFor(getDataRoot());
|
|
655
|
+
if (current.loaded) return current;
|
|
656
|
+
const stored = await readJsonFile(indexFile()).catch(() => null);
|
|
657
|
+
if (stored && stored.version === INDEX_VERSION && stored.entries && typeof stored.entries === "object") {
|
|
658
|
+
for (const [key, value] of Object.entries(stored.entries)) {
|
|
659
|
+
if (!isPublicDiagram(key) || !isEntry(value) || current.entries.has(key)) continue;
|
|
660
|
+
current.entries.set(key, value);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
current.loaded = true;
|
|
664
|
+
return current;
|
|
665
|
+
}
|
|
666
|
+
async function persist(current) {
|
|
667
|
+
if (!current.dirty) return;
|
|
668
|
+
const entries = {};
|
|
669
|
+
for (const [key, value] of current.entries) {
|
|
670
|
+
if (isPublicDiagram(key)) entries[key] = value;
|
|
671
|
+
}
|
|
672
|
+
await atomicWriteJson(indexFile(), { version: INDEX_VERSION, entries });
|
|
673
|
+
current.dirty = false;
|
|
674
|
+
}
|
|
675
|
+
function recordFor(content, mtimeMs, size) {
|
|
676
|
+
const parsed = parseDiagram(content);
|
|
677
|
+
const entry = {
|
|
678
|
+
mtimeMs,
|
|
679
|
+
size,
|
|
680
|
+
title: parsed.title,
|
|
681
|
+
description: parsed.description,
|
|
682
|
+
notes: parsed.notes,
|
|
683
|
+
tags: parsed.tags,
|
|
684
|
+
elements: extractTextElements(content, parsed)
|
|
685
|
+
};
|
|
686
|
+
if (content.length <= 64 * 1024) entry.bloom = gramBloom(content);
|
|
687
|
+
return entry;
|
|
688
|
+
}
|
|
689
|
+
var BLOOM_BYTES = 128;
|
|
690
|
+
function hash32(value) {
|
|
691
|
+
let hash = 2166136261;
|
|
692
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
693
|
+
hash ^= value.charCodeAt(index);
|
|
694
|
+
hash = Math.imul(hash, 16777619);
|
|
695
|
+
}
|
|
696
|
+
return hash >>> 0;
|
|
697
|
+
}
|
|
698
|
+
function gramBloom(content) {
|
|
699
|
+
const bits = Buffer.alloc(BLOOM_BYTES);
|
|
700
|
+
const lower = content.toLowerCase();
|
|
701
|
+
const limit = Math.min(lower.length, 64 * 1024);
|
|
702
|
+
for (let index = 0; index + 3 <= limit; index += 1) {
|
|
703
|
+
const hash = hash32(lower.slice(index, index + 3));
|
|
704
|
+
bits[hash >>> 3 & BLOOM_BYTES - 1] |= 1 << (hash & 7);
|
|
705
|
+
const second = Math.imul(hash, 16777619) >>> 0;
|
|
706
|
+
bits[second >>> 3 & BLOOM_BYTES - 1] |= 1 << (second & 7);
|
|
707
|
+
}
|
|
708
|
+
return bits.toString("base64");
|
|
709
|
+
}
|
|
710
|
+
function bloomMayContain(bloom, needle) {
|
|
711
|
+
if (needle.length < 3) return true;
|
|
712
|
+
const bits = Buffer.from(bloom, "base64");
|
|
713
|
+
if (bits.length !== BLOOM_BYTES) return true;
|
|
714
|
+
const lower = needle.toLowerCase();
|
|
715
|
+
for (let index = 0; index + 3 <= lower.length; index += 1) {
|
|
716
|
+
const hash = hash32(lower.slice(index, index + 3));
|
|
717
|
+
if ((bits[hash >>> 3 & BLOOM_BYTES - 1] & 1 << (hash & 7)) === 0) return false;
|
|
718
|
+
const second = Math.imul(hash, 16777619) >>> 0;
|
|
719
|
+
if ((bits[second >>> 3 & BLOOM_BYTES - 1] & 1 << (second & 7)) === 0) return false;
|
|
720
|
+
}
|
|
721
|
+
return true;
|
|
722
|
+
}
|
|
723
|
+
async function freshIndexedBlooms(files) {
|
|
724
|
+
return queued(async () => {
|
|
725
|
+
const current = await ensureLoaded();
|
|
726
|
+
const found = /* @__PURE__ */ new Map();
|
|
727
|
+
for (const file of files) {
|
|
728
|
+
const entry = current.entries.get(file.path);
|
|
729
|
+
if (!entry || entry.mtimeMs !== file.mtimeMs || entry.size !== file.size) continue;
|
|
730
|
+
if (typeof entry.bloom !== "string") continue;
|
|
731
|
+
found.set(file.path, entry.bloom);
|
|
732
|
+
}
|
|
733
|
+
return found;
|
|
734
|
+
});
|
|
735
|
+
}
|
|
736
|
+
async function rememberSearchEntries(entries) {
|
|
737
|
+
if (entries.length === 0) return;
|
|
738
|
+
await queued(async () => {
|
|
739
|
+
const current = await ensureLoaded();
|
|
740
|
+
let changed = false;
|
|
741
|
+
for (const entry of entries) {
|
|
742
|
+
if (!isPublicDiagram(entry.path)) continue;
|
|
743
|
+
current.entries.set(entry.path, recordFor(entry.content, entry.mtimeMs, entry.size));
|
|
744
|
+
changed = true;
|
|
745
|
+
}
|
|
746
|
+
if (!changed) return;
|
|
747
|
+
current.dirty = true;
|
|
748
|
+
await persist(current);
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
var UNSAFE_NESTED = /(\([^()]*[+*][^()]*\))(?:[+*]|\{\d)/;
|
|
752
|
+
function compileSearchLocator(query, regex) {
|
|
753
|
+
const trimmed = query.trim();
|
|
754
|
+
if (!trimmed) throw httpFail(400, "query is required", "invalid");
|
|
755
|
+
if (trimmed.length > MAX_SEARCH_QUERY_CHARS) {
|
|
756
|
+
throw httpFail(400, "Query is too long", "invalid");
|
|
757
|
+
}
|
|
758
|
+
if (!regex) {
|
|
759
|
+
const locator = literalLocator(trimmed);
|
|
760
|
+
if (!locator) throw httpFail(400, "query is required", "invalid");
|
|
761
|
+
return locator;
|
|
762
|
+
}
|
|
763
|
+
const quantifiers = trimmed.match(/[+*]|\{\d/g) ?? [];
|
|
764
|
+
if (trimmed.length > MAX_SEARCH_REGEX_CHARS || UNSAFE_NESTED.test(trimmed) || trimmed.includes("(?") || /\\[1-9]/.test(trimmed) || quantifiers.length > 8) {
|
|
765
|
+
throw httpFail(400, "That regular expression is not allowed", "unsafe_regex");
|
|
766
|
+
}
|
|
767
|
+
let expression;
|
|
768
|
+
try {
|
|
769
|
+
expression = new RegExp(trimmed, "i");
|
|
770
|
+
} catch {
|
|
771
|
+
throw httpFail(400, "That regular expression is invalid", "invalid");
|
|
772
|
+
}
|
|
773
|
+
return (text) => {
|
|
774
|
+
const haystack = text.length > 4e3 ? text.slice(0, 4e3) : text;
|
|
775
|
+
const match = expression.exec(haystack);
|
|
776
|
+
if (!match || match[0].length === 0) return null;
|
|
777
|
+
return { index: match.index, length: match[0].length };
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
function snippetAround(text, index, length, max = MAX_SNIPPET_CHARS) {
|
|
781
|
+
const safeIndex = Math.max(0, Math.min(index, text.length));
|
|
782
|
+
const safeLength = Math.max(0, Math.min(length, text.length - safeIndex));
|
|
783
|
+
if (text.length <= max) {
|
|
784
|
+
return { snippet: text, matchStart: safeIndex, matchLength: safeLength };
|
|
785
|
+
}
|
|
786
|
+
const room = Math.max(0, max - safeLength);
|
|
787
|
+
let start = Math.max(0, safeIndex - Math.floor(room / 2));
|
|
788
|
+
let end = Math.min(text.length, start + max);
|
|
789
|
+
start = Math.max(0, end - max);
|
|
790
|
+
const prefix = start > 0 ? "\u2026" : "";
|
|
791
|
+
const suffix = end < text.length ? "\u2026" : "";
|
|
792
|
+
return {
|
|
793
|
+
snippet: prefix + text.slice(start, end) + suffix,
|
|
794
|
+
matchStart: prefix.length + (safeIndex - start),
|
|
795
|
+
matchLength: safeLength
|
|
796
|
+
};
|
|
797
|
+
}
|
|
798
|
+
function toHit(filePath, name, title, match) {
|
|
799
|
+
const snippet = snippetAround(match.text, match.index, match.length);
|
|
800
|
+
return {
|
|
801
|
+
path: filePath,
|
|
802
|
+
name,
|
|
803
|
+
title,
|
|
804
|
+
rank: match.rank,
|
|
805
|
+
field: match.field,
|
|
806
|
+
element: match.elementKind,
|
|
807
|
+
label: searchMatchLabel(match.field, match.elementKind),
|
|
808
|
+
line: match.line,
|
|
809
|
+
snippet: snippet.snippet,
|
|
810
|
+
matchStart: snippet.matchStart,
|
|
811
|
+
matchLength: snippet.matchLength
|
|
812
|
+
};
|
|
813
|
+
}
|
|
814
|
+
function flattenFiles(nodes) {
|
|
815
|
+
const files = [];
|
|
816
|
+
for (const node of nodes) {
|
|
817
|
+
if (node.type === "folder") {
|
|
818
|
+
if (node.children) files.push(...flattenFiles(node.children));
|
|
819
|
+
} else if (isPublicDiagram(node.path)) {
|
|
820
|
+
files.push(node);
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
return files;
|
|
824
|
+
}
|
|
825
|
+
async function entryFor(file, current) {
|
|
826
|
+
const cached = current.entries.get(file.path);
|
|
827
|
+
const size = file.size ?? -1;
|
|
828
|
+
if (cached && cached.mtimeMs === file.mtimeMs && cached.size === size) return cached;
|
|
829
|
+
const disk = await readFile(file.path);
|
|
830
|
+
const next = recordFor(disk.content, disk.mtimeMs, disk.size);
|
|
831
|
+
current.entries.set(disk.path, next);
|
|
832
|
+
current.dirty = true;
|
|
833
|
+
return next;
|
|
834
|
+
}
|
|
835
|
+
function discardSearchCache() {
|
|
836
|
+
cache = null;
|
|
837
|
+
}
|
|
838
|
+
async function rememberSearchEntry(relativePath, content, mtimeMs, size) {
|
|
839
|
+
if (!isPublicDiagram(relativePath)) return;
|
|
840
|
+
await queued(async () => {
|
|
841
|
+
const current = await ensureLoaded();
|
|
842
|
+
current.entries.set(relativePath, recordFor(content, mtimeMs, size));
|
|
843
|
+
current.dirty = true;
|
|
844
|
+
await persist(current);
|
|
845
|
+
});
|
|
846
|
+
}
|
|
847
|
+
async function moveSearchEntries(fromPath, toPath, isDir) {
|
|
848
|
+
await queued(async () => {
|
|
849
|
+
const current = await ensureLoaded();
|
|
850
|
+
const moved = [];
|
|
851
|
+
for (const [key, value] of current.entries) {
|
|
852
|
+
const next = rewriteMovedPath(key, fromPath, toPath, isDir);
|
|
853
|
+
if (next === key) continue;
|
|
854
|
+
moved.push([key, value]);
|
|
855
|
+
}
|
|
856
|
+
if (moved.length === 0) return;
|
|
857
|
+
for (const [key] of moved) current.entries.delete(key);
|
|
858
|
+
for (const [key, value] of moved) {
|
|
859
|
+
const next = rewriteMovedPath(key, fromPath, toPath, isDir);
|
|
860
|
+
if (isPublicDiagram(next)) current.entries.set(next, value);
|
|
861
|
+
}
|
|
862
|
+
current.dirty = true;
|
|
863
|
+
await persist(current);
|
|
864
|
+
});
|
|
865
|
+
}
|
|
866
|
+
async function removeSearchEntries(fromPath, isDir) {
|
|
867
|
+
await queued(async () => {
|
|
868
|
+
const current = await ensureLoaded();
|
|
869
|
+
let changed = false;
|
|
870
|
+
for (const key of [...current.entries.keys()]) {
|
|
871
|
+
const drop = key === fromPath || isDir && key.startsWith(`${fromPath}/`);
|
|
872
|
+
if (!drop) continue;
|
|
873
|
+
current.entries.delete(key);
|
|
874
|
+
changed = true;
|
|
875
|
+
}
|
|
876
|
+
if (!changed) return;
|
|
877
|
+
current.dirty = true;
|
|
878
|
+
await persist(current);
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
async function searchDiagrams(options) {
|
|
882
|
+
const query = options.q ?? "";
|
|
883
|
+
if (!query.trim()) return { query: "", hits: [], truncated: false };
|
|
884
|
+
const scope = options.namesOnly ? "names" : options.contents === false ? "meta" : "all";
|
|
885
|
+
const locator = compileSearchLocator(query, options.regex === true);
|
|
886
|
+
return queued(async () => {
|
|
887
|
+
const current = await ensureLoaded();
|
|
888
|
+
const files = flattenFiles(await buildTree());
|
|
889
|
+
const live = new Set(files.map((file) => file.path));
|
|
890
|
+
for (const key of [...current.entries.keys()]) {
|
|
891
|
+
if (!live.has(key)) {
|
|
892
|
+
current.entries.delete(key);
|
|
893
|
+
current.dirty = true;
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
const hits = [];
|
|
897
|
+
for (const file of files) {
|
|
898
|
+
const entry = await entryFor(file, current);
|
|
899
|
+
const match = matchDiagramSearch(
|
|
900
|
+
{
|
|
901
|
+
path: file.path,
|
|
902
|
+
title: entry.title,
|
|
903
|
+
description: entry.description,
|
|
904
|
+
notes: entry.notes,
|
|
905
|
+
tags: entry.tags,
|
|
906
|
+
elements: entry.elements
|
|
907
|
+
},
|
|
908
|
+
query,
|
|
909
|
+
locator,
|
|
910
|
+
scope
|
|
911
|
+
);
|
|
912
|
+
if (!match) continue;
|
|
913
|
+
hits.push(toHit(file.path, file.name, entry.title, match));
|
|
914
|
+
}
|
|
915
|
+
hits.sort(
|
|
916
|
+
(a, b) => a.rank - b.rank || a.path.localeCompare(b.path) || (a.line ?? 0) - (b.line ?? 0)
|
|
917
|
+
);
|
|
918
|
+
const truncated = hits.length > MAX_SEARCH_RESULTS;
|
|
919
|
+
await persist(current);
|
|
920
|
+
return {
|
|
921
|
+
query: query.trim(),
|
|
922
|
+
truncated,
|
|
923
|
+
hits: truncated ? hits.slice(0, MAX_SEARCH_RESULTS) : hits
|
|
924
|
+
};
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
export {
|
|
928
|
+
MAX_SEARCH_QUERY_CHARS,
|
|
929
|
+
MAX_SEARCH_REGEX_CHARS,
|
|
930
|
+
MAX_SEARCH_RESULTS,
|
|
931
|
+
MAX_SNIPPET_CHARS,
|
|
932
|
+
bloomMayContain,
|
|
933
|
+
compileSearchLocator,
|
|
934
|
+
discardSearchCache,
|
|
935
|
+
freshIndexedBlooms,
|
|
936
|
+
gramBloom,
|
|
937
|
+
moveSearchEntries,
|
|
938
|
+
rememberSearchEntries,
|
|
939
|
+
rememberSearchEntry,
|
|
940
|
+
removeSearchEntries,
|
|
941
|
+
searchDiagrams,
|
|
942
|
+
snippetAround
|
|
943
|
+
};
|