Re-enable math

single-block-api
Peter Stuifzand 3 years ago
parent 3f922fffd2
commit 2a5d3f2498

@ -21,13 +21,13 @@ import moment from 'moment'
// import 'prismjs/components/prism-jq'
import MD from './markdown'
import he from 'he'
// import {all, create} from 'mathjs'
// import formulaFunctions from './formula'
import {all, create} from 'mathjs'
import formulaFunctions from './formula'
moment.locale('nl')
// const math = create(all)
// math.import(formulaFunctions)
const math = create(all)
math.import(formulaFunctions)
function isMultiline(input) {
return input.value.startsWith("```", 0)
@ -90,7 +90,7 @@ function addIndicator(editor, indicator) {
return true
})
.catch(reason => {
console.warn('error while saving: '+ reason)
console.warn('error while saving: ' + reason)
indicator.addError('error!')
indicator.done();
})
@ -181,13 +181,14 @@ function Editor(holder, input) {
}
function transformTable(editor, id, element) {
Promise.any()
editor.treeForId(id).then(tree => {
let header = _.find(tree[0].children, c => c.text === 'headers') || []
let rows = _.find(tree[0].children, c => c.text === 'rows') || []
let rowData = _.map(rows.children, row => {
// FIXME: Use real parse link function
// FIXME: Use a real parse link function
let page = row.text.substring(2).substring(0, row.text.length - 4)
return fetch('/' + page + '?format=metakv')
.then(res => res.json())
@ -230,6 +231,23 @@ function Editor(holder, input) {
})
}
function transformMathExpression(converted, scope) {
try {
let expr = converted.substring(1);
let parsedExpr = math.parse(expr)
let compiled = parsedExpr.compile()
let evaluated = compiled.evaluate(scope);
if (parsedExpr.isAssignmentNode) {
converted = parsedExpr.object.name + " = <i>" + evaluated.toString() + "</i>"
} else {
converted = expr + " = <i>" + evaluated.toString() + "</i>"
}
} catch (e) {
converted = converted + ' <span style="background: red; color: white;">' + e.message + '</span>';
}
return converted;
}
function transform(text, element, id, editor, scope) {
if (text === undefined) {
return;
@ -248,11 +266,7 @@ function Editor(holder, input) {
} else if (converted.startsWith("```", 0) || converted.startsWith("$$", 0)) {
converted = MD.render(converted)
} else if (converted.startsWith("=", 0)) {
// try {
// converted = math.evaluate(converted.substring(1), scope).toString()
// } catch (e) {
// converted = converted + ' <span style="background: red; color: white;">' + e.message + '</span>';
// }
converted = transformMathExpression(converted, scope);
} else {
let re = /^([A-Z0-9 ]+)::\s*(.+)$/i;
let res = text.match(re)
@ -274,6 +288,7 @@ function Editor(holder, input) {
element.toggleClass('todo--done', todo === false)
element.toggleClass('todo--todo', todo === true)
}
element.html(converted)
}
@ -535,9 +550,10 @@ function Editor(holder, input) {
]
})
editor.replaceChildren(id, results)
}).then(function () {
editor.render()
})
})
.then(function () {
editor.render()
})
}
}
});

Loading…
Cancel
Save