-
Notifications
You must be signed in to change notification settings - Fork 2
/
wypst.js
65 lines (57 loc) · 1.63 KB
/
wypst.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import init, { parseTree as _parseTree, typstContentTree } from './src/core/pkg';
import utils from './src/utils';
import wasm from './src/core/pkg/core_bg.wasm';
function parseTree(expression, settings) {
expression = expression.trim().replace(/\n/g, ' ');
return _parseTree(expression, settings);
}
function renderToDomTree(expression, options) {
let settings = new utils.Settings(options);
try {
const tree = parseTree(expression, settings);
return utils.buildTree(tree, expression, settings);
} catch (error) {
// Temporary fix so that we actually see errors like "unknown variable: ..."
return utils.renderError(error, error, settings);
}
}
/**
* Renders a Typst expression into the specified DOM element
* @param expression A Typst expression
* @param element The DOM element to render into
* @param options Render options
*/
function render(expression, baseNode, options) {
baseNode.textContent = "";
const node = renderToDomTree(expression, options).toNode();
baseNode.appendChild(node);
};
/**
* Renders a Typst expression into an HTML string
* @param expression A Typst expression
* @param options Render options
*/
function renderToString(expression, options) {
const markup = renderToDomTree(expression, options).toMarkup();
return markup;
}
async function initialize(path) {
if (path) {
await init(path);
} else {
await init(wasm);
}
}
export default {
render,
renderToString,
parseTree,
__typstContentTree: typstContentTree,
initialize,
};
export {
render,
renderToString,
parseTree,
initialize,
};