yuhwa 1.0.1 → 1.0.2

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.
@@ -589,8 +589,28 @@ function resolveColor(v, theme, fallbackLevel) {
589
589
  const lvl = m[2] ? Number(m[2]) : fallbackLevel;
590
590
  return ramp(m[1], lvl in L ? lvl : fallbackLevel);
591
591
  }
592
- return v;
592
+ return isSafeCss(v) ? v : void 0;
593
593
  }
594
+ /**
595
+ * True when a value can sit inside an SVG attribute or a style rule as is:
596
+ * any CSS colour or font list passes, quotes, angle brackets, `;`, braces,
597
+ * `&` and backslashes do not. Colours outside the palette go through this.
598
+ */
599
+ const isSafeCss = (v) => /^[\w\s#(),.%/+-]*$/.test(v);
600
+ /**
601
+ * True for a link a diagram may carry: http(s), mailto, an anchor or a
602
+ * relative path. Other schemes (`javascript:`, `data:`, …) run code when the
603
+ * SVG is inlined in a page. Browsers drop whitespace and control characters
604
+ * inside a scheme, so the check does too.
605
+ */
606
+ const isSafeLink = (v) => {
607
+ const scheme = v.replace(/[\u0000-\u0020\u007f]/g, "").match(/^([a-z][a-z0-9+.-]*):/i);
608
+ return !scheme || [
609
+ "http",
610
+ "https",
611
+ "mailto"
612
+ ].includes(scheme[1].toLowerCase());
613
+ };
594
614
  // src/core/inline.ts
595
615
  const COLOR_RE = /^(#[0-9a-fA-F]{3,8}|[a-z]+(?:-\d{2,3})?)$/;
596
616
  const INLINE_RE = /\*\*(.+?)\*\*|~~(.+?)~~|`([^`]+)`|\[\[(.+?)\]\]|\{(#[0-9a-fA-F]{3,8}|[a-z]+(?:-\d{2,3})?):[ ]?(.+?)\}|\*(.+?)\*/g;
@@ -1950,6 +1970,7 @@ function checkProps(props, allowed, what, line0, warn, propLines) {
1950
1970
  warn(line, `Unknown ${k} "${v}"${didYouMean(v, opts) || ` (use one of: ${shown.join(", ")})`}`);
1951
1971
  } else if (BOOLS.includes(k) && !["true", "false"].includes(v)) warn(line, `${k} expects true or false, got "${v}"`);
1952
1972
  else if (NUMBERS.includes(k) && !Number.isFinite(parseFloat(v))) warn(line, `${k} expects a number, got "${v}"`);
1973
+ else if (k === "link" && !isSafeLink(v)) warn(line, `link "${v}" is not rendered: use http(s), mailto, an #anchor or a relative path`);
1953
1974
  }
1954
1975
  }
