nerdgram1337 1.4.5 → 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.
@@ -0,0 +1,87 @@
1
+ import {
2
+ matchFrontMatter
3
+ } from "./chunk-AFGGIQ22.js";
4
+
5
+ // server/src/parseError.ts
6
+ function locateParseError(error, source) {
7
+ const hash = readHash(error);
8
+ const loc = hash?.loc;
9
+ const shift = frontMatterLineShift(source);
10
+ if (loc && typeof loc.first_line === "number" && loc.first_line > 0) {
11
+ const line2 = loc.first_line + shift;
12
+ const column2 = typeof loc.first_column === "number" && loc.first_column >= 0 ? loc.first_column + 1 : 1;
13
+ const span2 = spanFor(source, line2, column2, loc);
14
+ return { ...span2, message: plainMessage(line2, column2, hash?.token) };
15
+ }
16
+ const message = error instanceof Error ? error.message : String(error);
17
+ const lineMatch = message.match(/Parse error on line (\d+)/i) ?? message.match(/\bline\s+(\d+)\b/i);
18
+ const line = (lineMatch ? Math.max(1, Number(lineMatch[1])) : 1) + shift;
19
+ const column = caretColumn(message, source, line);
20
+ const span = spanFor(source, line, column, null);
21
+ return { ...span, message: plainMessage(line, column, void 0) };
22
+ }
23
+ function isMermaidEnvironmentError(error) {
24
+ const message = error instanceof Error ? error.message : String(error);
25
+ return /DOMPurify|document is not defined|window is not defined|CSSStyleSheet is not defined/i.test(message);
26
+ }
27
+ function frontMatterLineShift(source) {
28
+ const front = matchFrontMatter(source);
29
+ if (!front || front.length <= 0) return 0;
30
+ const prefix = source.slice(0, front.length);
31
+ let lines = 0;
32
+ for (let i = 0; i < prefix.length; i += 1) {
33
+ if (prefix[i] === "\n") lines += 1;
34
+ }
35
+ return lines;
36
+ }
37
+ function readHash(error) {
38
+ if (!error || typeof error !== "object" || !("hash" in error)) return null;
39
+ const hash = error.hash;
40
+ if (!hash || typeof hash !== "object") return null;
41
+ const record = hash;
42
+ return { loc: record.loc, token: typeof record.token === "string" ? record.token : void 0 };
43
+ }
44
+ function spanFor(source, line, column, loc) {
45
+ const lines = source.split(/\n/);
46
+ const index = Math.min(Math.max(line - 1, 0), Math.max(lines.length - 1, 0));
47
+ let lineStart = 0;
48
+ for (let i = 0; i < index; i += 1) lineStart += (lines[i]?.length ?? 0) + 1;
49
+ const lineText = (lines[index] ?? "").replace(/\r$/, "");
50
+ const from = lineStart + Math.min(Math.max(column - 1, 0), lineText.length);
51
+ let to = lineStart + lineText.length;
52
+ if (loc && loc.last_line === loc.first_line && typeof loc.last_column === "number" && typeof loc.first_column === "number" && loc.last_column > loc.first_column) {
53
+ to = lineStart + Math.min(loc.last_column, lineText.length);
54
+ }
55
+ if (to <= from) to = Math.min(lineStart + lineText.length, from + 1);
56
+ if (to <= from) to = from + 1;
57
+ return { line: index + 1, column: from - lineStart + 1, from, to };
58
+ }
59
+ function caretColumn(message, source, line) {
60
+ const lines = message.split(/\n/);
61
+ const caret = lines.find((item) => /^\s*-+\^/.test(item) || /^\s*\^+$/.test(item));
62
+ if (!caret) return 1;
63
+ const mark = caret.lastIndexOf("^");
64
+ if (mark < 0) return 1;
65
+ const sourceLine = source.split(/\n/)[line - 1] ?? "";
66
+ if (sourceLine.length <= mark) return mark + 1;
67
+ return 1;
68
+ }
69
+ function plainMessage(line, column, token) {
70
+ const where = `Line ${line}, column ${column}`;
71
+ const found = foundPhrase(token);
72
+ if (!found) return `${where}: this isn't valid Mermaid.`;
73
+ return `${where}: this isn't valid Mermaid. The parser stopped at ${found}.`;
74
+ }
75
+ function foundPhrase(token) {
76
+ if (!token) return null;
77
+ const name = token.replace(/^'+|'+$/g, "").toUpperCase();
78
+ if (name === "EOF") return "the end of the diagram";
79
+ if (name === "NEWLINE" || name === "NL") return "the end of the line";
80
+ if (name === "INVALID") return "text Mermaid doesn't accept here";
81
+ return null;
82
+ }
83
+
84
+ export {
85
+ locateParseError,
86
+ isMermaidEnvironmentError
87
+ };
@@ -0,0 +1,90 @@
1
+ // server/src/diagramLinks.ts
2
+ var DIAGRAM_SCHEME = /^(nerdgram|diagram):/i;
3
+ var ANY_SCHEME = /^[a-z][a-z0-9+.-]*:/i;
4
+ function isDiagramLink(url) {
5
+ if (!url || url.startsWith("//")) return false;
6
+ if (ANY_SCHEME.test(url) && !DIAGRAM_SCHEME.test(url)) return false;
7
+ const pathPart = url.split(/[?#]/)[0] ?? "";
8
+ return DIAGRAM_SCHEME.test(url) || /\.mmd$/i.test(pathPart) || pathPart.startsWith("./") || pathPart.includes("/");
9
+ }
10
+ function parentPath(filePath) {
11
+ const parts = filePath.replace(/\\/g, "/").split("/").filter(Boolean);
12
+ parts.pop();
13
+ return parts.join("/");
14
+ }
15
+ function resolveDiagramTarget(raw, sourcePath = "") {
16
+ const trimmed = raw.trim();
17
+ if (!isDiagramLink(trimmed)) return { ok: false, reason: "not-diagram" };
18
+ let target = trimmed.replace(/\\/g, "/");
19
+ const scheme = DIAGRAM_SCHEME.exec(target);
20
+ if (scheme) target = target.slice(scheme[0].length);
21
+ const cut = target.search(/[?#]/);
22
+ if (cut >= 0) target = target.slice(0, cut);
23
+ target = target.replace(/^\/+/, "");
24
+ if (!target || target.includes("\0")) return { ok: false, reason: "invalid" };
25
+ const base = scheme ? "" : parentPath(sourcePath);
26
+ const combined = base ? `${base}/${target}` : target;
27
+ const parts = [];
28
+ for (const part of combined.split("/")) {
29
+ if (!part || part === ".") continue;
30
+ if (part === "..") {
31
+ if (parts.length === 0) return { ok: false, reason: "escape" };
32
+ parts.pop();
33
+ continue;
34
+ }
35
+ if (part.startsWith(".")) return { ok: false, reason: "invalid" };
36
+ parts.push(part);
37
+ }
38
+ const path = parts.join("/");
39
+ if (!path.toLowerCase().endsWith(".mmd")) return { ok: false, reason: "not-diagram" };
40
+ return { ok: true, path };
41
+ }
42
+ function normalizeWorkspacePath(raw) {
43
+ const trimmed = raw.trim().replace(/\\/g, "/").replace(/^\/+|\/+$/g, "");
44
+ if (!trimmed) return "";
45
+ const parts = [];
46
+ for (const part of trimmed.split("/")) {
47
+ if (!part || part === ".") continue;
48
+ if (part === "..") {
49
+ if (parts.length === 0) return trimmed.replace(/^\.\//, "");
50
+ parts.pop();
51
+ continue;
52
+ }
53
+ parts.push(part);
54
+ }
55
+ return parts.join("/");
56
+ }
57
+ function diagramTargetExists(target, paths, sourcePath = "") {
58
+ const resolved = resolveDiagramTarget(target, sourcePath);
59
+ if (!resolved.ok) return false;
60
+ return paths.some((path) => normalizeWorkspacePath(path) === resolved.path);
61
+ }
62
+ function parseClickLine(line, lineStart, lineNo) {
63
+ const match = /^\s*click\s+([A-Za-z_][\w-]*)\s+(.*)$/i.exec(line);
64
+ if (!match || match[1] == null || match[2] == null) return null;
65
+ const nodeId = match[1];
66
+ const rest = match[2];
67
+ if (/^\s*call\b/i.test(rest)) return null;
68
+ const href = /^\s*href\s+/i.exec(rest);
69
+ const urlPart = href ? rest.slice(href[0].length) : rest;
70
+ const urlPartStart = line.length - rest.length + (href ? href[0].length : 0);
71
+ const quoted = /"([^"]*)"/.exec(urlPart);
72
+ if (!quoted || quoted[1] == null || quoted.index == null) return null;
73
+ const quoteAt = urlPartStart + quoted.index;
74
+ const from = lineStart + quoteAt + 1;
75
+ return {
76
+ nodeId,
77
+ target: quoted[1],
78
+ from,
79
+ to: from + quoted[1].length,
80
+ line: lineNo,
81
+ column: quoteAt + 2
82
+ };
83
+ }
84
+
85
+ export {
86
+ isDiagramLink,
87
+ resolveDiagramTarget,
88
+ diagramTargetExists,
89
+ parseClickLine
90
+ };
@@ -376,84 +376,254 @@ function diagramInitConfig(source, fallbackTheme = FALLBACK_THEME) {
376
376
  init.fontFamily = fontFamily || DEFAULT_FONT_FAMILY;
377
377
  return init;
378
378
  }
379
-
380
- // server/src/parseError.ts
381
- function locateParseError(error, source) {
382
- const hash = readHash(error);
383
- const loc = hash?.loc;
384
- const shift = frontMatterLineShift(source);
385
- if (loc && typeof loc.first_line === "number" && loc.first_line > 0) {
386
- const line2 = loc.first_line + shift;
387
- const column2 = typeof loc.first_column === "number" && loc.first_column >= 0 ? loc.first_column + 1 : 1;
388
- const span2 = spanFor(source, line2, column2, loc);
389
- return { ...span2, message: plainMessage(line2, column2, hash?.token) };
390
- }
391
- const message = error instanceof Error ? error.message : String(error);
392
- const lineMatch = message.match(/Parse error on line (\d+)/i) ?? message.match(/\bline\s+(\d+)\b/i);
393
- const line = (lineMatch ? Math.max(1, Number(lineMatch[1])) : 1) + shift;
394
- const column = caretColumn(message, source, line);
395
- const span = spanFor(source, line, column, null);
396
- return { ...span, message: plainMessage(line, column, void 0) };
397
- }
398
- function isMermaidEnvironmentError(error) {
399
- const message = error instanceof Error ? error.message : String(error);
400
- return /DOMPurify|document is not defined|window is not defined|CSSStyleSheet is not defined/i.test(message);
401
- }
402
- function frontMatterLineShift(source) {
403
- const front = matchFrontMatter(source);
404
- if (!front || front.length <= 0) return 0;
405
- const prefix = source.slice(0, front.length);
406
- let lines = 0;
407
- for (let i = 0; i < prefix.length; i += 1) {
408
- if (prefix[i] === "\n") lines += 1;
409
- }
410
- return lines;
411
- }
412
- function readHash(error) {
413
- if (!error || typeof error !== "object" || !("hash" in error)) return null;
414
- const hash = error.hash;
415
- if (!hash || typeof hash !== "object") return null;
416
- const record = hash;
417
- return { loc: record.loc, token: typeof record.token === "string" ? record.token : void 0 };
418
- }
419
- function spanFor(source, line, column, loc) {
420
- const lines = source.split(/\n/);
421
- const index = Math.min(Math.max(line - 1, 0), Math.max(lines.length - 1, 0));
422
- let lineStart = 0;
423
- for (let i = 0; i < index; i += 1) lineStart += (lines[i]?.length ?? 0) + 1;
424
- const lineText = (lines[index] ?? "").replace(/\r$/, "");
425
- const from = lineStart + Math.min(Math.max(column - 1, 0), lineText.length);
426
- let to = lineStart + lineText.length;
427
- if (loc && loc.last_line === loc.first_line && typeof loc.last_column === "number" && typeof loc.first_column === "number" && loc.last_column > loc.first_column) {
428
- to = lineStart + Math.min(loc.last_column, lineText.length);
429
- }
430
- if (to <= from) to = Math.min(lineStart + lineText.length, from + 1);
431
- if (to <= from) to = from + 1;
432
- return { line: index + 1, column: from - lineStart + 1, from, to };
433
- }
434
- function caretColumn(message, source, line) {
435
- const lines = message.split(/\n/);
436
- const caret = lines.find((item) => /^\s*-+\^/.test(item) || /^\s*\^+$/.test(item));
437
- if (!caret) return 1;
438
- const mark = caret.lastIndexOf("^");
439
- if (mark < 0) return 1;
440
- const sourceLine = source.split(/\n/)[line - 1] ?? "";
441
- if (sourceLine.length <= mark) return mark + 1;
442
- return 1;
443
- }
444
- function plainMessage(line, column, token) {
445
- const where = `Line ${line}, column ${column}`;
446
- const found = foundPhrase(token);
447
- if (!found) return `${where}: this isn't valid Mermaid.`;
448
- return `${where}: this isn't valid Mermaid. The parser stopped at ${found}.`;
449
- }
450
- function foundPhrase(token) {
451
- if (!token) return null;
452
- const name = token.replace(/^'+|'+$/g, "").toUpperCase();
453
- if (name === "EOF") return "the end of the diagram";
454
- if (name === "NEWLINE" || name === "NL") return "the end of the line";
455
- if (name === "INVALID") return "text Mermaid doesn't accept here";
456
- return null;
379
+ function newlineOf(source) {
380
+ return source.includes("\r\n") ? "\r\n" : "\n";
381
+ }
382
+ function isEmptyCollection(node) {
383
+ if (isMap(node) || isSeq(node)) return node.items.length === 0;
384
+ return false;
385
+ }
386
+ function pruneEmpty(doc, path) {
387
+ if (path.length === 0) return;
388
+ const node = doc.getIn(path);
389
+ if (!isEmptyCollection(node)) return;
390
+ doc.deleteIn(path);
391
+ pruneEmpty(doc, path.slice(0, -1));
392
+ }
393
+ function setOrClear(doc, path, value) {
394
+ const clear = value == null || typeof value === "string" && value.trim() === "" || Array.isArray(value) && value.length === 0;
395
+ if (clear) doc.deleteIn(path);
396
+ else doc.setIn(path, value);
397
+ pruneEmpty(doc, path.slice(0, -1));
398
+ }
399
+ function loadDocument(yaml) {
400
+ const doc = parseDocument(yaml, { schema: "core", prettyErrors: false });
401
+ if (doc.errors.length > 0) {
402
+ const message = doc.errors[0]?.message.split("\n")[0]?.trim() || "The YAML front matter could not be parsed.";
403
+ return { doc, error: message };
404
+ }
405
+ const raw = doc.toJS();
406
+ if (raw != null && !isPlainObject(raw)) {
407
+ return { doc, error: "Front matter must be a YAML mapping of keys." };
408
+ }
409
+ if (!isMap(doc.contents)) {
410
+ const blank = new Document({}, { schema: "core" });
411
+ doc.contents = blank.contents;
412
+ }
413
+ return { doc, error: null };
414
+ }
415
+ function compose(source, body, doc) {
416
+ const bom = source.startsWith("\uFEFF") ? "\uFEFF" : "";
417
+ const newline = newlineOf(source);
418
+ if (!doc || !isMap(doc.contents) || doc.contents.items.length === 0) {
419
+ return bom + body;
420
+ }
421
+ let yaml = doc.toString({ lineWidth: 0 });
422
+ if (!yaml.endsWith("\n")) yaml += "\n";
423
+ let header = `---
424
+ ${yaml}---
425
+ `;
426
+ if (newline === "\r\n") header = header.replace(/\n/g, "\r\n");
427
+ return bom + header + body;
428
+ }
429
+ function applyToDocument(doc, patch) {
430
+ if ("title" in patch) setOrClear(doc, ["title"], patch.title ?? null);
431
+ if ("displayMode" in patch) setOrClear(doc, ["displayMode"], patch.displayMode?.trim() ?? null);
432
+ if ("description" in patch) setOrClear(doc, [NERDGRAM, "description"], patch.description ?? null);
433
+ if ("notes" in patch) setOrClear(doc, [NERDGRAM, "notes"], patch.notes ?? null);
434
+ if ("tags" in patch) {
435
+ const tags = (patch.tags ?? []).map((tag) => tag.trim()).filter((tag, index, all) => tag && all.indexOf(tag) === index);
436
+ setOrClear(doc, [NERDGRAM, "tags"], tags);
437
+ }
438
+ if ("category" in patch) setOrClear(doc, [NERDGRAM, "category"], patch.category?.trim() ?? null);
439
+ if ("theme" in patch) setOrClear(doc, ["config", "theme"], patch.theme?.trim() ?? null);
440
+ if ("look" in patch) setOrClear(doc, ["config", "look"], patch.look?.trim() ?? null);
441
+ if ("layout" in patch) setOrClear(doc, ["config", "layout"], patch.layout?.trim() ?? null);
442
+ if ("fontFamily" in patch) setOrClear(doc, ["config", "fontFamily"], patch.fontFamily?.trim() ?? null);
443
+ if ("themeCSS" in patch) setOrClear(doc, ["config", "themeCSS"], patch.themeCSS ?? null);
444
+ if ("handDrawnSeed" in patch) {
445
+ if (patch.handDrawnSeed == null || !Number.isFinite(patch.handDrawnSeed)) {
446
+ setOrClear(doc, ["config", "handDrawnSeed"], null);
447
+ } else {
448
+ setOrClear(doc, ["config", "handDrawnSeed"], patch.handDrawnSeed);
449
+ }
450
+ }
451
+ if (patch.themeVariables) {
452
+ for (const [key, value] of Object.entries(patch.themeVariables)) {
453
+ setOrClear(doc, ["config", "themeVariables", key], value ?? null);
454
+ }
455
+ }
456
+ if (patch.elk) {
457
+ for (const [key, value] of Object.entries(patch.elk)) {
458
+ setOrClear(doc, ["config", "elk", key], value ?? null);
459
+ }
460
+ }
461
+ if ("flowchartCurve" in patch) {
462
+ setOrClear(doc, ["config", "flowchart", "curve"], patch.flowchartCurve?.trim() ?? null);
463
+ }
464
+ pruneEmpty(doc, [NERDGRAM]);
465
+ pruneEmpty(doc, ["config"]);
466
+ }
467
+ function applyDiagramPatch(source, patch) {
468
+ const text = source.startsWith("\uFEFF") ? source.slice(1) : source;
469
+ const match = matchFrontMatter(text);
470
+ if (match) {
471
+ const loaded = loadDocument(yamlText(match));
472
+ if (loaded.error) return { ok: false, error: loaded.error, source };
473
+ try {
474
+ applyToDocument(loaded.doc, patch);
475
+ return { ok: true, source: compose(source, text.slice(match.length), loaded.doc) };
476
+ } catch (err) {
477
+ const message = err instanceof Error ? err.message : "Could not update front matter.";
478
+ return { ok: false, error: message, source };
479
+ }
480
+ }
481
+ const doc = new Document({}, { schema: "core" });
482
+ try {
483
+ applyToDocument(doc, patch);
484
+ return { ok: true, source: compose(source, text, doc) };
485
+ } catch (err) {
486
+ const message = err instanceof Error ? err.message : "Could not update front matter.";
487
+ return { ok: false, error: message, source };
488
+ }
489
+ }
490
+ function literalLocator(query) {
491
+ const needle = query.trim();
492
+ if (!needle) return null;
493
+ const lower = needle.toLowerCase();
494
+ return (text) => {
495
+ const index = text.toLowerCase().indexOf(lower);
496
+ if (index < 0) return null;
497
+ return { index, length: needle.length };
498
+ };
499
+ }
500
+ var ELEMENT_LABELS = {
501
+ node: "Node label",
502
+ edge: "Edge label",
503
+ subgraph: "Subgraph title",
504
+ title: "Diagram title",
505
+ accTitle: "Title",
506
+ accDescr: "Description",
507
+ note: "Note",
508
+ participant: "Participant",
509
+ message: "Sequence message",
510
+ class: "Class",
511
+ member: "Member",
512
+ entity: "Entity",
513
+ attribute: "Attribute",
514
+ task: "Task",
515
+ section: "Section",
516
+ slice: "Slice",
517
+ state: "State",
518
+ text: "Text"
519
+ };
520
+ function searchMatchLabel(field, elementKind) {
521
+ if (field === "filename") return "File name";
522
+ if (field === "path") return "Path";
523
+ if (field === "title") return "Title";
524
+ if (field === "tag") return "Tag";
525
+ if (field === "description") return "Description";
526
+ if (field === "notes") return "Notes";
527
+ if (elementKind && ELEMENT_LABELS[elementKind]) return ELEMENT_LABELS[elementKind];
528
+ return "Text";
529
+ }
530
+ function matchDiagramSearch(item, query, locate = literalLocator(query), scope = "all") {
531
+ if (!locate) return null;
532
+ const fileName = item.path.split("/").pop()?.replace(/\.mmd$/i, "") ?? item.path;
533
+ const fileHit = locate(fileName);
534
+ if (fileHit) {
535
+ return {
536
+ rank: 0,
537
+ field: "filename",
538
+ elementKind: null,
539
+ line: null,
540
+ text: fileName,
541
+ index: fileHit.index,
542
+ length: fileHit.length
543
+ };
544
+ }
545
+ const pathHit = locate(item.path);
546
+ if (pathHit) {
547
+ return {
548
+ rank: 0,
549
+ field: "path",
550
+ elementKind: null,
551
+ line: null,
552
+ text: item.path,
553
+ index: pathHit.index,
554
+ length: pathHit.length
555
+ };
556
+ }
557
+ if (scope === "names") return null;
558
+ const title = item.title ?? "";
559
+ const titleHit = title ? locate(title) : null;
560
+ if (titleHit) {
561
+ return {
562
+ rank: 1,
563
+ field: "title",
564
+ elementKind: null,
565
+ line: null,
566
+ text: title,
567
+ index: titleHit.index,
568
+ length: titleHit.length
569
+ };
570
+ }
571
+ for (const tag of item.tags) {
572
+ const tagHit = locate(tag);
573
+ if (tagHit) {
574
+ return {
575
+ rank: 2,
576
+ field: "tag",
577
+ elementKind: null,
578
+ line: null,
579
+ text: tag,
580
+ index: tagHit.index,
581
+ length: tagHit.length
582
+ };
583
+ }
584
+ }
585
+ const descriptionHit = item.description ? locate(item.description) : null;
586
+ if (descriptionHit) {
587
+ return {
588
+ rank: 3,
589
+ field: "description",
590
+ elementKind: null,
591
+ line: null,
592
+ text: item.description,
593
+ index: descriptionHit.index,
594
+ length: descriptionHit.length
595
+ };
596
+ }
597
+ const notesHit = item.notes ? locate(item.notes) : null;
598
+ if (notesHit) {
599
+ return {
600
+ rank: 4,
601
+ field: "notes",
602
+ elementKind: null,
603
+ line: null,
604
+ text: item.notes,
605
+ index: notesHit.index,
606
+ length: notesHit.length
607
+ };
608
+ }
609
+ if (scope !== "all") return null;
610
+ let best = null;
611
+ for (const element of item.elements ?? []) {
612
+ if (!element.text) continue;
613
+ const hit = locate(element.text);
614
+ if (!hit || hit.length <= 0) continue;
615
+ const candidate = {
616
+ rank: 5,
617
+ field: "text",
618
+ elementKind: element.kind,
619
+ line: element.line,
620
+ text: element.text,
621
+ index: hit.index,
622
+ length: hit.length
623
+ };
624
+ if (!best || (candidate.line ?? 0) < (best.line ?? 0)) best = candidate;
625
+ }
626
+ return best;
457
627
  }
458
628
 
459
629
  export {
@@ -466,6 +636,8 @@ export {
466
636
  parseDiagram,
467
637
  diagramConfigKey,
468
638
  diagramInitConfig,
469
- locateParseError,
470
- isMermaidEnvironmentError
639
+ applyDiagramPatch,
640
+ literalLocator,
641
+ searchMatchLabel,
642
+ matchDiagramSearch
471
643
  };