@markdy/mcp-server 1.5.8 → 1.6.0

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.
Files changed (2) hide show
  1. package/dist/index.js +408 -65
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -4229,29 +4229,94 @@ function getArchitectureSuggestions(docText) {
4229
4229
  }
4230
4230
 
4231
4231
  // ../compat/src/mermaid/mermaid-transpiler.ts
4232
- function sanitizeId(id) {
4233
- return id.trim().replace(/[^a-zA-Z0-9_]/g, "_");
4232
+ var RESERVED_IDS = /* @__PURE__ */ new Set([
4233
+ "scene",
4234
+ "layout",
4235
+ "group",
4236
+ "beat",
4237
+ "style",
4238
+ "pattern",
4239
+ "use",
4240
+ "edge",
4241
+ "annotation",
4242
+ "var",
4243
+ "player",
4244
+ "show",
4245
+ "hide",
4246
+ "glow",
4247
+ "focus",
4248
+ "frame"
4249
+ ]);
4250
+ function sanitizeId(id, fallback = "node") {
4251
+ let cleaned = id.trim().replace(/[^a-zA-Z0-9_]/g, "_").replace(/^_+|_+$/g, "");
4252
+ if (!cleaned || /^[0-9]/.test(cleaned)) {
4253
+ cleaned = `${fallback}_${cleaned || "item"}`;
4254
+ }
4255
+ if (RESERVED_IDS.has(cleaned)) {
4256
+ cleaned = `${cleaned}_node`;
4257
+ }
4258
+ return cleaned;
4234
4259
  }
4235
4260
  function cleanLabel(raw) {
4236
- return raw.trim().replace(/^["'\[\(\{]+/, "").replace(/["'\]\)\}]+$/, "").replace(/<br\s*\/?>/gi, " ");
4261
+ let s = raw.trim();
4262
+ const pairs = [
4263
+ ["[(", ")]"],
4264
+ ["[[", "]]"],
4265
+ ["([", "])"],
4266
+ ["((", "))"],
4267
+ ["{{", "}}"],
4268
+ ["[/", "/]"],
4269
+ ["[\\", "\\]"],
4270
+ ["[", "]"],
4271
+ ["{", "}"],
4272
+ ["(", ")"]
4273
+ ];
4274
+ for (const [open, close] of pairs) {
4275
+ if (s.startsWith(open) && s.endsWith(close) && s.length >= open.length + close.length) {
4276
+ s = s.slice(open.length, s.length - close.length).trim();
4277
+ break;
4278
+ }
4279
+ }
4280
+ return s.replace(/^["'`]+|["'`]+$/g, "").replace(/<br\s*\/?>/gi, " ").replace(/"/g, "'").replace(/\s+/g, " ").trim();
4281
+ }
4282
+ function extractMermaidBracket(body) {
4283
+ if (!body) return void 0;
4284
+ for (const prefix of ["[(", "[[", "([", "((", "{{", "[/", "[\\"]) {
4285
+ if (body.startsWith(prefix)) return prefix;
4286
+ }
4287
+ return body.slice(0, 1);
4237
4288
  }
4238
4289
  function inferKindFromMermaid(id, label, shapeBracket) {
4239
4290
  const text = `${id} ${label}`.toLowerCase();
4240
- if (shapeBracket === "[(" || shapeBracket === ")]" || /(db|database|sql|postgres|mongo|dynamo|redis)/.test(text)) {
4291
+ if (shapeBracket === "{") return "decision";
4292
+ if (/(cache|redis|memcached|varnish)/.test(text)) return "cache";
4293
+ if (shapeBracket === "[(" || shapeBracket === ")]" || /(db|database|sql|postgres|mysql|mongo|dynamo|sqlite|aurora)/.test(text)) {
4241
4294
  return "database";
4242
4295
  }
4243
- if (shapeBracket === "{{" || shapeBracket === "}}" || /(queue|kafka|rabbitmq|sqs|event|bus)/.test(text)) {
4296
+ if (shapeBracket === "{{") {
4297
+ return /(gateway|proxy|ingress|lb|alb|nginx|envoy)/.test(text) && !/(queue|kafka|rabbitmq|sqs|event|bus|pubsub|nats)/.test(text) ? "gateway" : "queue";
4298
+ }
4299
+ if (shapeBracket === "([") {
4300
+ if (/(user|customer|actor)/.test(text)) return "user";
4301
+ if (/^(start|begin|init|entry)\b/.test(text)) return "start";
4302
+ if (/^(end|done|finish|stop|exit)\b/.test(text)) return "end";
4303
+ return "browser";
4304
+ }
4305
+ if (/(queue|kafka|rabbitmq|sqs|event|bus|pubsub|nats)/.test(text)) {
4244
4306
  return "queue";
4245
4307
  }
4246
- if (shapeBracket === "{" || shapeBracket === "}" || /(decision|check|valid|auth|gate)/.test(text)) {
4308
+ if (/(decision|check|valid|condition|branch|if_)/.test(text)) {
4309
+ return "decision";
4310
+ }
4311
+ if (/(gateway|proxy|ingress|lb|alb|nginx|envoy)/.test(text)) {
4247
4312
  return "gateway";
4248
4313
  }
4249
- if (shapeBracket === "([" || shapeBracket === "])" || /(client|user|browser|ui|app|web|frontend)/.test(text)) {
4250
- return "browser";
4314
+ if (shapeBracket === "((" || /(client|user|browser|ui|app|web|frontend|mobile)/.test(text)) {
4315
+ return /(user|customer|actor)/.test(text) ? "user" : "browser";
4251
4316
  }
4252
- if (/(cache|memcached|varnish)/.test(text)) return "cache";
4253
- if (/(storage|s3|bucket|blob)/.test(text)) return "storage";
4254
- if (/(worker|cron|job|lambda|function)/.test(text)) return "worker";
4317
+ if (/(storage|s3|bucket|blob|disk|volume)/.test(text)) return "storage";
4318
+ if (/(worker|cron|job|lambda|function|consumer)/.test(text)) return "worker";
4319
+ if (/(firewall|waf|auth|oauth|oidc|vault|security)/.test(text)) return "auth";
4255
4320
  return "service";
4256
4321
  }
4257
4322
  function transpileMermaidToMarkdy(mermaidSource, sceneTitle = "Imported Diagram") {
@@ -4263,59 +4328,269 @@ layout LR
4263
4328
  `,
4264
4329
  diagramType: "architecture",
4265
4330
  nodeCount: 0,
4266
- edgeCount: 0
4331
+ edgeCount: 0,
4332
+ groupCount: 0,
4333
+ warnings: []
4267
4334
  };
4268
4335
  }
4269
4336
  const firstLine = lines[0].toLowerCase();
4270
4337
  if (firstLine.startsWith("sequencediagram")) {
4271
4338
  return transpileSequenceDiagram(lines.slice(1), sceneTitle);
4272
4339
  }
4340
+ if (firstLine.startsWith("statediagram")) {
4341
+ return transpileStateDiagram(lines.slice(1), sceneTitle);
4342
+ }
4273
4343
  return transpileFlowchart(lines, sceneTitle);
4274
4344
  }
4275
4345
  function transpileSequenceDiagram(lines, title) {
4276
4346
  const participants = /* @__PURE__ */ new Map();
4277
4347
  const messages = [];
4348
+ const warnings = [];
4278
4349
  for (const line of lines) {
4279
- const partMatch = /^(?:participant|actor)\s+([^\s]+)(?:\s+as\s+(.+))?$/i.exec(line);
4350
+ if (/^(autonumber|activate|deactivate|note\b|alt\b|else\b|opt\b|loop\b|par\b|and\b|critical\b|option\b|break\b|rect\b|box\b|end\b|title\b|link\b|links\b)/i.test(line)) {
4351
+ continue;
4352
+ }
4353
+ const partMatch = /^(?:participant|actor)\s+("[^"]+"|[^\s]+)(?:\s+as\s+(.+))?$/i.exec(line);
4280
4354
  if (partMatch) {
4281
- const id = sanitizeId(partMatch[1]);
4282
- const label = partMatch[2] ? cleanLabel(partMatch[2]) : id;
4283
- const kind = inferKindFromMermaid(id, label);
4355
+ const rawAlias = partMatch[1].replace(/^["']|["']$/g, "");
4356
+ const id = sanitizeId(rawAlias);
4357
+ const label = partMatch[2] ? cleanLabel(partMatch[2]) : rawAlias;
4358
+ const isActor = line.toLowerCase().startsWith("actor");
4359
+ const kind = isActor ? "user" : inferKindFromMermaid(id, label);
4284
4360
  participants.set(id, { id, label, kind });
4285
4361
  continue;
4286
4362
  }
4287
- const msgMatch = /^([a-zA-Z0-9_]+)\s*(-->>|->>|->|-->|-\)|~>)\s*([a-zA-Z0-9_]+)\s*:\s*(.+)$/.exec(line);
4363
+ const msgMatch = /^([a-zA-Z0-9_-]+?)\s*(-->>|->>|-->|->|-\)|--x|-x|~>)[+-]?\s*([a-zA-Z0-9_-]+)\s*:\s*(.*)$/.exec(line);
4288
4364
  if (msgMatch) {
4289
4365
  const from = sanitizeId(msgMatch[1]);
4290
4366
  const arrow = msgMatch[2];
4291
4367
  const to = sanitizeId(msgMatch[3]);
4292
- const label = cleanLabel(msgMatch[4]);
4368
+ const label = cleanLabel(msgMatch[4] || "call") || "call";
4293
4369
  if (!participants.has(from)) participants.set(from, { id: from, label: from, kind: inferKindFromMermaid(from, from) });
4294
4370
  if (!participants.has(to)) participants.set(to, { id: to, label: to, kind: inferKindFromMermaid(to, to) });
4295
4371
  let kind = "->";
4296
- if (arrow === "-->>" || arrow === "-->" || arrow === "-.->") kind = "~>";
4372
+ if (arrow === "-->>" || arrow === "-->" || arrow === "--x") kind = "<-";
4373
+ else if (arrow === "~>" || arrow === "-)") kind = "~>";
4297
4374
  messages.push({ from, to, label, kind });
4375
+ continue;
4298
4376
  }
4377
+ warnings.push(`Skipped sequence directive: ${line.slice(0, 60)}`);
4299
4378
  }
4300
4379
  const out = [];
4301
- out.push(title ? `scene "${title}" type=sequence theme=paper` : `scene type=sequence theme=paper`);
4380
+ out.push(title ? `scene "${cleanLabel(title)}" type=sequence theme=paper` : `scene type=sequence theme=paper`);
4302
4381
  out.push("");
4303
4382
  for (const p of participants.values()) {
4304
4383
  out.push(`${p.kind} ${p.id} "${p.label}"`);
4305
4384
  }
4306
4385
  out.push("");
4307
- out.push('beat main "Sequence Flow":');
4386
+ out.push("beat main:");
4308
4387
  out.push(" show $nodes");
4309
4388
  for (const msg of messages) {
4310
- out.push(` ${msg.from} ${msg.kind} ${msg.to} "${msg.label}"`);
4389
+ if (msg.kind === "<-") {
4390
+ out.push(` ${msg.to} <- ${msg.from} "${msg.label}"`);
4391
+ } else {
4392
+ out.push(` ${msg.from} ${msg.kind} ${msg.to} "${msg.label}"`);
4393
+ }
4311
4394
  }
4312
4395
  return {
4313
4396
  code: out.join("\n"),
4314
4397
  diagramType: "sequence",
4315
4398
  nodeCount: participants.size,
4316
- edgeCount: messages.length
4399
+ edgeCount: messages.length,
4400
+ groupCount: 0,
4401
+ warnings
4317
4402
  };
4318
4403
  }
4404
+ function transpileStateDiagram(lines, title) {
4405
+ let direction = "LR";
4406
+ const states = /* @__PURE__ */ new Map();
4407
+ const groups = /* @__PURE__ */ new Map();
4408
+ const transitions = [];
4409
+ const warnings = [];
4410
+ let currentGroup = null;
4411
+ const ensureState = (raw, isTarget) => {
4412
+ if (raw === "[*]") {
4413
+ const id2 = isTarget ? "EndState" : "StartState";
4414
+ if (!states.has(id2)) {
4415
+ states.set(id2, {
4416
+ id: id2,
4417
+ label: isTarget ? "End" : "Start",
4418
+ kind: isTarget ? "end" : "start"
4419
+ });
4420
+ }
4421
+ return id2;
4422
+ }
4423
+ const id = sanitizeId(raw, "state");
4424
+ if (!states.has(id)) {
4425
+ states.set(id, { id, label: raw.replace(/_/g, " "), kind: "state" });
4426
+ }
4427
+ if (currentGroup && !currentGroup.members.includes(id)) {
4428
+ currentGroup.members.push(id);
4429
+ }
4430
+ return id;
4431
+ };
4432
+ for (const line of lines) {
4433
+ const dirMatch = /^direction\s+(lr|rl|tb|td|bt)\b/i.exec(line);
4434
+ if (dirMatch) {
4435
+ direction = dirMatch[1].toUpperCase() === "TD" ? "TB" : dirMatch[1].toUpperCase();
4436
+ continue;
4437
+ }
4438
+ if (/^(note\b|classdef\b|class\b|style\b|hide\b)/i.test(line)) continue;
4439
+ const compositeMatch = /^state\s+(?:"([^"]+)"\s+as\s+)?([a-zA-Z0-9_-]+)\s*\{$/i.exec(line);
4440
+ if (compositeMatch) {
4441
+ const id = sanitizeId(compositeMatch[2], "state_group");
4442
+ const label = compositeMatch[1] ? cleanLabel(compositeMatch[1]) : id;
4443
+ currentGroup = { id, label, members: [] };
4444
+ groups.set(id, currentGroup);
4445
+ continue;
4446
+ }
4447
+ if (line === "}" && currentGroup) {
4448
+ currentGroup = null;
4449
+ continue;
4450
+ }
4451
+ const stateAliasMatch = /^state\s+"([^"]+)"\s+as\s+([a-zA-Z0-9_-]+)$/i.exec(line);
4452
+ if (stateAliasMatch) {
4453
+ const id = sanitizeId(stateAliasMatch[2], "state");
4454
+ const label = cleanLabel(stateAliasMatch[1]);
4455
+ states.set(id, { id, label, kind: "state" });
4456
+ if (currentGroup && !currentGroup.members.includes(id)) currentGroup.members.push(id);
4457
+ continue;
4458
+ }
4459
+ const stateDescMatch = /^([a-zA-Z0-9_-]+)\s*:\s*(.+)$/.exec(line);
4460
+ if (stateDescMatch && !line.includes("-->") && !line.includes("->")) {
4461
+ const id = sanitizeId(stateDescMatch[1], "state");
4462
+ const label = cleanLabel(stateDescMatch[2]);
4463
+ states.set(id, { id, label, kind: "state" });
4464
+ if (currentGroup && !currentGroup.members.includes(id)) currentGroup.members.push(id);
4465
+ continue;
4466
+ }
4467
+ const transMatch = /^(\[\*\]|[a-zA-Z0-9_-]+)\s*(-->|->)\s*(\[\*\]|[a-zA-Z0-9_-]+)(?:\s*:\s*(.+))?$/.exec(line);
4468
+ if (transMatch) {
4469
+ const from = ensureState(transMatch[1], false);
4470
+ const to = ensureState(transMatch[3], true);
4471
+ const label = transMatch[4] ? cleanLabel(transMatch[4]) : void 0;
4472
+ transitions.push({ from, to, label });
4473
+ continue;
4474
+ }
4475
+ warnings.push(`Skipped state directive: ${line.slice(0, 60)}`);
4476
+ }
4477
+ const out = [];
4478
+ out.push(title ? `scene "${cleanLabel(title)}" type=state theme=paper` : `scene type=state theme=paper`);
4479
+ out.push(`layout ${direction}`);
4480
+ out.push("");
4481
+ for (const g of groups.values()) {
4482
+ if (g.members.length > 0) {
4483
+ out.push(`group ${g.id} "${g.label}": ${g.members.join(" ")}`);
4484
+ }
4485
+ }
4486
+ if (groups.size > 0) out.push("");
4487
+ for (const s of states.values()) {
4488
+ out.push(`${s.kind} ${s.id} "${s.label}"`);
4489
+ }
4490
+ out.push("");
4491
+ out.push('beat transitions "State Machine Flow":');
4492
+ out.push(" show $nodes stagger=60ms");
4493
+ for (const tr of transitions) {
4494
+ out.push(tr.label ? ` ${tr.from} -> ${tr.to} "${tr.label}"` : ` ${tr.from} -> ${tr.to}`);
4495
+ }
4496
+ return {
4497
+ code: out.join("\n"),
4498
+ diagramType: "state",
4499
+ nodeCount: states.size,
4500
+ edgeCount: transitions.length,
4501
+ groupCount: groups.size,
4502
+ warnings
4503
+ };
4504
+ }
4505
+ var MERMAID_SHAPE_BODY = "\\[\\([^\\n]*?\\)\\]|\\[\\[[^\\n]*?\\]\\]|\\(\\[[^\\n]*?\\]\\)|\\(\\([^\\n]*?\\)\\)|\\{\\{[^\\n]*?\\}\\}|\\[\\/[^\\n]*?\\/\\]|\\[\\\\[^\\n]*?\\\\\\]|\\[[^\\n\\]]*?\\]|\\{[^\\n\\}]*?\\}|\\([^\\n\\)]*?\\)";
4506
+ function parseMermaidNodeTokens(segment) {
4507
+ return segment.split("&").map((part) => part.trim()).filter(Boolean).map((part) => {
4508
+ const m = new RegExp(`^([a-zA-Z0-9_-]+)\\s*(${MERMAID_SHAPE_BODY})?$`).exec(part);
4509
+ if (!m) {
4510
+ const idMatch = /^([a-zA-Z0-9_-]+)/.exec(part);
4511
+ const raw = idMatch ? idMatch[1] : part;
4512
+ return { id: sanitizeId(raw) };
4513
+ }
4514
+ const rawId = m[1];
4515
+ const body = m[2];
4516
+ return {
4517
+ id: sanitizeId(rawId),
4518
+ label: body ? cleanLabel(body) : void 0,
4519
+ bracket: extractMermaidBracket(body)
4520
+ };
4521
+ });
4522
+ }
4523
+ var TOP_LEVEL_ARROW_RE = /^(<-->|-\.->|==>|<->|-->|---|->|-\.\s*([^.]+?)\s*\.->|==\s*([^=]+?)\s*==>|-\-\s*([^-]+?)\s*-->|--\s*([^-]+?)\s*---)/;
4524
+ function findTopLevelEdges(line) {
4525
+ const matches = [];
4526
+ let inQuote = false;
4527
+ let quoteChar = "";
4528
+ let parenDepth = 0;
4529
+ let bracketDepth = 0;
4530
+ let braceDepth = 0;
4531
+ for (let i = 0; i < line.length; i++) {
4532
+ const ch = line[i];
4533
+ if (inQuote) {
4534
+ if (ch === "\\" && i + 1 < line.length) {
4535
+ i++;
4536
+ continue;
4537
+ }
4538
+ if (ch === quoteChar) inQuote = false;
4539
+ continue;
4540
+ }
4541
+ if (ch === '"' || ch === "'") {
4542
+ inQuote = true;
4543
+ quoteChar = ch;
4544
+ continue;
4545
+ }
4546
+ if (ch === "(") {
4547
+ parenDepth++;
4548
+ continue;
4549
+ }
4550
+ if (ch === ")" && parenDepth > 0) {
4551
+ parenDepth--;
4552
+ continue;
4553
+ }
4554
+ if (ch === "[") {
4555
+ bracketDepth++;
4556
+ continue;
4557
+ }
4558
+ if (ch === "]" && bracketDepth > 0) {
4559
+ bracketDepth--;
4560
+ continue;
4561
+ }
4562
+ if (ch === "{") {
4563
+ braceDepth++;
4564
+ continue;
4565
+ }
4566
+ if (ch === "}" && braceDepth > 0) {
4567
+ braceDepth--;
4568
+ continue;
4569
+ }
4570
+ if (parenDepth > 0 || bracketDepth > 0 || braceDepth > 0) continue;
4571
+ if (ch !== "-" && ch !== "=" && ch !== "<") continue;
4572
+ const sub = line.slice(i);
4573
+ const am = TOP_LEVEL_ARROW_RE.exec(sub);
4574
+ if (!am) continue;
4575
+ let totalLen = am[0].length;
4576
+ const inlineLabel = am[2] || am[3] || am[4] || am[5];
4577
+ let pipeLabel;
4578
+ const afterArrow = line.slice(i + totalLen);
4579
+ const pipeMatch = /^\s*\|([^|]+)\|/.exec(afterArrow);
4580
+ if (pipeMatch) {
4581
+ pipeLabel = pipeMatch[1];
4582
+ totalLen += pipeMatch[0].length;
4583
+ }
4584
+ matches.push({
4585
+ index: i,
4586
+ length: totalLen,
4587
+ arrow: am[1],
4588
+ label: pipeLabel ? cleanLabel(pipeLabel) : inlineLabel ? cleanLabel(inlineLabel) : void 0
4589
+ });
4590
+ i += totalLen - 1;
4591
+ }
4592
+ return matches;
4593
+ }
4319
4594
  function transpileFlowchart(lines, title) {
4320
4595
  let direction = "LR";
4321
4596
  const first = lines[0].toLowerCase();
@@ -4330,58 +4605,124 @@ function transpileFlowchart(lines, title) {
4330
4605
  const nodes = /* @__PURE__ */ new Map();
4331
4606
  const groups = /* @__PURE__ */ new Map();
4332
4607
  const flows = [];
4333
- let currentSubgraph = null;
4334
- const explicitNodeRe = /([a-zA-Z0-9_-]+)\s*(\[\([^\n]*?\)\]|\[\[[^\n]*?\]\]|\(\[[^\n]*?\]\)|\(\([^\n]*?\)\)|\[[^\n]*?\]|\{[^\n]*?\}|\([^\n]*?\))/g;
4335
- const flowPattern = /([a-zA-Z0-9_-]+)\s*(?:\[[^\]]*\]|\([^)]*\)|\{[^}]*\})?\s*(-->|->|==>|-.->|--\s*([^-]+)\s*-->)\s*(?:\|([^|]+)\|)?\s*([a-zA-Z0-9_-]+)/;
4336
- for (const line of lines) {
4337
- const subMatch = /^subgraph\s+([a-zA-Z0-9_]+)?\s*(\[.*\]|".*")?/i.exec(line);
4608
+ const warnings = [];
4609
+ const ownedGroup = /* @__PURE__ */ new Map();
4610
+ const declaredOutsideGroup = /* @__PURE__ */ new Set();
4611
+ const groupStack = [];
4612
+ const registerNode = (id, label, bracket) => {
4613
+ const existing = nodes.get(id);
4614
+ const activeGroup = groupStack[groupStack.length - 1];
4615
+ if (existing) {
4616
+ if (label && existing.label === existing.id) {
4617
+ existing.label = label;
4618
+ existing.kind = inferKindFromMermaid(id, label, bracket);
4619
+ }
4620
+ if (activeGroup && !ownedGroup.has(id) && (!declaredOutsideGroup.has(id) || Boolean(label))) {
4621
+ if (!activeGroup.members.includes(id)) activeGroup.members.push(id);
4622
+ ownedGroup.set(id, activeGroup.id);
4623
+ }
4624
+ } else {
4625
+ const finalLabel = label || id;
4626
+ nodes.set(id, { id, label: finalLabel, kind: inferKindFromMermaid(id, finalLabel, bracket) });
4627
+ if (activeGroup) {
4628
+ if (!activeGroup.members.includes(id)) activeGroup.members.push(id);
4629
+ ownedGroup.set(id, activeGroup.id);
4630
+ } else {
4631
+ declaredOutsideGroup.add(id);
4632
+ }
4633
+ }
4634
+ };
4635
+ const explicitNodeRe = new RegExp(`([a-zA-Z0-9_-]+)\\s*(${MERMAID_SHAPE_BODY})`, "g");
4636
+ for (const rawLine of lines) {
4637
+ const line = rawLine.replace(/;+\s*$/, "").trim();
4638
+ if (!line) continue;
4639
+ if (/^(classDef|class|style|linkStyle|click|direction|accTitle|accDescr)\b/i.test(line)) {
4640
+ continue;
4641
+ }
4642
+ const subMatch = /^subgraph\s+(.+)$/i.exec(line);
4338
4643
  if (subMatch) {
4339
- const rawId = subMatch[1] || `group_${groups.size + 1}`;
4340
- const id = sanitizeId(rawId);
4341
- const label = subMatch[2] ? cleanLabel(subMatch[2]) : id;
4342
- currentSubgraph = { id, label, members: [] };
4343
- groups.set(id, currentSubgraph);
4644
+ const rest = subMatch[1].trim();
4645
+ let rawId = "";
4646
+ let label = "";
4647
+ const idBracket = /^([a-zA-Z0-9_-]+)\s*\[([^\]]+)\]$/.exec(rest);
4648
+ const idQuoted = /^([a-zA-Z0-9_-]+)\s+"([^"]+)"$/.exec(rest);
4649
+ const quotedOnly = /^"([^"]+)"$/.exec(rest);
4650
+ if (idBracket) {
4651
+ rawId = idBracket[1];
4652
+ label = cleanLabel(idBracket[2]);
4653
+ } else if (idQuoted) {
4654
+ rawId = idQuoted[1];
4655
+ label = cleanLabel(idQuoted[2]);
4656
+ } else if (quotedOnly) {
4657
+ rawId = `group_${groups.size + 1}`;
4658
+ label = cleanLabel(quotedOnly[1]);
4659
+ } else if (/^[a-zA-Z0-9_-]+$/.test(rest)) {
4660
+ rawId = rest;
4661
+ label = rest;
4662
+ } else {
4663
+ rawId = `group_${groups.size + 1}`;
4664
+ label = cleanLabel(rest);
4665
+ }
4666
+ const id = sanitizeId(rawId, "group");
4667
+ const grp = { id, label: label || id, members: [] };
4668
+ groups.set(id, grp);
4669
+ groupStack.push(grp);
4344
4670
  continue;
4345
4671
  }
4346
- if (line === "end" && currentSubgraph) {
4347
- currentSubgraph = null;
4672
+ if (line === "end" && groupStack.length > 0) {
4673
+ groupStack.pop();
4674
+ continue;
4675
+ }
4676
+ const edgeMatches = findTopLevelEdges(line);
4677
+ if (edgeMatches.length > 0) {
4678
+ const segments = [];
4679
+ let cursor = 0;
4680
+ for (const m of edgeMatches) {
4681
+ segments.push(line.slice(cursor, m.index).trim());
4682
+ cursor = m.index + m.length;
4683
+ }
4684
+ segments.push(line.slice(cursor).trim());
4685
+ for (let i = 0; i < edgeMatches.length; i++) {
4686
+ const leftNodes = parseMermaidNodeTokens(segments[i] || "");
4687
+ const rightNodes = parseMermaidNodeTokens(segments[i + 1] || "");
4688
+ for (const ln of leftNodes) registerNode(ln.id, ln.label, ln.bracket);
4689
+ for (const rn of rightNodes) registerNode(rn.id, rn.label, rn.bracket);
4690
+ const arrow = edgeMatches[i].arrow;
4691
+ let op = "->";
4692
+ if (arrow === "<-->" || arrow === "<->") op = "<->";
4693
+ else if (arrow.includes("-.")) op = "~>";
4694
+ else if (arrow === "---" || arrow.endsWith("---")) op = "..>";
4695
+ for (const ln of leftNodes) {
4696
+ for (const rn of rightNodes) {
4697
+ flows.push({ from: ln.id, to: rn.id, op, label: edgeMatches[i].label });
4698
+ }
4699
+ }
4700
+ }
4348
4701
  continue;
4349
4702
  }
4350
4703
  let match;
4704
+ let matchedNode = false;
4705
+ explicitNodeRe.lastIndex = 0;
4351
4706
  while ((match = explicitNodeRe.exec(line)) !== null) {
4352
- const rawId = match[1];
4707
+ matchedNode = true;
4708
+ const id = sanitizeId(match[1]);
4353
4709
  const rawBody = match[2];
4354
- const id = sanitizeId(rawId);
4355
- const bracket = rawBody.slice(0, 2);
4710
+ const bracket = extractMermaidBracket(rawBody);
4356
4711
  const label = cleanLabel(rawBody) || id;
4357
- const kind = inferKindFromMermaid(id, label, bracket);
4358
- nodes.set(id, { id, label, kind });
4359
- if (currentSubgraph && !currentSubgraph.members.includes(id)) {
4360
- currentSubgraph.members.push(id);
4361
- }
4712
+ registerNode(id, label, bracket);
4362
4713
  }
4363
- let remainingLine = line;
4364
- while (true) {
4365
- const fMatch = flowPattern.exec(remainingLine);
4366
- if (!fMatch) break;
4367
- const from = sanitizeId(fMatch[1]);
4368
- const arrow = fMatch[2];
4369
- const inlineLabel = fMatch[3];
4370
- const pipeLabel = fMatch[4];
4371
- const to = sanitizeId(fMatch[5]);
4372
- const label = pipeLabel ? cleanLabel(pipeLabel) : inlineLabel ? cleanLabel(inlineLabel) : void 0;
4373
- if (!nodes.has(from)) nodes.set(from, { id: from, label: from, kind: inferKindFromMermaid(from, from) });
4374
- if (!nodes.has(to)) nodes.set(to, { id: to, label: to, kind: inferKindFromMermaid(to, to) });
4375
- let op = "->";
4376
- if (arrow.includes("-.->") || arrow.includes("~")) op = "~>";
4377
- flows.push({ from, to, op, label });
4378
- const toIndex = fMatch.index + fMatch[0].lastIndexOf(fMatch[5]);
4379
- if (toIndex === 0) break;
4380
- remainingLine = remainingLine.substring(toIndex);
4714
+ if (!matchedNode && /^[a-zA-Z0-9_-]+$/.test(line)) {
4715
+ registerNode(sanitizeId(line));
4716
+ } else if (!matchedNode) {
4717
+ warnings.push(`Skipped unrecognized Mermaid line: ${line.slice(0, 60)}`);
4381
4718
  }
4382
4719
  }
4720
+ const hasFlowchartShapes = [...nodes.values()].some(
4721
+ (n) => n.kind === "decision" || n.kind === "condition" || n.kind === "start" || n.kind === "end"
4722
+ );
4383
4723
  const out = [];
4384
- out.push(title ? `scene "${title}" theme=paper` : `scene theme=paper`);
4724
+ const typeAttr = hasFlowchartShapes ? " type=flowchart" : "";
4725
+ out.push(title ? `scene "${cleanLabel(title)}"${typeAttr} theme=paper` : `scene${typeAttr} theme=paper`);
4385
4726
  out.push(`layout ${direction}`);
4386
4727
  out.push("");
4387
4728
  for (const g of groups.values()) {
@@ -4393,7 +4734,7 @@ function transpileFlowchart(lines, title) {
4393
4734
  out.push(`${n.kind} ${n.id} "${n.label}"`);
4394
4735
  }
4395
4736
  out.push("");
4396
- out.push('beat main "Render Diagram":');
4737
+ out.push("beat main:");
4397
4738
  out.push(" show $nodes stagger=60ms");
4398
4739
  for (const flow of flows) {
4399
4740
  if (flow.label) {
@@ -4404,9 +4745,11 @@ function transpileFlowchart(lines, title) {
4404
4745
  }
4405
4746
  return {
4406
4747
  code: out.join("\n"),
4407
- diagramType: "architecture",
4748
+ diagramType: hasFlowchartShapes ? "flowchart" : "architecture",
4408
4749
  nodeCount: nodes.size,
4409
- edgeCount: flows.length
4750
+ edgeCount: flows.length,
4751
+ groupCount: groups.size,
4752
+ warnings
4410
4753
  };
4411
4754
  }
4412
4755
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markdy/mcp-server",
3
- "version": "1.5.8",
3
+ "version": "1.6.0",
4
4
  "description": "Model Context Protocol server for Markdy diagram validation, transpilation, and generation.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -39,8 +39,8 @@
39
39
  },
40
40
  "dependencies": {
41
41
  "@modelcontextprotocol/sdk": "^1.6.1",
42
- "@markdy/core": "1.5.8",
43
- "@markdy/compat": "1.5.8"
42
+ "@markdy/compat": "1.6.0",
43
+ "@markdy/core": "1.6.0"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@types/node": "^25.9.5",