Skip to content

Commit 9ad4629

Browse files
committed
Add mathjs singleton and expose it via API
This allows for modification of `mathjs` by the user, mostly to allow for the use of `math.import` to define custom functions
1 parent 09d6371 commit 9ad4629

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
num: 6
3+
---
4+
5+
```js-engine
6+
const mb = engine.getPlugin('obsidian-meta-bind-plugin').api;
7+
const math = mb.getMathjs();
8+
9+
math.import({
10+
clamp: function (val, min, max) {
11+
return Math.min(Math.max(min, val), max);
12+
}
13+
}, {silent:true});
14+
```
15+
16+
This is a clamped value: `VIEW[clamp({num}, 0, 10)][math]`
17+
Test it with the slider
18+
19+
```meta-bind
20+
INPUT[slider(minValue(-5),maxValue(15)):num]
21+
```
22+
23+
24+
> [!Note]
25+
> Please note the second parameter for `math.import`.
26+
> Passing `silent:true` leads to the function <u>not</u> beeing updated on edit! (only on reload of meta-bind)
27+
> Pass `override:true` to override the function everytime you switch between edit and viewing mode.

packages/core/src/api/API.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { MathJsInstance } from 'mathjs';
12
import { SyntaxHighlightingAPI } from 'packages/core/src/api/SyntaxHighlightingAPI';
23
import type {
34
ButtonGroupOptions,
@@ -63,6 +64,7 @@ import { Signal } from 'packages/core/src/utils/Signal';
6364
import { expectType, getUUID } from 'packages/core/src/utils/Utils';
6465
import { validateAPIArgs } from 'packages/core/src/utils/ZodUtils';
6566
import { z } from 'zod';
67+
import { getMathjsSingleton } from 'packages/core/src/utils/Mathjs';
6668

6769
export interface LifecycleHook {
6870
register(cb: () => void): void;
@@ -827,4 +829,12 @@ export abstract class API<Plugin extends IPlugin> {
827829
lineEnd: lineEnd,
828830
});
829831
}
832+
833+
/**
834+
* get the mathjs instance used in math-views. Useful to modify the config and scope.
835+
*
836+
*/
837+
public getMathjs(): MathJsInstance {
838+
return getMathjsSingleton();
839+
}
830840
}

packages/core/src/fields/viewFields/fields/MathVF.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { EvalFunction } from 'mathjs';
2-
import { compile as MathJsCompile } from 'mathjs';
1+
import type { EvalFunction, MathJsInstance } from 'mathjs';
32
import { AbstractViewField } from 'packages/core/src/fields/viewFields/AbstractViewField';
43
import type { ViewFieldMountable } from 'packages/core/src/fields/viewFields/ViewFieldMountable';
54
import type { ViewFieldVariable } from 'packages/core/src/fields/viewFields/ViewFieldVariable';
65
import { ErrorLevel, MetaBindExpressionError } from 'packages/core/src/utils/errors/MetaBindErrors';
76
import { parseLiteral, stringifyUnknown } from 'packages/core/src/utils/Literal';
7+
import { getMathjsSingleton } from 'packages/core/src/utils/Mathjs';
88
import { Signal } from 'packages/core/src/utils/Signal';
99
import { DomHelpers, getUUID } from 'packages/core/src/utils/Utils';
1010

@@ -13,12 +13,15 @@ export class MathVF extends AbstractViewField<unknown> {
1313
expression?: EvalFunction;
1414
expressionStr?: string;
1515
hasError: boolean;
16+
math: MathJsInstance;
1617

1718
hidden: boolean;
1819

1920
constructor(mountable: ViewFieldMountable) {
2021
super(mountable);
2122

23+
this.math = getMathjsSingleton();
24+
2225
this.hidden = false;
2326

2427
this.hasError = false;
@@ -48,7 +51,7 @@ export class MathVF extends AbstractViewField<unknown> {
4851
}
4952
}
5053

51-
this.expression = MathJsCompile(this.expressionStr);
54+
this.expression = this.math.compile(this.expressionStr);
5255
}
5356

5457
private buildMathJSContext(): Record<string, unknown> {

packages/core/src/utils/MathJS.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { MathJsInstance } from 'mathjs';
2+
import { create as MathjsCreate, all } from 'mathjs';
3+
4+
const math = MathjsCreate(all);
5+
6+
export function getMathjsSingleton(): MathJsInstance {
7+
return math;
8+
}

0 commit comments

Comments
 (0)