Skip to content

Add mathjs singleton and expose it via API #481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/core/src/api/API.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ImportObject as MathJSImportObject, ImportOptions as MathJSImportOptions } from 'mathjs';
import { SyntaxHighlightingAPI } from 'packages/core/src/api/SyntaxHighlightingAPI';
import type {
ButtonGroupOptions,
Expand Down Expand Up @@ -827,4 +828,12 @@ export abstract class API<Plugin extends IPlugin> {
lineEnd: lineEnd,
});
}

/**
* Import new definitions into the internal mathJS instance.
* For details on how to use, see https://mathjs.org/docs/reference/functions/import.html
*/
public mathJSimport(object: MathJSImportObject | MathJSImportObject[], options?: MathJSImportOptions): void {
this.plugin.internal.math.import(object, options);
}
}
4 changes: 4 additions & 0 deletions packages/core/src/api/InternalAPI.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { MathJsInstance } from 'mathjs';
import type { Moment } from 'moment';
import type { LifecycleHook } from 'packages/core/src/api/API';
import type { FileAPI } from 'packages/core/src/api/FileAPI';
Expand Down Expand Up @@ -28,6 +29,7 @@ import type { ContextMenuItemDefinition, IContextMenu } from 'packages/core/src/
import type { IFuzzySearch } from 'packages/core/src/utils/IFuzzySearch';
import type { IJsRenderer } from 'packages/core/src/utils/IJsRenderer';
import type { MBLiteral } from 'packages/core/src/utils/Literal';
import { createMathJS } from 'packages/core/src/utils/MathJS';
import { mount, unmount } from 'svelte';
import type { z } from 'zod';

Expand Down Expand Up @@ -75,10 +77,12 @@ export const IMAGE_FILE_EXTENSIONS_WITH_DOTS = IMAGE_FILE_EXTENSIONS.map(ext =>
export abstract class InternalAPI<Plugin extends IPlugin> {
readonly plugin: Plugin;
readonly file: FileAPI<Plugin>;
readonly math: MathJsInstance;

constructor(plugin: Plugin, fileAPI: FileAPI<Plugin>) {
this.plugin = plugin;
this.file = fileAPI;
this.math = createMathJS();
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/fields/viewFields/fields/MathVF.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { EvalFunction } from 'mathjs';
import { compile as MathJsCompile } from 'mathjs';
import { AbstractViewField } from 'packages/core/src/fields/viewFields/AbstractViewField';
import type { ViewFieldMountable } from 'packages/core/src/fields/viewFields/ViewFieldMountable';
import type { ViewFieldVariable } from 'packages/core/src/fields/viewFields/ViewFieldVariable';
Expand Down Expand Up @@ -50,7 +49,7 @@ export class MathVF extends AbstractViewField<MathVFResult> {
}
}

this.expression = MathJsCompile(this.expressionStr);
this.expression = this.plugin.internal.math.compile(this.expressionStr);
}

private buildMathJSContext(): Record<string, unknown> {
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/utils/MathJS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { MathJsInstance, ConfigOptions } from 'mathjs';
import { create as MathJSCreate, all } from 'mathjs';

export function createMathJS(): MathJsInstance {
// TODO: we should probably limit the functionality of MathJS
// we don't need full support for matrices, big numbers and all the other high level stuff
const options: ConfigOptions = {};
return MathJSCreate(all, options);
}