Skip to content

Commit

Permalink
Add support for specifying a custom format function within a math blo…
Browse files Browse the repository at this point in the history
…ck (#99)
  • Loading branch information
heyman authored Dec 31, 2023
1 parent b0f3bdd commit 0b6a1a4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/editor/block/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,19 @@ function mathDeco(view) {

// if we got a result from math.js, add the result decoration
if (result !== undefined) {
let format = parser.get("format")

let resultWidget
if (typeof(result) === "string") {
resultWidget = new MathResult(result, result)
} else {
} else if (format !== undefined && typeof(format) === "function") {
try {
resultWidget = new MathResult(format(result), format(result))
} catch (e) {
// suppress any errors
}
}
if (resultWidget === undefined) {
resultWidget = new MathResult(
math.format(result, {
precision: 8,
Expand Down
10 changes: 10 additions & 0 deletions tests/math.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,13 @@ format(1/3, 3)
`)
await expect(page.locator("css=.heynote-math-result")).toHaveText("0.333")
})

test("custom format function", async ({ page }) => {
await heynotePage.setContent(`
∞∞∞math
_format = format
format(x) = _format(x, {notation:"exponential"})
42
`)
await expect(page.locator("css=.heynote-math-result").last()).toHaveText("4.2e+1")
})

0 comments on commit 0b6a1a4

Please sign in to comment.