Skip to content

Commit 8ed650b

Browse files
committed
2 parents 6ff6030 + 6d98c9b commit 8ed650b

File tree

6 files changed

+61
-4
lines changed

6 files changed

+61
-4
lines changed

astro.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export default defineConfig({
4848
items: [
4949
{ label: 'API', link: '/guides/api/' },
5050
{ label: 'Advanced Use-Cases', link: '/guides/advancedusecases/' },
51+
{ label: 'Customizing MathJS', link: '/guides/custommathjs/' },
5152
],
5253
},
5354
],

src/content/docs/guides/api.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ const button = mb.createButtonMountable(context.file.path, buttonOptions);
179179
mb.wrapInMDRC(button, container, component);
180180
```
181181
182-
If you are daring you can skipt the intermediate objects.
182+
If you are daring you can skip the intermediate objects.
183183
184184
```js
185185
// First we get an instance of the Meta Bind plugin, then we access the API.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
title: Customizing MathJS
3+
description: An example on how to import customizations into mathjs
4+
---
5+
6+
You may find yourself wanting to add functionality to the [math view fields](/obsidian-meta-bind-plugin-docs/reference/viewfields/math/).
7+
And as they use [mathjs](https://mathjs.org/) internally, you actually can!
8+
9+
## Importing new options into mathJS
10+
11+
The mathjs library allows the user to define his own functions and constants, as described in [their documentation](https://mathjs.org/docs/core/extension.html).
12+
13+
To leverage that, Meta Bind exposed its mathjs instance for you to modify.
14+
The most sensible place to do this, is inside a [JS-Engine startup-script](/obsidian-js-engine-plugin-docs/guides/startupscripts/).
15+
This ensures the modifications are loaded early and will be immediately available when the first documents gets rendered.
16+
17+
:::caution
18+
Modifying mathJS via a js-engine codeblock inside a document may cause timing problems and is not recommended!
19+
:::
20+
21+
### Adding a custom function `clamp`
22+
23+
As an example, we defined the `clamp()` function, which is not part of default mathJS, but can be very helpful.
24+
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.
25+
26+
```js
27+
clamp:  (val, min, max) => Math.min(Math.max(min, val), max)
28+
```
29+
30+
Add this definitions inside a JavaScrypt file stored in you Vault and enable that file to be [run as a startup script](/obsidian-js-engine-plugin-docs/guides/startupscripts/).
31+
Inside the file you use the `mathJSimport(dict, options)` function from the API to import you definitions into mathjs.
32+
33+
```js
34+
const mb = engine.getPlugin('obsidian-meta-bind-plugin').api;
35+
36+
mb.mathJSimport({
37+
// we define the function 'clamp'
38+
clamp: (val, min, max) => Math.min(Math.max(min, val), max),
39+
40+
// and a constant 'foo'
41+
foo: 42
42+
});
43+
```
44+
45+
Now you can create a view field using that function.
46+
This example will always display values between 0 and 10, even if `num` gets outside that range.
47+
48+
```meta-bind
49+
VIEW[clamp({num}, 0, 10)]
50+
```
51+
52+
You can also use the new constant `foo`. This will display 52:
53+
54+
```meta-bind
55+
VIEW[foo + 10]
56+
```

src/content/docs/guides/viewFields.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ The first section contains [Bind Targets](/obsidian-meta-bind-plugin-docs/guides
159159
The second section contains the JavaScript code.
160160

161161
When rendered, the JS view field displays the value returned in the JavaScript section just as [JS Engine](https://github.com/mProjectsCode/obsidian-js-engine-plugin) would.
162-
This means you can to things like rendering markdown.
162+
This means you can do things like rendering markdown.
163163

164164
The same variables and APIs that are [available in JS Engine](https://github.com/mProjectsCode/obsidian-js-engine-plugin#api-docs) code blocks are also available in JS view fields.
165165
The bound variables are available in the `context.bound` object.

src/content/docs/reference/buttonActions/inlineJS.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ interface InlineJSButtonAction {
2020
```
2121

2222
The button configuration is available as a **read only** variable in the script as `context.buttonConfig`.
23-
Aditional information about the button is available in the `context.buttonContext` object.
23+
Additional information about the button is available in the `context.buttonContext` object.
2424
See [Button Context](/obsidian-meta-bind-plugin-docs/api/interfaces/buttoncontext/) for more information.
2525

2626
:::tip[Multiline Strings]

src/content/docs/reference/buttonActions/runJavaScript.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface JSButtonAction {
2121
```
2222

2323
The button configuration is available as a **read only** variable in the script as `context.buttonConfig`.
24-
Aditional information about the button is available in the `context.buttonContext` object.
24+
Additional information about the button is available in the `context.buttonContext` object.
2525
See [Button Context](/obsidian-meta-bind-plugin-docs/api/interfaces/buttoncontext/) for more information.
2626
The args is passed to the script as `context.args`.
2727

0 commit comments

Comments
 (0)