1955
1976
  function lint(d) {
@@ -2643,312 +2664,295 @@ function parse(source, opts = {}) {
2643
2664
  statement(raw, li);
2644
2665
  gen = was;
2645
2666
  };
2646
- const statement = (raw, li) => {
2647
- const top = stack[stack.length - 1];
2648
- if (top?.kind === "body") {
2649
- if (raw.trim() === "}") {
2650
- stack.pop();
2651
- const n = d.nodes.get(top.id);
2652
- n.body = dedent(top.lines);
2653
- n.bodyLines = [top.start, li];
2654
- } else top.lines.push(raw);
2655
- return;
2656
- }
2657
- if (top?.kind === "panel") {
2658
- if (raw.trim() === "}") {
2659
- stack.pop();
2660
- const p = top.panel;
2661
- const lead = top.lines.findIndex((l) => l.trim());
2662
- p.body = dedent(top.lines);
2663
- p.lines = [top.start, li];
2664
- p.data = PANELS.get(p.kind).parse(p.body, p.props, (i, msg) => warn(i < 0 ? top.start : top.start + 1 + Math.max(0, lead) + i, msg));
2665
- } else top.lines.push(raw.slice(0, lex(raw).codeEnd).trimEnd());
2666
- return;
2667
- }
2668
- if (top?.kind === "theme") {
2669
- const code = raw.slice(0, lex(raw).codeEnd).trim();
2670
- if (!code) return;
2671
- if (code === "}") {
2672
- stack.pop();
2673
- defineTheme(top.id, top.props, top.start);
2674
- return;
2675
- }
2676
- const m = code.match(/^([A-Za-z][\w-]*)\s*:\s*(.*)$/);
2677
- if (!m) return void warn(li, `Expected "key: value" in theme ${top.id} (keys: ${THEME_KEY_NAMES.join(", ")})`, "error");
2678
- themeProp(top.props, m[1].toLowerCase(), unquote(m[2].trim()), li);
2679
- return;
2680
- }
2681
- if (top?.kind === "layout") {
2682
- const code = raw.slice(0, lex(raw).codeEnd).trim();
2683
- if (!code) return;
2684
- if (code.startsWith("}")) {
2685
- stack.pop();
2686
- d.layoutBlock[1] = li;
2687
- return;
2688
- }
2689
- layoutLines.set(code.split(/\s+/)[0], li);
2690
- parseLayoutLine(code, li, d, warn);
2691
- return;
2692
- }
2693
- const { tokens, codeEnd } = lex(raw);
2694
- if (!tokens.length) return;
2695
- const t = tokens;
2696
- const foreign = /^\s*%%/.test(raw) ? "Comments start with # or //" : /(--|==|-\.|->|=>)\|[^|]*\|/.test(raw) ? "Edge labels go after a colon: a -> b: label" : void 0;
2697
- if (foreign) {
2698
- warn(li, foreign, "error");
2667
+ /** node body: verbatim lines until a line that is only "}" */
2668
+ const bodyLine = (top, raw, li) => {
2669
+ if (raw.trim() === "}") {
2670
+ stack.pop();
2671
+ const n = d.nodes.get(top.id);
2672
+ n.body = dedent(top.lines);
2673
+ n.bodyLines = [top.start, li];
2674
+ } else top.lines.push(raw);
2675
+ };
2676
+ /** panel body: verbatim too, handed to the panel plugin on close */
2677
+ const panelLine = (top, raw, li) => {
2678
+ if (raw.trim() === "}") {
2679
+ stack.pop();
2680
+ const p = top.panel;
2681
+ const lead = top.lines.findIndex((l) => l.trim());
2682
+ p.body = dedent(top.lines);
2683
+ p.lines = [top.start, li];
2684
+ p.data = PANELS.get(p.kind).parse(p.body, p.props, (i, msg) => warn(i < 0 ? top.start : top.start + 1 + Math.max(0, lead) + i, msg));
2685
+ } else top.lines.push(raw.slice(0, lex(raw).codeEnd).trimEnd());
2686
+ };
2687
+ /** theme body: `key: value` lines */
2688
+ const themeBodyLine = (top, raw, li) => {
2689
+ const code = raw.slice(0, lex(raw).codeEnd).trim();
2690
+ if (!code) return;
2691
+ if (code === "}") {
2692
+ stack.pop();
2693
+ defineTheme(top.id, top.props, top.start);
2699
2694
  return;
2700
2695
  }
2701
- if (t[0].kind === "punct" && t[0].value === "}") {
2702
- if (!stack.length) warn(li, "Unmatched \"}\"", "error");
2703
- else stack.pop();
2704
- lastDecl = null;
2696
+ const m = code.match(/^([A-Za-z][\w-]*)\s*:\s*(.*)$/);
2697
+ if (!m) return void warn(li, `Expected "key: value" in theme ${top.id} (keys: ${THEME_KEY_NAMES.join(", ")})`, "error");
2698
+ themeProp(top.props, m[1].toLowerCase(), unquote(m[2].trim()), li);
2699
+ };
2700
+ const layoutLine = (raw, li) => {
2701
+ const code = raw.slice(0, lex(raw).codeEnd).trim();
2702
+ if (!code) return;
2703
+ if (code.startsWith("}")) {
2704
+ stack.pop();
2705
+ d.layoutBlock[1] = li;
2705
2706
  return;
2706
2707
  }
2707
- const selective = t[1]?.value === "{" && t.some((x) => x.kind === "ident" && x.value === "from");
2708
- if (t[0].kind === "ident" && t[0].value === "import" && (t[1]?.kind === "string" || t.length === 1 || selective)) {
2709
- if (stack.length) warn(li, "import must be at the top level", "error");
2710
- else if (!t[1]) warn(li, "import needs a path: import \"./shared.yuhwa\"", "error");
2711
- else if (!selective) {
2712
- if (t.length > 2) warn(li, `Unexpected "${t[2].value}" after import path`, "error");
2713
- importFile(t[1].value, li);
2714
- } else {
2715
- const names = readImportList(t, li, warn);
2716
- if (names) importFile(names.path, li, names.names);
2717
- }
2718
- return;
2708
+ layoutLines.set(code.split(/\s+/)[0], li);
2709
+ parseLayoutLine(code, li, d, warn);
2710
+ };
2711
+ const closeBlock = (li) => {
2712
+ if (!stack.length) warn(li, "Unmatched \"}\"", "error");
2713
+ else stack.pop();
2714
+ lastDecl = null;
2715
+ };
2716
+ /** import "./shared.yuhwa" | import { api, db as store, .svc } from "./shared.yuhwa" */
2717
+ const importStmt = (t, li, selective) => {
2718
+ if (stack.length) warn(li, "import must be at the top level", "error");
2719
+ else if (!t[1]) warn(li, "import needs a path: import \"./shared.yuhwa\"", "error");
2720
+ else if (!selective) {
2721
+ if (t.length > 2) warn(li, `Unexpected "${t[2].value}" after import path`, "error");
2722
+ importFile(t[1].value, li);
2723
+ } else {
2724
+ const names = readImportList(t, li, warn);
2725
+ if (names) importFile(names.path, li, names.names);
2719
2726
  }
2720
- if (t[0].kind === "ident" && t[0].value === "export" && t.length > 1 && t[1].kind !== "arrow" && t[1].value !== ":") {
2721
- if (stack.length) warn(li, "export must be at the top level", "error");
2722
- const setting = t[1].kind === "ident" && t[2]?.value === ":" && KNOWN_SETTINGS.has(t[1].value.toLowerCase());
2723
- if (setting || t[1].value === "theme" && t[2]?.kind === "ident") {
2724
- const key = t[1].value.toLowerCase();
2725
- if (setting && !d.sharedSettings?.includes(key)) (d.sharedSettings ??= []).push(key);
2726
- return step(raw.slice(0, t[0].start) + " ".repeat(6) + raw.slice(t[0].end), li);
2727
- }
2728
- hasExport = true;
2729
- const list = t.slice(1).every((x) => x.kind === "ident" || x.kind === "class" || x.value === ",");
2730
- if (t[1].value === "def" && t[2]) exported.add(t[2].value);
2731
- else if (t[1].value.startsWith("$") && (t[2]?.value === ":" || t[2]?.value === "=")) exported.add(t[1].value);
2732
- else for (const x of t.slice(1)) if (x.kind === "ident" || x.kind === "class") exported.add(x.kind === "class" ? `.${x.value}` : x.value);
2733
- if (list && !t.slice(1).some((x) => x.kind === "ident" && PANELS.has(x.value))) return;
2727
+ };
2728
+ /** export api, .svc | export api "API" [blue] | export group g { ... } */
2729
+ const exportStmt = (t, raw, li) => {
2730
+ if (stack.length) warn(li, "export must be at the top level", "error");
2731
+ const setting = t[1].kind === "ident" && t[2]?.value === ":" && KNOWN_SETTINGS.has(t[1].value.toLowerCase());
2732
+ if (setting || t[1].value === "theme" && t[2]?.kind === "ident") {
2733
+ const key = t[1].value.toLowerCase();
2734
+ if (setting && !d.sharedSettings?.includes(key)) (d.sharedSettings ??= []).push(key);
2734
2735
  return step(raw.slice(0, t[0].start) + " ".repeat(6) + raw.slice(t[0].end), li);
2735
2736
  }
2736
- const stmt = t[0].kind === "ident" ? STATEMENTS.get(t[0].value) : void 0;
2737
- if (stmt && t[1]?.kind !== "arrow" && t[1]?.value !== ":" && t[1]?.value !== ",") {
2738
- stmt.parse(raw.slice(t[0].end, codeEnd).trim(), d, {
2739
- line: li,
2740
- warn: (msg, sev) => warn(li, msg, sev)
2741
- });
2742
- return;
2743
- }
2744
- if (t[0].kind === "ident" && t[0].value === "theme" && t[1]?.kind === "ident" && (t[2]?.kind === "props" || t[2]?.value === "{")) {
2745
- if (stack.length) warn(li, "theme must be at the top level", "error");
2746
- const props = {};
2747
- let k = 2;
2748
- if (t[k]?.kind === "props") {
2749
- for (const [key, v] of Object.entries(parseProps(t[k].value))) themeProp(props, key, v, li);
2750
- k++;
2751
- }
2752
- if (t[k]?.value === "{") {
2753
- k++;
2754
- if (t[k]?.value === "}") k++;
2755
- else if (k === t.length) return void stack.push({
2756
- kind: "theme",
2757
- id: t[1].value,
2758
- start: li,
2759
- props
2760
- });
2761
- }
2762
- if (k < t.length) warn(li, `Unexpected "${t[k].value}" in theme ${t[1].value}`, "error");
2763
- defineTheme(t[1].value, props, li);
2764
- return;
2737
+ hasExport = true;
2738
+ const list = t.slice(1).every((x) => x.kind === "ident" || x.kind === "class" || x.value === ",");
2739
+ if (t[1].value === "def" && t[2]) exported.add(t[2].value);
2740
+ else if (t[1].value.startsWith("$") && (t[2]?.value === ":" || t[2]?.value === "=")) exported.add(t[1].value);
2741
+ else for (const x of t.slice(1)) if (x.kind === "ident" || x.kind === "class") exported.add(x.kind === "class" ? `.${x.value}` : x.value);
2742
+ if (list && !t.slice(1).some((x) => x.kind === "ident" && PANELS.has(x.value))) return;
2743
+ return step(raw.slice(0, t[0].start) + " ".repeat(6) + raw.slice(t[0].end), li);
2744
+ };
2745
+ /** theme brand [base: midnight] { | theme brand [base: midnight, accent: red] */
2746
+ const themeStmt = (t, li) => {
2747
+ if (stack.length) warn(li, "theme must be at the top level", "error");
2748
+ const props = {};
2749
+ let k = 2;
2750
+ if (t[k]?.kind === "props") {
2751
+ for (const [key, v] of Object.entries(parseProps(t[k].value))) themeProp(props, key, v, li);
2752
+ k++;
2765
2753
  }
2766
- if (t.length > 2 && t[0].kind === "ident" && !PANELS.has(t[0].value) && t[1].kind === "ident" && t[t.length - 1].value === "{" && t[0].value !== "group" && t[0].value !== "layout") warn(li, `"${t[0].value} ${t[1].value} {" reads as two nodes; "${t[0].value}" is not a panel kind (${[...PANELS.keys()].join(", ")})` + (PANEL_ALIASES[t[0].value] && PANELS.has(PANEL_ALIASES[t[0].value].split(" ")[0]) ? `: write "${PANEL_ALIASES[t[0].value].replace("ID", t[1].value)} {"` : ""));
2767
- if (t[0].kind === "ident" && PANELS.has(t[0].value) && t[t.length - 1].value === "{" && (t[1]?.kind === "ident" || t[1]?.kind === "string" || t[1]?.kind === "props" || t[1]?.value === "{")) {
2768
- const kind = t[0].value;
2769
- let k = 1;
2770
- const id = t[k]?.kind === "ident" ? t[k++].value : `${kind}${d.panels.length + 1}`;
2771
- const labelRef = gen ? void 0 : t[k]?.kind === "string" ? {
2772
- line: li,
2773
- at: t[k].start,
2774
- range: [t[k].start, t[k].end]
2775
- } : {
2776
- line: li,
2777
- at: t[k - 1].end
2778
- };
2779
- const label = t[k]?.kind === "string" ? t[k++].value : void 0;
2780
- const ref = {
2781
- line: li,
2782
- insertAt: t[k - 1].end
2783
- };
2784
- let props = {};
2785
- if (t[k]?.kind === "props") {
2786
- props = parseProps(t[k].value);
2787
- ref.props = [t[k].start, t[k].end];
2788
- k++;
2789
- }
2790
- if (props.vertical !== void 0 && props.direction === void 0 && props.vertical !== "false") props.direction = "down";
2791
- delete props.vertical;
2792
- if (k !== t.length - 1) warn(li, `Unexpected "${t[k].value}" in ${kind} header`, "error");
2793
- if (stack.length) warn(li, `${kind} must be at the top level`, "error");
2794
- const panel = {
2795
- kind,
2796
- id,
2797
- label,
2798
- props,
2799
- body: [],
2800
- data: void 0,
2801
- ref: gen ? void 0 : ref,
2802
- labelRef
2803
- };
2804
- d.panels.push(panel);
2805
- stack.push({
2806
- kind: "panel",
2807
- id,
2754
+ if (t[k]?.value === "{") {
2755
+ k++;
2756
+ if (t[k]?.value === "}") k++;
2757
+ else if (k === t.length) return void stack.push({
2758
+ kind: "theme",
2759
+ id: t[1].value,
2808
2760
  start: li,
2809
- lines: [],
2810
- panel
2761
+ props
2811
2762
  });
2812
- return;
2813
2763
  }
2814
- if (t[0].kind === "ident" && t[0].value === "layout" && t[1]?.value === "{") {
2815
- if (stack.length) warn(li, "layout block must be at top level", "error");
2816
- d.layoutBlock = [li, li];
2817
- if (t[2]?.value === "}") return;
2818
- stack.push({ kind: "layout" });
2819
- return;
2764
+ if (k < t.length) warn(li, `Unexpected "${t[k].value}" in theme ${t[1].value}`, "error");
2765
+ defineTheme(t[1].value, props, li);
2766
+ };
2767
+ /** `gantt g {`: looks like a panel of a kind nobody registered */
2768
+ const unknownPanelHint = (t, li) => {
2769
+ warn(li, `"${t[0].value} ${t[1].value} {" reads as two nodes; "${t[0].value}" is not a panel kind (${[...PANELS.keys()].join(", ")})` + (PANEL_ALIASES[t[0].value] && PANELS.has(PANEL_ALIASES[t[0].value].split(" ")[0]) ? `: write "${PANEL_ALIASES[t[0].value].replace("ID", t[1].value)} {"` : ""));
2770
+ };
2771
+ /** sequence id "Title" [props] { — a panel drawn by a plugin */
2772
+ const panelStmt = (t, li) => {
2773
+ const kind = t[0].value;
2774
+ let k = 1;
2775
+ const id = t[k]?.kind === "ident" ? t[k++].value : `${kind}${d.panels.length + 1}`;
2776
+ const labelRef = gen ? void 0 : t[k]?.kind === "string" ? {
2777
+ line: li,
2778
+ at: t[k].start,
2779
+ range: [t[k].start, t[k].end]
2780
+ } : {
2781
+ line: li,
2782
+ at: t[k - 1].end
2783
+ };
2784
+ const label = t[k]?.kind === "string" ? t[k++].value : void 0;
2785
+ const ref = {
2786
+ line: li,
2787
+ insertAt: t[k - 1].end
2788
+ };
2789
+ let props = {};
2790
+ if (t[k]?.kind === "props") {
2791
+ props = parseProps(t[k].value);
2792
+ ref.props = [t[k].start, t[k].end];
2793
+ k++;
2820
2794
  }
2821
- if (t[0].kind === "ident" && t[0].value === "group" && (t[1]?.kind === "ident" || t[1]?.kind === "string" && t.some((x) => x.value === "{"))) {
2822
- let k = 2;
2823
- let id = t[1].value;
2824
- if (t[1].kind === "string") {
2825
- id = t[1].value.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "") || "group";
2826
- k = 1;
2827
- }
2828
- const g = d.groups.get(id) ?? {
2829
- id,
2830
- label: id,
2831
- props: {},
2832
- parent: currentGroup()
2833
- };
2834
- if (t[k]?.kind === "string") {
2835
- const s = t[k++];
2836
- g.label = s.value;
2837
- g.labelRef = {
2838
- line: li,
2839
- at: s.start,
2840
- range: [s.start, s.end],
2841
- id: k === 2 ? id : void 0
2842
- };
2843
- } else g.labelRef ??= {
2844
- line: li,
2845
- at: t[1].end
2846
- };
2847
- if (gen) g.labelRef.generated = true;
2848
- while (t[k]?.kind === "class") (g.classes ??= []).push(t[k++].value);
2849
- const ref = {
2795
+ if (props.vertical !== void 0 && props.direction === void 0 && props.vertical !== "false") props.direction = "down";
2796
+ delete props.vertical;
2797
+ if (k !== t.length - 1) warn(li, `Unexpected "${t[k].value}" in ${kind} header`, "error");
2798
+ if (stack.length) warn(li, `${kind} must be at the top level`, "error");
2799
+ const panel = {
2800
+ kind,
2801
+ id,
2802
+ label,
2803
+ props,
2804
+ body: [],
2805
+ data: void 0,
2806
+ ref: gen ? void 0 : ref,
2807
+ labelRef
2808
+ };
2809
+ d.panels.push(panel);
2810
+ stack.push({
2811
+ kind: "panel",
2812
+ id,
2813
+ start: li,
2814
+ lines: [],
2815
+ panel
2816
+ });
2817
+ };
2818
+ /** layout { ... } */
2819
+ const layoutStmt = (t, li) => {
2820
+ if (stack.length) warn(li, "layout block must be at top level", "error");
2821
+ d.layoutBlock = [li, li];
2822
+ if (t[2]?.value === "}") return;
2823
+ stack.push({ kind: "layout" });
2824
+ };
2825
+ /** group id "Label" [.class ...] [props] { ... } | group "Label" { ... } */
2826
+ const groupStmt = (t, raw, li) => {
2827
+ let k = 2;
2828
+ let id = t[1].value;
2829
+ if (t[1].kind === "string") {
2830
+ id = t[1].value.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "") || "group";
2831
+ k = 1;
2832
+ }
2833
+ const g = d.groups.get(id) ?? {
2834
+ id,
2835
+ label: id,
2836
+ props: {},
2837
+ parent: currentGroup()
2838
+ };
2839
+ if (t[k]?.kind === "string") {
2840
+ const s = t[k++];
2841
+ g.label = s.value;
2842
+ g.labelRef = {
2850
2843
  line: li,
2851
- insertAt: t[k - 1].end,
2852
- generated: gen || void 0
2844
+ at: s.start,
2845
+ range: [s.start, s.end],
2846
+ id: k === 2 ? id : void 0
2853
2847
  };
2854
- if (t[k]?.kind === "props") {
2855
- Object.assign(g.props, parseProps(t[k].value));
2856
- ref.props = [t[k].start, t[k].end];
2857
- k++;
2858
- }
2859
- g.ref = ref;
2860
- lastDecl = {
2861
- indent: indentOf(raw),
2862
- targets: [g]
2863
- };
2864
- d.groups.set(id, g);
2865
- if (t[k]?.value !== "{") {
2866
- warn(li, `Expected "{" after group ${id}`, "error");
2867
- return;
2868
- }
2848
+ } else g.labelRef ??= {
2849
+ line: li,
2850
+ at: t[1].end
2851
+ };
2852
+ if (gen) g.labelRef.generated = true;
2853
+ while (t[k]?.kind === "class") (g.classes ??= []).push(t[k++].value);
2854
+ const ref = {
2855
+ line: li,
2856
+ insertAt: t[k - 1].end,
2857
+ generated: gen || void 0
2858
+ };
2859
+ if (t[k]?.kind === "props") {
2860
+ Object.assign(g.props, parseProps(t[k].value));
2861
+ ref.props = [t[k].start, t[k].end];
2869
2862
  k++;
2870
- const close = t.findIndex((x, idx) => idx >= k && x.value === "}");
2871
- if (close >= 0) {
2872
- for (let j = k; j < close; j++) if (t[j].kind === "ident") {
2873
- const n = touchNode(t[j].value);
2874
- n.group = id;
2875
- }
2876
- return;
2877
- }
2878
- stack.push({
2879
- kind: "group",
2880
- id
2881
- });
2863
+ }
2864
+ g.ref = ref;
2865
+ lastDecl = {
2866
+ indent: indentOf(raw),
2867
+ targets: [g]
2868
+ };
2869
+ d.groups.set(id, g);
2870
+ if (t[k]?.value !== "{") {
2871
+ warn(li, `Expected "{" after group ${id}`, "error");
2882
2872
  return;
2883
2873
  }
2884
- const last = t[t.length - 1];
2885
- if (t[0].kind === "class" && t.slice(1, -1).every((x) => x.kind === "class") && (last.kind === "props" || last.kind === "class")) {
2886
- const base = {};
2887
- for (const x of t.slice(1)) {
2888
- if (x.kind !== "class") continue;
2889
- if (!d.classes[x.value]) warn(li, `Unknown class ".${x.value}"${didYouMean(x.value, Object.keys(d.classes))}`);
2890
- else Object.assign(base, d.classes[x.value]);
2874
+ k++;
2875
+ const close = t.findIndex((x, idx) => idx >= k && x.value === "}");
2876
+ if (close >= 0) {
2877
+ for (let j = k; j < close; j++) if (t[j].kind === "ident") {
2878
+ const n = touchNode(t[j].value);
2879
+ n.group = id;
2891
2880
  }
2892
- d.classes[t[0].value] = {
2893
- ...d.classes[t[0].value],
2894
- ...base,
2895
- ...last.kind === "props" ? parseProps(last.value) : {}
2896
- };
2897
- (d.classLines ??= {})[t[0].value] = li;
2898
- lastDecl = {
2899
- indent: indentOf(raw),
2900
- targets: [{ props: d.classes[t[0].value] }]
2901
- };
2902
2881
  return;
2903
2882
  }
2904
- if (t[0].kind === "ident" && t[1]?.kind === "punct" && t[1].value === ":") {
2905
- const key = propKey(t[0].value);
2906
- let value = raw.slice(t[1].end, codeEnd).trim();
2907
- if (/^(["']).*\1$/.test(value)) value = unescape(value.slice(1, -1));
2908
- const deeper = lastDecl && indentOf(raw) > lastDecl.indent ? lastDecl.targets : null;
2909
- const grp = top?.kind === "group" ? d.groups.get(top.id) : void 0;
2910
- const targets = deeper ?? (grp ? [grp] : null);
2911
- if (targets) {
2912
- for (const x of targets) {
2913
- x.props[key] = value || "true";
2914
- if (x.ref && !gen) (x.ref.propLines ??= {})[key] = li;
2915
- }
2916
- return;
2917
- }
2918
- if (stack.length) warn(li, `Setting "${key}" should be at the top level`);
2919
- if (!KNOWN_SETTINGS.has(key)) {
2920
- const id = t[0].value;
2921
- const node = value.startsWith("{") ? `${id} [${value.slice(1, -1).trim()}]` : `${id} "${value}"`;
2922
- warn(li, `Unknown setting "${key}"${didYouMean(key, [...KNOWN_SETTINGS]) || `. For a node write ${node}`}`);
2923
- return;
2883
+ stack.push({
2884
+ kind: "group",
2885
+ id
2886
+ });
2887
+ };
2888
+ /** .class [props] | .primary .svc [bold]: a class built on other classes | .class alone, props on lines below */
2889
+ const classStmt = (t, raw, li) => {
2890
+ const last = t[t.length - 1];
2891
+ const base = {};
2892
+ for (const x of t.slice(1)) {
2893
+ if (x.kind !== "class") continue;
2894
+ if (!d.classes[x.value]) warn(li, `Unknown class ".${x.value}"${didYouMean(x.value, Object.keys(d.classes))}`);
2895
+ else Object.assign(base, d.classes[x.value]);
2896
+ }
2897
+ d.classes[t[0].value] = {
2898
+ ...d.classes[t[0].value],
2899
+ ...base,
2900
+ ...last.kind === "props" ? parseProps(last.value) : {}
2901
+ };
2902
+ (d.classLines ??= {})[t[0].value] = li;
2903
+ lastDecl = {
2904
+ indent: indentOf(raw),
2905
+ targets: [{ props: d.classes[t[0].value] }]
2906
+ };
2907
+ };
2908
+ /** key: value — a setting, or a prop of the declaration above or the enclosing group */
2909
+ const settingStmt = (t, raw, li, codeEnd, top) => {
2910
+ const key = propKey(t[0].value);
2911
+ let value = raw.slice(t[1].end, codeEnd).trim();
2912
+ if (/^(["']).*\1$/.test(value)) value = unescape(value.slice(1, -1));
2913
+ const deeper = lastDecl && indentOf(raw) > lastDecl.indent ? lastDecl.targets : null;
2914
+ const grp = top?.kind === "group" ? d.groups.get(top.id) : void 0;
2915
+ const targets = deeper ?? (grp ? [grp] : null);
2916
+ if (targets) {
2917
+ for (const x of targets) {
2918
+ x.props[key] = value || "true";
2919
+ if (x.ref && !gen) (x.ref.propLines ??= {})[key] = li;
2924
2920
  }
2925
- if (key === "direction") value = DIRECTION_ALIASES[value.toLowerCase()] ?? value;
2926
- if (key === "theme") themeLine = li;
2927
- const allowed = key === "theme" ? void 0 : SETTING_VALUES[key];
2928
- if (allowed && value && !value.includes("$") && !allowed.includes(value)) warn(li, `Unknown ${key} "${value}"${didYouMean(value, allowed) || ` (use one of: ${allowed.join(", ")})`}`);
2929
- d.settings[key] = value;
2930
2921
  return;
2931
2922
  }
2932
- if (t.some((x) => x.kind === "arrow")) {
2933
- const ne = d.edges.length;
2934
- parseEdgeStatement(t, li, raw.slice(0, codeEnd), d, touchNode, edgeCount, warn, relabel);
2935
- lastDecl = {
2936
- indent: indentOf(raw),
2937
- targets: d.edges.slice(ne)
2938
- };
2939
- if (gen) {
2940
- for (const e of d.edges.slice(ne)) {
2941
- if (e.ref) e.ref.generated = true;
2942
- if (e.labelRef) e.labelRef.generated = true;
2943
- }
2944
- for (const x of t) if (x.kind === "ident") {
2945
- const n = d.nodes.get(x.value);
2946
- if (n?.ref?.line === li) n.ref.generated = true;
2947
- if (n?.labelRef?.line === li) n.labelRef.generated = true;
2948
- }
2949
- }
2923
+ if (stack.length) warn(li, `Setting "${key}" should be at the top level`);
2924
+ if (!KNOWN_SETTINGS.has(key)) {
2925
+ const id = t[0].value;
2926
+ const node = value.startsWith("{") ? `${id} [${value.slice(1, -1).trim()}]` : `${id} "${value}"`;
2927
+ warn(li, `Unknown setting "${key}"${didYouMean(key, [...KNOWN_SETTINGS]) || `. For a node write ${node}`}`);
2950
2928
  return;
2951
2929
  }
2930
+ if (key === "direction") value = DIRECTION_ALIASES[value.toLowerCase()] ?? value;
2931
+ if (key === "theme") themeLine = li;
2932
+ const allowed = key === "theme" ? void 0 : SETTING_VALUES[key];
2933
+ if (allowed && value && !value.includes("$") && !allowed.includes(value)) warn(li, `Unknown ${key} "${value}"${didYouMean(value, allowed) || ` (use one of: ${allowed.join(", ")})`}`);
2934
+ d.settings[key] = value;
2935
+ };
2936
+ const edgeStmt = (t, raw, li, codeEnd) => {
2937
+ const ne = d.edges.length;
2938
+ parseEdgeStatement(t, li, raw.slice(0, codeEnd), d, touchNode, edgeCount, warn, relabel);
2939
+ lastDecl = {
2940
+ indent: indentOf(raw),
2941
+ targets: d.edges.slice(ne)
2942
+ };
2943
+ if (!gen) return;
2944
+ for (const e of d.edges.slice(ne)) {
2945
+ if (e.ref) e.ref.generated = true;
2946
+ if (e.labelRef) e.labelRef.generated = true;
2947
+ }
2948
+ for (const x of t) if (x.kind === "ident") {
2949
+ const n = d.nodes.get(x.value);
2950
+ if (n?.ref?.line === li) n.ref.generated = true;
2951
+ if (n?.labelRef?.line === li) n.labelRef.generated = true;
2952
+ }
2953
+ };
2954
+ /** node declarations: id "Label" .cls [props] (several per line allowed) */
2955
+ const nodeStmt = (t, raw, li) => {
2952
2956
  let k = 0;
2953
2957
  let sep = true;
2954
2958
  while (k < t.length) {
@@ -3014,6 +3018,39 @@ function parse(source, opts = {}) {
3014
3018
  };
3015
3019
  }
3016
3020
  };
3021
+ /** one line after substitution: picks the handler; the order of the checks matters */
3022
+ const statement = (raw, li) => {
3023
+ const top = stack[stack.length - 1];
3024
+ if (top?.kind === "body") return bodyLine(top, raw, li);
3025
+ if (top?.kind === "panel") return panelLine(top, raw, li);
3026
+ if (top?.kind === "theme") return themeBodyLine(top, raw, li);
3027
+ if (top?.kind === "layout") return layoutLine(raw, li);
3028
+ const { tokens: t, codeEnd } = lex(raw);
3029
+ if (!t.length) return;
3030
+ const foreign = /^\s*%%/.test(raw) ? "Comments start with # or //" : /(--|==|-\.|->|=>)\|[^|]*\|/.test(raw) ? "Edge labels go after a colon: a -> b: label" : void 0;
3031
+ if (foreign) return void warn(li, foreign, "error");
3032
+ if (t[0].kind === "punct" && t[0].value === "}") return closeBlock(li);
3033
+ const head = t[0].kind === "ident" ? t[0].value : void 0;
3034
+ const selective = t[1]?.value === "{" && t.some((x) => x.kind === "ident" && x.value === "from");
3035
+ if (head === "import" && (t[1]?.kind === "string" || t.length === 1 || selective)) return importStmt(t, li, selective);
3036
+ if (head === "export" && t.length > 1 && t[1].kind !== "arrow" && t[1].value !== ":") return exportStmt(t, raw, li);
3037
+ const stmt = head !== void 0 ? STATEMENTS.get(head) : void 0;
3038
+ if (stmt && t[1]?.kind !== "arrow" && t[1]?.value !== ":" && t[1]?.value !== ",") return void stmt.parse(raw.slice(t[0].end, codeEnd).trim(), d, {
3039
+ line: li,
3040
+ warn: (msg, sev) => warn(li, msg, sev)
3041
+ });
3042
+ if (head === "theme" && t[1]?.kind === "ident" && (t[2]?.kind === "props" || t[2]?.value === "{")) return themeStmt(t, li);
3043
+ const opensBlock = t[t.length - 1].value === "{";
3044
+ if (head !== void 0 && t.length > 2 && !PANELS.has(head) && t[1].kind === "ident" && opensBlock && head !== "group" && head !== "layout") unknownPanelHint(t, li);
3045
+ if (head !== void 0 && PANELS.has(head) && opensBlock && (t[1]?.kind === "ident" || t[1]?.kind === "string" || t[1]?.kind === "props" || t[1]?.value === "{")) return panelStmt(t, li);
3046
+ if (head === "layout" && t[1]?.value === "{") return layoutStmt(t, li);
3047
+ if (head === "group" && (t[1]?.kind === "ident" || t[1]?.kind === "string" && t.some((x) => x.value === "{"))) return groupStmt(t, raw, li);
3048
+ const last = t[t.length - 1];
3049
+ if (t[0].kind === "class" && t.slice(1, -1).every((x) => x.kind === "class") && (last.kind === "props" || last.kind === "class")) return classStmt(t, raw, li);
3050
+ if (head !== void 0 && t[1]?.kind === "punct" && t[1].value === ":") return settingStmt(t, raw, li, codeEnd, top);
3051
+ if (t.some((x) => x.kind === "arrow")) return edgeStmt(t, raw, li, codeEnd);
3052
+ nodeStmt(t, raw, li);
3053
+ };
3017
3054
  for (let li = 0; li < lines.length; li++) step(lines[li], li);
3018
3055
  for (const ctx of stack) if (ctx.kind === "group") warn(lines.length - 1, `Group "${ctx.id}" is missing "}"`, "error");
3019
3056
  else if (ctx.kind === "body") warn(ctx.start, `Body of "${ctx.id}" is missing a closing "}" line`, "error");
@@ -6756,7 +6793,8 @@ function parseQuadrant(body, _props, warn) {
6756
6793
  { text: "" },
6757
6794
  { text: "" }
6758
6795
  ],
6759
- points: []
6796
+ points: [],
6797
+ scale: 1
6760
6798
  };
6761
6799
  const raw = [];
6762
6800
  body.forEach((line0, i) => {
@@ -6788,6 +6826,7 @@ function parseQuadrant(body, _props, warn) {
6788
6826
  });
6789
6827
  });
6790
6828
  const scale = raw.some((p) => p.x > 1 || p.y > 1) ? 100 : 1;
6829
+ out.scale = scale;
6791
6830
  const seen = /* @__PURE__ */ new Set();
6792
6831
  raw.forEach((p, k) => {
6793
6832
  let x = p.x / scale;
@@ -6805,7 +6844,8 @@ function parseQuadrant(body, _props, warn) {
6805
6844
  id,
6806
6845
  x,
6807
6846
  y,
6808
- tone: p.tone
6847
+ tone: p.tone,
6848
+ line: p.line
6809
6849
  });
6810
6850
  });
6811
6851
  return out;
@@ -6819,6 +6859,26 @@ function pointLines(body) {
6819
6859
  });
6820
6860
  return out;
6821
6861
  }
6862
+ /** Replace just the numeric tokens, retaining labels, colour, spacing and percent signs. */
6863
+ function movePoint(body, q, point, x, y) {
6864
+ const out = [...body];
6865
+ const round = (v) => Number(v.toFixed(5));
6866
+ const normalize = q.scale === 100 && round(x * 100) <= 1 && round(y * 100) <= 1 && q.points.every((p) => p === point || p.x <= .01 && p.y <= .01);
6867
+ for (const p of q.points) {
6868
+ if (p !== point && !normalize) continue;
6869
+ const line = body[p.line];
6870
+ const [core] = splitTrailingProps(line, onlyColor);
6871
+ const match = new RegExp(POINT_RE, "d").exec(core.trimEnd());
6872
+ if (!match?.indices?.[3] || !match.indices[4]) return null;
6873
+ const [a, b] = match.indices[3];
6874
+ const [c, d] = match.indices[4];
6875
+ const scale = normalize ? 1 : q.scale;
6876
+ const nx = round((p === point ? x : p.x) * scale);
6877
+ const ny = round((p === point ? y : p.y) * scale);
6878
+ out[p.line] = line.slice(0, a) + nx + line.slice(b, c) + ny + line.slice(d);
6879
+ }
6880
+ return out;
6881
+ }
6822
6882
  const geometry$1 = perPass(function geometry(q, ctx) {
6823
6883
  const { theme, measure } = ctx;
6824
6884
  const fs = ctx.fs * .88;
@@ -7041,7 +7101,8 @@ const quadrant$1 = {
7041
7101
  part: p.id,
7042
7102
  x: px + G.pts[i].x,
7043
7103
  y: box.y + G.pts[i].y,
7044
- title: `move ${p.name}`
7104
+ title: `move ${p.name}`,
7105
+ editPosition: (body, offset) => movePoint(body, q, p, Math.min(1, Math.max(0, p.x + offset.x / G.S)), Math.min(1, Math.max(0, p.y - offset.y / G.S)))
7045
7106
  })), {
7046
7107
  part: "size",
7047
7108
  x: px + G.S,
@@ -8707,6 +8768,12 @@ function placePanels(d, theme, measure, graph) {
8707
8768
  }
8708
8769
  // src/core/render.ts
8709
8770
  const esc = (s) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
8771
+ /** wrap a node in its `link:`; unsafe schemes are dropped (lint reports them) */
8772
+ function withLink(g, link) {
8773
+ if (!link || !isSafeLink(link)) return g;
8774
+ const external = /^https?:/i.test(link.trim());
8775
+ return `<a href="${esc(link)}"${external ? " target=\"_blank\" rel=\"noopener\"" : ""}>${g}</a>`;
8776
+ }
8710
8777
  const f = (n) => (Math.round(n * 100) / 100).toString();
8711
8778
  const attr = (name, v) => v === null || v === void 0 || v === "" ? "" : ` ${name}="${typeof v === "number" ? f(v) : esc(v)}"`;
8712
8779
  let counter = 0;
@@ -8909,7 +8976,7 @@ function renderNode(d, n, L, shadowId) {
8909
8976
  }, L.theme));
8910
8977
  parts.push("</g>");
8911
8978
  let g = parts.join("");
8912
- if (p.link) g = `<a href="${esc(p.link)}"${p.link.startsWith("http") ? " target=\"_blank\" rel=\"noopener\"" : ""}>${g}</a>`;
8979
+ g = withLink(g, p.link);
8913
8980
  return g;
8914
8981
  }
8915
8982
  const cx = n.x + n.w / 2;
@@ -8929,7 +8996,7 @@ function renderNode(d, n, L, shadowId) {
8929
8996
  }
8930
8997
  parts.push("</g>");
8931
8998
  let g = parts.join("");
8932
- if (p.link) g = `<a href="${esc(p.link)}"${p.link.startsWith("http") ? " target=\"_blank\" rel=\"noopener\"" : ""}>${g}</a>`;
8999
+ g = withLink(g, p.link);
8933
9000
  return g;
8934
9001
  }
8935
9002
  // src/convert/json.ts
@@ -10911,5 +10978,5 @@ function render(input, opts = {}) {
10911
10978
  }
10912
10979
  // src/version.ts
10913
10980
  /** the package version, inlined by the build */
10914
- const VERSION = "1.0.1";
10981
+ const VERSION = "1.0.2";
10915
10982
  export { PANEL_PROPS as C, TONES as D, THEME_NAMES as E, themeNames as O, NUMBERS as S, THEME_KEYS as T, BOOLS as _, canvasMeasure as a, GROUP_PROPS as b, KNOWN_SETTINGS as c, blockBraces as d, isYuhwaPath as f, serializeProps as g, parse as h, isMermaid as i, PANELS as l, lex as m, render as n, registry as o, joinPath as p, toSource as r, diskResolve as s, VERSION as t, SETTING_VALUES as u, EDGE_PROPS as v, SHAPES$2 as w, NODE_PROPS as x, ENUMS as y };