微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么 MathJax.startup.promise 未定义?

如何解决为什么 MathJax.startup.promise 未定义?

我正在尝试让 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)));
        });
    }
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。