@danielsimonjr/mathts-expression 0.5.3 → 0.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.
package/dist/index.js CHANGED
@@ -7948,6 +7948,18 @@ var createNode = /* @__PURE__ */ factory(
7948
7948
  }
7949
7949
  }
7950
7950
  }
7951
+ function dotEscape(s) {
7952
+ return s.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n");
7953
+ }
7954
+ function dotNodeLabel(node) {
7955
+ const t = node.type;
7956
+ if (isConstantNode(node))
7957
+ return `${t}: ${String(node.value)}`;
7958
+ if (isSymbolNode(node)) return `${t}: ${node.name}`;
7959
+ if (isOperatorNode(node)) return `${t}: ${node.op}`;
7960
+ if (isFunctionNode(node)) return `${t}: ${node.name}`;
7961
+ return t;
7962
+ }
7951
7963
  class Node {
7952
7964
  get type() {
7953
7965
  return "Node";
@@ -8218,6 +8230,43 @@ var createNode = /* @__PURE__ */ factory(
8218
8230
  }
8219
8231
  return this._toTex(options);
8220
8232
  }
8233
+ /**
8234
+ * Render this node as Markdown-embeddable math. Display (block) math by
8235
+ * default (`$$…$$`), inline (`$…$`) when `options.inline` is true. Thin
8236
+ * wrapper over toTex(); adds no failure mode beyond toTex's own.
8237
+ * @param {Object} [options]
8238
+ * @return {string}
8239
+ */
8240
+ toMarkdown(options) {
8241
+ const tex = this.toTex(options);
8242
+ return options?.inline ? "$" + tex + "$" : "$$\n" + tex + "\n$$";
8243
+ }
8244
+ /**
8245
+ * Render the AST subtree rooted at this node as a Graphviz digraph: one
8246
+ * DOT node per AST node (label = node type + a value for leaves), with
8247
+ * parent→child edges. Never throws.
8248
+ * @param {Object} [options]
8249
+ * @return {string}
8250
+ */
8251
+ toDOT(options) {
8252
+ const ids = /* @__PURE__ */ new Map();
8253
+ const nodes = [];
8254
+ const edges = [];
8255
+ let counter = 0;
8256
+ this.traverse((node, _path, parent) => {
8257
+ const id = "n" + counter++;
8258
+ ids.set(node, id);
8259
+ nodes.push(` ${id} [label="${dotEscape(dotNodeLabel(node))}"];`);
8260
+ if (parent) {
8261
+ const pid = ids.get(parent);
8262
+ if (pid) edges.push(` ${pid} -> ${id};`);
8263
+ }
8264
+ });
8265
+ const name47 = options?.name ?? "AST";
8266
+ return `digraph ${name47} {
8267
+ ${nodes.concat(edges).join("\n")}
8268
+ }`;
8269
+ }
8221
8270
  /**
8222
8271
  * Render this node to a MathML fragment (the MathML analog of `toTex`).
8223
8272
  * Like `toTex`, this returns the body — wrap it in a `<math>` element with
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@danielsimonjr/mathts-expression",
3
- "version": "0.5.3",
3
+ "version": "0.6.0",
4
4
  "description": "Expression parser and evaluator for MathTS",
5
5
  "author": "Daniel Simon Jr.",
6
6
  "license": "MIT",