XyJax-v3 icon indicating copy to clipboard operation
XyJax-v3 copied to clipboard

Stab at getting xypic working on the server-side

Open artagnon opened this issue 3 years ago • 1 comments

Hi Isao,

I've made some progress trying to get xypic working with node.js, but the output is broken when using JSOM. Here's my code:

import { readFileSync, writeFileSync } from 'fs';
import glob from 'glob';

import { AllPackages } from 'mathjax-full/js/input/tex/AllPackages.js';
import { MathJax } from 'mathjax-full/js/components/startup.js';
import 'mathjax-full/js/util/entities/all.js';

const source = require('mathjax-full/components/src/source.js').source;

// Configure MathJax, with everything except startup.document
MathJax.config = {
    loader: {
        paths: { mathjax: 'mathjax-full/es5', custom: '.' },
        require: require,
        source: source,
        load: ['input/tex', 'output/chtml', '[custom]/jsdomAdaptor', '[custom]/xypic'],
        failed: err => console.log(err)
    },
    tex: {
        packages: AllPackages.concat('xypic'),
        inlineMath: [['$', '$']]
    },
    startup: {
        input: ['tex'],
        output: 'chtml',
        elements: ['.mathjax', 'li > a', 'h1', 'h2', 'h3'],
        handler: 'HTML'
    },
    JSDOM: require('jsdom').JSDOM
};

glob('**/*.html', { "ignore": ['node_modules/**/*', 'lib/**/*'] }, (_, res) =>
    res.forEach(r => {
        // Import the loader and extract the new MathJax object out of it
        import('mathjax-full/js/components/loader.js').then(m => {
            MathJax = m.MathJax;
            // First, load the tex, chtml, svg, and liteDOM packages
            MathJax.loader.load('input/tex', 'output/chtml', '[custom]/jsdomAdaptor').then(_ => {
                // Set the various fields in MathJax.startup, as required by xypic
                MathJax.startup.getComponents();

                // Now load xypic, which installs a render-hook
                MathJax.loader.load('[custom]/xypic').then(_ => {
                    // Read in the HTML file
                    const html = MathJax.startup.getDocument(readFileSync(r, 'utf8'));

                    // xypic has used the adaptor
                    const adaptor = MathJax.startup.adaptor;

                    // Typeset the document, with the render hooks that xypic has put in place
                    html.render();

                    //  Remove the stylesheet
                    adaptor.remove(html.outputJax.chtmlStyles);

                    // Output the resulting HTML in-place
                    writeFileSync(r, adaptor.doctype(html.document) + adaptor.outerHTML(adaptor.root(html.document)));
                }).catch(err => console.log(err))
            }).catch(err => console.log(err))
        }).catch(err => console.log(err));
    }));```

artagnon avatar Mar 15 '21 20:03 artagnon