-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathnode-main.js
96 lines (87 loc) · 3.15 KB
/
node-main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*************************************************************
*
* Copyright (c) 2019-2024 The MathJax Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Load the needed MathJax components
*/
import '../startup/init.js';
import {Loader, CONFIG} from '#js/components/loader.js';
import {MathJax, combineDefaults, combineConfig} from '#js/components/global.js';
import {resolvePath} from '#js/util/AsyncLoad.js';
import '../core/core.js';
import '../adaptors/liteDOM/liteDOM.js';
import {source} from '../source.js';
const path = eval('require("path")'); // get path from node, not webpack
const fs = eval('require("fs").promises');
const dir = MathJax.config.__dirname; // set up by node-main.mjs or node-main.cjs
/*
* Set up the initial configuration
*/
combineDefaults(MathJax.config, 'loader', {
paths: {'mathjax-modern': 'mathjax-modern-font'},
require: eval("(file) => import(file)"), // use dynamic imports
failed: (err) => {throw err} // pass on error message to init()'s catch function
});
combineDefaults(MathJax.config, 'output', {font: 'mathjax-modern'});
/*
* Mark the preloaded components
*/
Loader.preLoad('loader', 'startup', 'core', 'adaptors/liteDOM');
/*
* Set the paths.
*/
if (path.basename(dir) === 'node-main') {
CONFIG.paths.esm = CONFIG.paths.mathjax;
CONFIG.paths.sre = '[esm]/sre/mathmaps';
CONFIG.paths.mathjax = path.resolve(dir, '..', '..', '..', MathJax.config.__js);
combineDefaults(CONFIG, 'source', source);
} else {
CONFIG.paths.mathjax = dir;
}
/*
* Set the asynchronous loader to handle json files
*/
MathJax._.mathjax.mathjax.asyncLoad = function (name) {
const file = resolvePath(name, (name) => path.resolve(CONFIG.paths.mathjax, name));
return file.match(/\.json$/) ?
fs.readFile(file).then((json) => JSON.parse(json)) :
MathJax.config.loader.require(file);
};
/*
* The initialization function. Use as:
*
* require('mathjax').init({ ... }).then((MathJax) => { ... });
*
* or
*
* import {init} from 'mathjax';
* init({...}).then((MathJax) => {...});
*
* where the argument to init() is a MathJax configuration (what would be set as MathJax = {...}).
* The init() function returns a promise that is resolved when MathJax is loaded and ready, and that
* is passed the MathJax global variable when it is called.
*/
const init = (config = {}) => {
combineConfig(MathJax.config, config);
return Loader.load(...CONFIG.load)
.then(() => CONFIG.ready())
.then(() => MathJax) // Pass MathJax global as argument to subsequent .then() calls
.catch(error => CONFIG.failed(error));
}
/*
* Export the init() function
*/
export {init};