Skip to content

Commit e84db40

Browse files
committed
add example on how to customize mathjs
1 parent 4d09aab commit e84db40

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"startupScripts": [
3+
"mathJScustomisation.js"
4+
]
5+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
num: 5
3+
---
4+
5+
You may find yourself wanting to add functionality to the math-view elements. This file will show you some tricks how you can achieve that.
6+
7+
## Importing new options into mathJS
8+
9+
The math-views internal use the [mathJS](https://mathjs.org/) libary, which allows the user to define his own functions and constants, as described in [their documentation](https://mathjs.org/docs/core/extension.html).
10+
11+
To leverage that, Meta Bind exposed its mathjs instance for you to modify. The most sensible place to do this, is inside a JS-Engine startup-script. This ensures the modifications are loaded early and will be immediately available when the first documents gets rendered.
12+
13+
> [!warning]
14+
> Moddifying mathJS via a js-engine codeblock inside a document may cause timing problems and is not recomended!
15+
16+
17+
### Using a custom function `clamp`
18+
19+
As an example, we defined the `clamp()` function, which is not part of default mathJS, but can be very helpful.
20+
It takes in three parameters, the current value, a minimum and a maximum. It returns the current value as long as its inside the range otherwise the boundary-value.
21+
22+
```js
23+
clamp:  (val, min, max) => Math.min(Math.max(min, val), max)
24+
```
25+
26+
As long as `mathJScustomisation.js` is loads as a startup-script, we can now use this function in any math-view, like shown in this example:
27+
28+
This is a clamped value: `VIEW[clamp({num}, 0, 10)][math]` actual value: `VIEW[{num}][math]`
29+
Test it with the slider
30+
31+
```meta-bind
32+
INPUT[slider(minValue(-5),maxValue(15),addLabels):num]
33+
```
34+

exampleVault/mathJScustomisation.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const mb = engine.getPlugin('obsidian-meta-bind-plugin').api;
2+
3+
// the following shows how to customize meta-binds mathjs
4+
mb.mathJSimport({
5+
// we define the function 'clamp'
6+
clamp: (val, min, max) => Math.min(Math.max(min, val), max),
7+
});

0 commit comments

Comments
 (0)