问题描述
我正在尝试让 MathJaX v3 在节点上运行,这是我拥有的代码:
const fs = require('fs');
const glob = require('glob');
const { AllPackages } = require('mathjax-full/js/input/tex/AllPackages.js');
glob('**/*.html',{ "ignore": ['node_modules/**/*.html'] },(_,res) =>
res.forEach(r => {
const htmlfile = fs.readFileSync(r,'utf8');
MathJax = {
loader: {
paths: { mathjax: 'mathjax-full/js',custom: '.' },require: require,load: ['[custom]/xypic.min.js'],},tex: {
packages: AllPackages.concat('xypic'),inlineMath: [['$','$']]
},startup: {
input: 'tex',output: 'chtml',adaptor: 'liteAdaptor',document: htmlfile,}
}
require('mathjax-full/js/components/startup.js');
MathJax.startup.promise.then(() => {
const adaptor = MathJax.startup.adaptor;
const html = MathJax.startup.html;
// Remove the stylesheet
adaptor.remove(html.outputJax.chtmlStyles);
// Output the resulting HTML
fs.writeFileSync(r,adaptor.doctype(html.document) + adaptor.outerHTML(adaptor.root(html.document)));
});
}));
我收到错误:
mathjax.js:34
MathJax.startup.promise.then(() => {
^
TypeError: Cannot read property 'then' of undefined
我使用 https://github.com/mathjax/MathJax-demos-node 和 MathJax-src 作为参考,但我似乎仍然无法弄清楚。
编辑:我还没有想出如何让自定义扩展在 node.js 上工作,但这是最新进展:
import { readFileSync,writeFileSync } from 'fs';
import glob from 'glob';
import { mathjax } from 'mathjax-full/js/mathjax.js';
import { TeX } from 'mathjax-full/js/input/tex.js';
import { CHTML } from 'mathjax-full/js/output/chtml.js';
import { liteAdaptor } from 'mathjax-full/js/adaptors/liteAdaptor.js';
import { RegisterHTMLHandler } from 'mathjax-full/js/handlers/html.js';
import { AllPackages } from 'mathjax-full/js/input/tex/AllPackages.js';
import { Loader } from 'mathjax-full/js/components/loader.js';
import 'mathjax-full/js/util/entities/all';
MathJax = {
config: {
loader: {
paths: { custom: '.' },load: { '[+]': ['[custom]/xypic.min.js'] },Failed: err => console.log(err)
}
},loader: Loader
};
MathJax.loader.ready('xypic').then(() => {
glob('**/*.html',res) =>
res.forEach(r => {
const htmlfile = readFileSync(r,'utf8');
// Register the HTML handler
const adaptor = liteAdaptor();
RegisterHTMLHandler(adaptor);
// Create a MathJax document
const tex = new TeX({ packages: AllPackages.concat('xypic'),'$']] });
const chtml = new CHTML();
const html = mathjax.document(htmlfile,{ InputJax: tex,OutputJax: chtml });
// Typeset the document
html.render();
// Remove the stylesheet
adaptor.remove(html.outputJax.chtmlStyles);
// Output the resulting HTML
writeFileSync(r,adaptor.doctype(html.document) + adaptor.outerHTML(adaptor.root(html.document)));
}));
},err => console.log(err));
奇怪的是,它什么也没做。
解决方法
似乎 MathJax.startup.promise
只能在 MathJax.startup.ready()
内使用。
见the documentation here。
试试:
MathJax.startup = {
ready() {
MathJax.startup.defaultReady();
MathJax.startup.promise.then(() => {
const adaptor = MathJax.startup.adaptor;
const html = MathJax.startup.html;
// Remove the stylesheet
adaptor.remove(html.outputJax.chtmlStyles);
// Output the resulting HTML
fs.writeFileSync(r,adaptor.doctype(html.document) + adaptor.outerHTML(adaptor.root(html.document)));
});
}
};