prettier-plugin-mmd 0.3.0 → 0.4.1

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/lib/index.mjs +63 -11
  2. package/package.json +2 -2
package/lib/index.mjs CHANGED
@@ -5701,6 +5701,7 @@ function addLinkRules(source, link, rules) {
5701
5701
  expectValue(at, "@");
5702
5702
  rules.set(at.range[0], "none");
5703
5703
  rules.set((link.opening ?? link.arrow).range[0], "none");
5704
+ if (startsLikeArrow(link.id.name, link.type)) rules.set(link.range[0], "none");
5704
5705
  }
5705
5706
  if (link.opening !== null) {
5706
5707
  const label = link.label;
@@ -5720,6 +5721,43 @@ function addLinkRules(source, link, rules) {
5720
5721
  rules.set(closing.range[0], edge);
5721
5722
  }
5722
5723
  /**
5724
+ * Whether an edge id starts as an arrow does (diagrams/flowchart.md, Arrows
5725
+ * and the lexical rule of the edge id; diagrams/agentflow.md, Arrows). Such an
5726
+ * id is an edge id only where no whitespace stands before it, as in
5727
+ * `A[x]--a@-->B`; after whitespace, the lexer reads the arrow and `a@` is its
5728
+ * text, or the input is rejected. So no whitespace is put before it.
5729
+ *
5730
+ * In flowchart these are `--a`, `==a`, `-.a`, `.-a`, with or without the head
5731
+ * `x`, `o`, or `<`, and `~~~a`, which takes no head. In agentflow they are
5732
+ * `--a` (also `---a` and `-->a`), `-.-a`, and `-..-a`, without a head: an id
5733
+ * such as `x--a`, `==a`, `-.a`, or `.-a` is read the same after whitespace.
5734
+ */
5735
+ function startsLikeArrow(id, type) {
5736
+ if (type === "AgentflowLink") return id.startsWith("--") || startsWithDashedDots(id);
5737
+ return id.startsWith("~~~") || startsWithArrowBody(id) || [
5738
+ "x",
5739
+ "o",
5740
+ "<"
5741
+ ].includes(id.charAt(0)) && startsWithArrowBody(id.slice(1));
5742
+ }
5743
+ /** Whether `text` starts with `-`, one or more dots, and `-`: `-.-`, `-..-`. */
5744
+ function startsWithDashedDots(text) {
5745
+ let dots = 0;
5746
+ while (text.charAt(dots + 1) === ".") dots++;
5747
+ return text.startsWith("-") && dots > 0 && text.charAt(dots + 1) === "-";
5748
+ }
5749
+ /** Whether `text` starts with `--`, `==`, `-.`, or dots and then `-`. */
5750
+ function startsWithArrowBody(text) {
5751
+ if ([
5752
+ "--",
5753
+ "==",
5754
+ "-."
5755
+ ].some((start) => text.startsWith(start))) return true;
5756
+ let dots = 0;
5757
+ while (text.charAt(dots) === ".") dots++;
5758
+ return dots > 0 && text.charAt(dots) === "-";
5759
+ }
5760
+ /**
5723
5761
  * Whether a closing bracket of a slanted shape starts with a character that
5724
5762
  * a text run would take after whitespace: `?]` and `=]` (diagrams/flowchart.md,
5725
5763
  * the slanted context). `A[/a/?]` is a trapezoid-bottom with the label `a/`, but in
@@ -5742,7 +5780,7 @@ function takesTextBefore(closing) {
5742
5780
  */
5743
5781
  function labelEdgeRule(source, label) {
5744
5782
  const before = expectToken(findStructuralTokenEndingBefore(source, label.range[0]), "the bracket before a label");
5745
- return [...source.text.slice(before.range[1], label.range[0])].some(isWhitespace) && keepsLeadingWhitespace(label) ? "space" : "none";
5783
+ return [...source.text.slice(before.range[1], label.range[0])].some(isWhitespace) && keepsLeadingWhitespace(label, before) ? "space" : "none";
5746
5784
  }
5747
5785
  /**
5748
5786
  * Whether the whitespace before a label without quotes is kept, as one
@@ -5755,16 +5793,21 @@ function labelEdgeRule(source, label) {
5755
5793
  * with the label `/x/`, `A[/x/]` a lean-right with the label `x`;
5756
5794
  * `A( -x-)` is a rounded, `A(-x-)` an ellipse; `A[/ "x /]` has the
5757
5795
  * label `"x`, and `A[/"x/]` is rejected.
5758
- * - a label whose first word, up to whitespace or a `"`, holds a `@`:
5759
- * `A[x@]` is rejected, because `A[x@` is read as an edge id.
5796
+ * - a label whose first word, up to whitespace or a `"`, holds a `@`, in
5797
+ * agentflow and after the `>` of a flowchart shape: `A>x@]` is rejected,
5798
+ * because `A>x@` is read as an edge id, and `A> x@]` has the label `x@`. In
5799
+ * flowchart, the run of an edge id stops at the brackets and at `|`, so
5800
+ * `A[x@]` and `A-->|x@|B` are accepted with the same label as with the
5801
+ * whitespace, and it is dropped. In agentflow, `A[x@]` is rejected too.
5760
5802
  *
5761
- * The rule is the same for every shape and for the text between `|`, so
5762
- * that it does not depend on which brackets a label has. The label's value
5763
- * is its text without directives, as mermaid.js reads it.
5803
+ * Apart from the `@`, the rule is the same for every shape and for the text
5804
+ * between `|`, so that it does not depend on which brackets a label has. The
5805
+ * label's value is its text without directives, as mermaid.js reads it.
5764
5806
  */
5765
- function keepsLeadingWhitespace(label) {
5807
+ function keepsLeadingWhitespace(label, opening) {
5766
5808
  const first = label.value.charAt(0);
5767
5809
  if (first === "/" || first === "\\" || first === "-" || first === "\"") return true;
5810
+ if (label.type !== "AgentflowLabel" && opening.value !== ">") return false;
5768
5811
  let wordEnd = 0;
5769
5812
  while (wordEnd < label.value.length && !isWhitespace(label.value.charAt(wordEnd)) && label.value.charAt(wordEnd) !== "\"") wordEnd++;
5770
5813
  return label.value.slice(0, wordEnd).includes("@");
@@ -6012,10 +6055,19 @@ function layoutStatement$1(source, statement, end, preservesDescriptionColumns)
6012
6055
  if (!statement.closed) return keptAsWritten(start, end);
6013
6056
  for (const [position, rule] of descriptionOpeningRules(source, start)) rules.set(position, rule);
6014
6057
  break;
6015
- case "AgentflowVertexStatement": walk(statement, (node) => {
6016
- if (node.type === "AgentflowShape") addShapeRules(source, node, rules);
6017
- if (node.type === "AgentflowLink") addLinkRules(source, node, rules);
6018
- });
6058
+ case "AgentflowVertexStatement": {
6059
+ const dataBlockEnds = /* @__PURE__ */ new Set();
6060
+ walk(statement, (node) => {
6061
+ if (node.type === "AgentflowDataBlock") dataBlockEnds.add(node.range[1]);
6062
+ });
6063
+ walk(statement, (node) => {
6064
+ if (node.type === "AgentflowShape") addShapeRules(source, node, rules);
6065
+ if (node.type !== "AgentflowLink") return;
6066
+ addLinkRules(source, node, rules);
6067
+ if (node.id !== null && dataBlockEnds.has(node.range[0])) rules.set(node.range[0], "none");
6068
+ });
6069
+ break;
6070
+ }
6019
6071
  }
6020
6072
  const layout = layoutRange(source, start, end, collectValueNodes(statement, agentflowValueNodeTypes), kept, rules);
6021
6073
  if (statement.type === "AgentflowAccDescrBlockStatement") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-mmd",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "description": "Prettier plugin for the Mermaid diagram syntax (.mmd files and mermaid code blocks in Markdown)",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -37,7 +37,7 @@
37
37
  "prettier": "^3.0.0"
38
38
  },
39
39
  "dependencies": {
40
- "@mmd-toolkit/parser": "0.1.1",
40
+ "@mmd-toolkit/parser": "0.2.1",
41
41
  "marked": "18.1.3"
42
42
  },
43
43
  "devDependencies": {