Skip to content

Commit fdaa4d4

Browse files
Merge pull request #481 from Cube707/feat/mathjs-import
Add `mathjs` singleton and expose it via API
2 parents 96b83f8 + 4d09aab commit fdaa4d4

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

packages/core/src/api/API.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ImportObject as MathJSImportObject, ImportOptions as MathJSImportOptions } from 'mathjs';
12
import { SyntaxHighlightingAPI } from 'packages/core/src/api/SyntaxHighlightingAPI';
23
import type {
34
ButtonGroupOptions,
@@ -827,4 +828,12 @@ export abstract class API<Plugin extends IPlugin> {
827828
lineEnd: lineEnd,
828829
});
829830
}
831+
832+
/**
833+
* Import new definitions into the internal mathJS instance.
834+
* For details on how to use, see https://mathjs.org/docs/reference/functions/import.html
835+
*/
836+
public mathJSimport(object: MathJSImportObject | MathJSImportObject[], options?: MathJSImportOptions): void {
837+
this.plugin.internal.math.import(object, options);
838+
}
830839
}

packages/core/src/api/InternalAPI.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { MathJsInstance } from 'mathjs';
12
import type { Moment } from 'moment';
23
import type { LifecycleHook } from 'packages/core/src/api/API';
34
import type { FileAPI } from 'packages/core/src/api/FileAPI';
@@ -27,6 +28,7 @@ import type { ContextMenuItemDefinition, IContextMenu } from 'packages/core/src/
2728
import type { IFuzzySearch } from 'packages/core/src/utils/IFuzzySearch';
2829
import type { IJsRenderer } from 'packages/core/src/utils/IJsRenderer';
2930
import type { MBLiteral } from 'packages/core/src/utils/Literal';
31+
import { createMathJS } from 'packages/core/src/utils/MathJS';
3032
import { mount, unmount } from 'svelte';
3133
import type { z } from 'zod';
3234

@@ -74,10 +76,12 @@ export const IMAGE_FILE_EXTENSIONS_WITH_DOTS = IMAGE_FILE_EXTENSIONS.map(ext =>
7476
export abstract class InternalAPI<Plugin extends IPlugin> {
7577
readonly plugin: Plugin;
7678
readonly file: FileAPI<Plugin>;
79+
readonly math: MathJsInstance;
7780

7881
constructor(plugin: Plugin, fileAPI: FileAPI<Plugin>) {
7982
this.plugin = plugin;
8083
this.file = fileAPI;
84+
this.math = createMathJS();
8185
}
8286

8387
// eslint-disable-next-line @typescript-eslint/no-explicit-any

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { EvalFunction } from 'mathjs';
2-
import { compile as MathJsCompile } 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';
@@ -50,7 +49,7 @@ export class MathVF extends AbstractViewField<MathVFResult> {
5049
}
5150
}
5251

53-
this.expression = MathJsCompile(this.expressionStr);
52+
this.expression = this.plugin.internal.math.compile(this.expressionStr);
5453
}
5554

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

packages/core/src/utils/MathJS.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { MathJsInstance, ConfigOptions } from 'mathjs';
2+
import { create as MathJSCreate, all } from 'mathjs';
3+
4+
export function createMathJS(): MathJsInstance {
5+
// TODO: we should probably limit the functionality of MathJS
6+
// we don't need full support for matrices, big numbers and all the other high level stuff
7+
const options: ConfigOptions = {};
8+
return MathJSCreate(all, options);
9+
}

0 commit comments

Comments
 (0)