@odla-ai/crm 0.10.1 → 0.11.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.
package/dist/index.cjs CHANGED
@@ -109,6 +109,11 @@ __export(index_exports, {
109
109
  setRank: () => setRank,
110
110
  setStage: () => setStage,
111
111
  stageIndexOf: () => stageIndexOf,
112
+ stripTemplateTokens: () => stripTemplateTokens,
113
+ templateLiveVars: () => templateLiveVars,
114
+ templateSectionIssues: () => templateSectionIssues,
115
+ templateSectionNames: () => templateSectionNames,
116
+ templateTokens: () => templateTokens,
112
117
  templateVars: () => templateVars,
113
118
  typeSummary: () => typeSummary,
114
119
  updateRecord: () => updateRecord,
@@ -423,15 +428,74 @@ function canTransition(def, record, to) {
423
428
  }
424
429
 
425
430
  // src/template.ts
426
- var TOKEN_RE = /\{\{\s*([a-zA-Z][a-zA-Z0-9_]*)\s*\}\}/g;
431
+ var NAME = "[a-zA-Z][a-zA-Z0-9_]*";
432
+ var TOKEN_RE = new RegExp(`\\{\\{\\s*([#/]?)\\s*(${NAME})\\s*\\}\\}`, "g");
433
+ var SECTION_RE = new RegExp(`\\{\\{\\s*#\\s*(${NAME})\\s*\\}\\}([\\s\\S]*?)\\{\\{\\s*\\/\\s*\\1\\s*\\}\\}(\\n{0,2})`, "g");
434
+ var MAX_SECTION_PASSES = 10;
435
+ function templateTokens(template) {
436
+ const out = [];
437
+ for (const match of template.matchAll(TOKEN_RE)) {
438
+ out.push({ name: match[2], kind: match[1] === "#" ? "open" : match[1] === "/" ? "close" : "var" });
439
+ }
440
+ return out;
441
+ }
442
+ function stripTemplateTokens(template) {
443
+ return template.replace(TOKEN_RE, "");
444
+ }
445
+ function templateSectionIssues(template) {
446
+ const open = [], issues = [];
447
+ for (const token of templateTokens(template)) {
448
+ if (token.kind === "open") open.push(token.name);
449
+ else if (token.kind === "close") {
450
+ const at = open.lastIndexOf(token.name);
451
+ if (at < 0) issues.push(`{{/${token.name}}} closes a section that was never opened.`);
452
+ else open.splice(at, 1);
453
+ }
454
+ }
455
+ for (const name of open) issues.push(`Close the {{#${name}}} section with {{/${name}}}.`);
456
+ return issues;
457
+ }
458
+ function resolveSections(template, vars) {
459
+ let out = template;
460
+ for (let pass = 0; pass < MAX_SECTION_PASSES; pass++) {
461
+ let changed = false;
462
+ out = out.replace(SECTION_RE, (match, name, inner, trailing, offset, whole) => {
463
+ changed = true;
464
+ if ((vars[name] ?? "").trim()) return inner + trailing;
465
+ const before = whole.slice(0, offset);
466
+ if (!(offset === 0 || before.endsWith("\n"))) return trailing;
467
+ let keep = trailing.slice(1);
468
+ if (before.endsWith("\n\n") && keep.startsWith("\n")) keep = keep.slice(1);
469
+ return keep;
470
+ });
471
+ if (!changed) break;
472
+ }
473
+ return out;
474
+ }
427
475
  function renderTemplate(template, vars) {
428
- return template.replace(TOKEN_RE, (_, name) => vars[name] ?? "");
476
+ return resolveSections(template, vars).replace(TOKEN_RE, (_, kind, name) => kind ? "" : vars[name] ?? "");
429
477
  }
430
478
  function templateVars(template) {
431
479
  const seen = /* @__PURE__ */ new Set();
432
- for (const match of template.matchAll(TOKEN_RE)) seen.add(match[1]);
480
+ for (const token of templateTokens(template)) seen.add(token.name);
433
481
  return [...seen];
434
482
  }
483
+ function templateLiveVars(template, vars) {
484
+ const live = /* @__PURE__ */ new Set(), open = [];
485
+ for (const token of templateTokens(template)) {
486
+ if (token.kind === "open") open.push(token.name);
487
+ else if (token.kind === "close") {
488
+ const at = open.lastIndexOf(token.name);
489
+ if (at >= 0) open.splice(at, 1);
490
+ } else if (open.every((name) => (vars[name] ?? "").trim())) live.add(token.name);
491
+ }
492
+ return [...live];
493
+ }
494
+ function templateSectionNames(template) {
495
+ const tokens = templateTokens(template);
496
+ const plain = new Set(tokens.filter((token) => token.kind === "var").map((token) => token.name));
497
+ return [...new Set(tokens.filter((token) => token.kind !== "var" && !plain.has(token.name)).map((token) => token.name))];
498
+ }
435
499
  function lintTemplate(template, knownVars) {
436
500
  const used = templateVars(template);
437
501
  const known = new Set(knownVars);
@@ -1799,13 +1863,16 @@ ${text}`;
1799
1863
  if (/^\s*(?:\(\s*unused\s*:|Unused\.\s*Switched off\b|(?:TODO|TBD|PLACEHOLDER)\s*:|DRAFT\s*[-:]|\[\s*(?:draft|placeholder)\s*\]|do not (?:send|use) this (?:email|template))/im.test(combined)) {
1800
1864
  issues.push({ code: "internal-copy", message: "Replace internal draft or retirement notes with recipient-facing copy." });
1801
1865
  }
1802
- const tokens = [...new Set([...combined.matchAll(/\{\{(\w+)\}\}/g)].map((match) => match[1]))];
1803
- if (/[{}]/.test(combined.replace(/\{\{\w+\}\}/g, ""))) issues.push({ code: "placeholder-invalid", message: "Fix malformed placeholders; use {{variableName}}." });
1866
+ const tokens = templateVars(combined);
1867
+ const sections = new Set(templateSectionNames(combined));
1868
+ if (/[{}]/.test(stripTemplateTokens(combined))) issues.push({ code: "placeholder-invalid", message: "Fix malformed placeholders; use {{variableName}}, or {{#name}} \u2026 {{/name}} for a section." });
1869
+ for (const message of templateSectionIssues(combined)) issues.push({ code: "section-invalid", message });
1804
1870
  for (const variable of tokens) {
1805
1871
  if (options.allowedVariables && !options.allowedVariables.includes(variable)) issues.push({ code: "variable-unknown", variable, message: `This event does not supply {{${variable}}}.` });
1806
1872
  }
1807
1873
  if (options.values) {
1808
- for (const variable of /* @__PURE__ */ new Set([...tokens, ...options.requiredVariables ?? []])) {
1874
+ for (const name of sections) if (typeof options.values[name] !== "string") issues.push({ code: "value-missing", variable: name, message: `Missing event value: ${name}.` });
1875
+ for (const variable of /* @__PURE__ */ new Set([...templateLiveVars(combined, options.values), ...options.requiredVariables ?? []])) {
1809
1876
  const value = options.values[variable];
1810
1877
  if (typeof value !== "string" || !value.trim() && !options.optionalVariables?.includes(variable)) {
1811
1878
  issues.push({ code: "value-missing", variable, message: `Missing event value: ${variable}.` });