Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions components/mjs/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ if (MathJax.startup) {
if (MathJax.loader) {
const config = MathJax.config.loader;
const {mathjax} = MathJax._.mathjax;
mathjax.asyncLoad = (name) => {
if (name.match(/\.json$/)) {
if (!mathjax.asyncLoad) {
mathjax.asyncLoad = (name) => {
if (name.match(/\.json$/)) {
name = Package.resolvePath(name);
return (config.json || mathjax.json)(name).then((data) => data.default ?? data);
}
return name.substring(0, 5) === 'node:'
? config.require(name)
: MathJax.loader.load(name).then(result => result[0]);
};
mathjax.json = mathjax.context.window
? (file) => fetch(file).then((data) => data.json())
: (file) => import( /* webpackIgnore: true */ file, {with: {type: 'json'}});
return (config.json || mathjax.json)(name).then((data) => data.default ?? data);
}
return name.substring(0, 5) === 'node:'
? config.require(name)
: MathJax.loader.load(name).then(result => result[0]);
};
mathjax.json = mathjax.context.window
? (file) => fetch(file).then((data) => data.json())
: (file) => import( /* webpackIgnore: true */ file, {with: {type: 'json'}});
}
}
4 changes: 3 additions & 1 deletion components/mjs/core/locale.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {Locale} from '#js/util/Locale.js';

Locale.isComponent = true;

for (const location of Object.values(Locale.locations)) {
location[0].replace(/^(?:\.\.\/)*ts\//, '[mathjax]/');
}
8 changes: 7 additions & 1 deletion components/mjs/loader/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ combineDefaults(MathJax.config.loader, 'provides', provides);

let locale = MathJax.config.locale ?? Locale.current;
try { locale = localStorage.getitem('MathJax-locale') ?? locale; } catch (_err) {}
if (MathJax.config.options?.sre) {
combineDefaults(MathJax.config.options, 'sre', { locale });
}
const load = CONFIG.load;
CONFIG.load = [];
Locale.setLocale(locale)
.then(() => Loader.load(...CONFIG.load))
.then(() => Loader.load(...load))
.then(() => CONFIG.ready())
.then(() => CONFIG.load = load)
.catch((message, name) => CONFIG.failed(message, name));
1 change: 1 addition & 0 deletions components/mjs/main/browser/browser.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./browser.js').default;
19 changes: 19 additions & 0 deletions components/mjs/main/browser/browser.d.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type {
COMPONENT_LIST,
ADAPTOR_LIST,
MATHJAX_CONFIG,
MATHJAX_OBJECT,
MATHJAX_COMPONENTS,
ADAPTOR_DOM,
} from '@mathjax/src/mjs/types/mathjax.js';
import type { MathJaxObject } from '@mathjax/src/mjs/components/startup.js';

export function init<
T extends COMPONENT_LIST<ADAPTOR_DOM<A>> = 'startup',
A extends ADAPTOR_LIST = 'browser'
>(
config: MATHJAX_CONFIG<T, A>,
component?: MATHJAX_COMPONENTS<T, A>
): Promise<MATHJAX_OBJECT<T, A>>;

export const MathJax: MathJaxObject;
19 changes: 19 additions & 0 deletions components/mjs/main/browser/browser.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type {
COMPONENT_LIST,
ADAPTOR_LIST,
MATHJAX_CONFIG,
MATHJAX_OBJECT,
MATHJAX_COMPONENTS,
ADAPTOR_DOM,
} from '@mathjax/src/mjs/types/mathjax.js';
import type { MathJaxObject } from '@mathjax/src/mjs/components/startup.js';

export function init<
T extends COMPONENT_LIST<ADAPTOR_DOM<A>> = 'startup',
A extends ADAPTOR_LIST = 'browser'
>(
config: MATHJAX_CONFIG<T, A>,
component?: MATHJAX_COMPONENTS<T, A>
): Promise<MATHJAX_OBJECT<T, A>>;

export const MathJax: MathJaxObject;
20 changes: 20 additions & 0 deletions components/mjs/main/browser/browser.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type {
COMPONENT_LIST,
ADAPTOR_LIST,
MATHJAX_CONFIG,
MATHJAX_OBJECT,
MATHJAX_COMPONENTS,
ADAPTOR_DOM,
} from '@mathjax/src/mjs/types/mathjax.js';
import type { MathJaxObject } from '@mathjax/src/mjs/components/startup.js';

export function init<
T extends COMPONENT_LIST<ADAPTOR_DOM<A>> = 'startup',
A extends ADAPTOR_LIST = 'browser'
>(
config: MATHJAX_CONFIG<T, A>,
component?: MATHJAX_COMPONENTS<T, A>
): Promise<MATHJAX_OBJECT<T, A>>;

export const MathJax: MathJaxObject;

50 changes: 50 additions & 0 deletions components/mjs/main/browser/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { MathJax, combineConfig, combineDefaults } from '#js/components/global.js';
import { Loader } from '#js/components/loader.js';
import { hasWindow } from '#js/util/context.js';

if (hasWindow) {
window.exports ??= {};
}

/**
* This funciton is called below and by ../main.js in order to create
* the needed init() function that is exported for initializing
* MathJax and returning the completed MathJax object. For use in node,
* the nodeSetup function is called to do extra setup (like setting the
* the asyncLoad function and getting the path to the MathJax files), and
* the adaptor will be added to the loading list if one isnt already
* inclkuded there.
*
* @param {() => void} nodeSetup The function to do extra setup for node
* @param {string} adaptor The adaptor to use for node
* @returns {(config} => MathJax} The init() function for export
*/
export function makeInit(nodeSetup = null, adaptor = '') {
Comment thread
dpvc marked this conversation as resolved.
return async function init(config, component = 'startup') {
Loader.preLoaded('loader', 'core');
MathJax.config.loader.failed = (err) => {throw err};
if (hasWindow) {
MathJax.config.loader.paths.mathjax = 'https://cdn.jsdelivr.net/npm/mathjax';
}
if (nodeSetup) {
await nodeSetup();
}
combineConfig(MathJax.config, config);
if (adaptor) {
const CONFIG = MathJax.config.loader;
CONFIG.load ??= [];
if (CONFIG.load.filter((name) => name.substring(0, 9) === 'adaptors/').length === 0) {
CONFIG.load.unshift(adaptor);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I fully understand:
If we give an adaptor to init, it is only used if no other adaptor is already loaded by the component. But what if we have an adaptor that is not in the adaptors/ directory?

@dpvc dpvc Sep 24, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that makeInit() is only called by the component files, not by the user, so adaptor will either be empty or adaptors/liteDOM. This code tries to avoid adding adaptors/liteDOM in the event that the loader.load from the configuration includes an adaptor.

If a third-party adaptor is included in the loader.load array, then adaptors/liteDOM will be added, and so both adaptors will be loaded. In that case, the user may need to tell MathJax.startup which one to use. That can be done in the startup.ready() function in the configuration via the MathJax.startup.useAdaptor() call.

I suppose it would be better to push() the adaptor, so that if there is already a third-part one, then the liteDOM will be added to the end, and since the first one loaded is the one that will be used, that would mean the third-party one would be used. But while the loading is requested in the order given, it is possible for later ones to arrive first, so it might be that you have to do the setAdaptor() call anyway.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you still want to do this?

}
}
MathJax.loader = Loader;
if (component) {
await Loader.load(component);
}
await MathJax.startup?.promise ?? Promise.resolve();
return MathJax;
}
};

export const init = MathJax.init = makeInit();
export default MathJax;
4 changes: 4 additions & 0 deletions components/mjs/main/browser/browser.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import MathJax from './browser.js';

export default MathJax;
export const init = MathJax.init;
15 changes: 15 additions & 0 deletions components/mjs/main/browser/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"copy": {
"to": "[bundle]/main",
"from": ".",
"copy": [
"browser.mjs",
"browser.d.mts",
"browser.d.ts"
]
},
"webpack": {
"name": "main/browser",
"noDTS": true
}
}
1 change: 1 addition & 0 deletions components/mjs/main/browser/json.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const json = (file) => fetch(file).then((data) => data.json())
1 change: 1 addition & 0 deletions components/mjs/main/browser/json.d.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const json: (file: string) => Promise<any>;
18 changes: 18 additions & 0 deletions components/mjs/main/browser/webpack.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const webpack = require('webpack');

module.exports = (pkg) => {
pkg.output.library = {
type: 'commonjs2',
export: ['default'],
};
pkg.plugins.push(
new webpack.NormalModuleReplacementPlugin(
/mjs\/components\/mjs\/json\.js/,
'../../../components/mjs/main/browser/json.cjs',
),
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1,
}),
);
return pkg;
}
15 changes: 15 additions & 0 deletions components/mjs/main/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"copy": {
"to": "[bundle]/main",
"from": ".",
"copy": [
"main.mjs",
"main.d.mts",
"main.d.ts"
]
},
"webpack": {
"name": "main/main",
"noDTS": true
}
}
4 changes: 4 additions & 0 deletions components/mjs/main/json.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let REQUIRE;
try { REQUIRE = eval("require") } catch (_) { REQUIRE = () => {} }
export const json = (file) => Promise.resolve().then(() => REQUIRE(file));

1 change: 1 addition & 0 deletions components/mjs/main/json.d.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const json: (file: string) => Promise<any>;
1 change: 1 addition & 0 deletions components/mjs/main/main.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./main.js').default;
19 changes: 19 additions & 0 deletions components/mjs/main/main.d.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type {
COMPONENT_LIST,
ADAPTOR_LIST,
MATHJAX_CONFIG,
MATHJAX_OBJECT,
MATHJAX_COMPONENTS,
ADAPTOR_DOM,
} from '@mathjax/src/mjs/types/mathjax.js';
import type { MathJaxObject } from '@mathjax/src/mjs/components/startup.js';

export function init<
T extends COMPONENT_LIST<ADAPTOR_DOM<A>> = 'startup',
A extends ADAPTOR_LIST = 'liteDOM'
>(
config: MATHJAX_CONFIG<T, A>,
component?: MATHJAX_COMPONENTS<T, A>
): Promise<MATHJAX_OBJECT<T, A>>;

export const MathJax: MathJaxObject;
19 changes: 19 additions & 0 deletions components/mjs/main/main.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type {
COMPONENT_LIST,
ADAPTOR_LIST,
MATHJAX_CONFIG,
MATHJAX_OBJECT,
MATHJAX_COMPONENTS,
ADAPTOR_DOM,
} from '@mathjax/src/mjs/types/mathjax.js';
import type { MathJaxObject } from '@mathjax/src/mjs/components/startup.js';

export function init<
T extends COMPONENT_LIST<ADAPTOR_DOM<A>> = 'startup',
A extends ADAPTOR_LIST = 'liteDOM'
>(
config: MATHJAX_CONFIG<T, A>,
component?: MATHJAX_COMPONENTS<T, A>
): Promise<MATHJAX_OBJECT<T, A>>;

export const MathJax: MathJaxObject;
19 changes: 19 additions & 0 deletions components/mjs/main/main.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type {
COMPONENT_LIST,
ADAPTOR_LIST,
MATHJAX_CONFIG,
MATHJAX_OBJECT,
MATHJAX_COMPONENTS,
ADAPTOR_DOM,
} from '@mathjax/src/mjs/types/mathjax.js';
import type { MathJaxObject } from '@mathjax/src/mjs/components/startup.js';

export function init<
T extends COMPONENT_LIST<ADAPTOR_DOM<A>> = 'startup',
A extends ADAPTOR_LIST = 'liteDOM'
>(
config: MATHJAX_CONFIG<T, A>,
component?: MATHJAX_COMPONENTS<T, A>
): Promise<MATHJAX_OBJECT<T, A>>;

export const MathJax: MathJaxObject;
12 changes: 12 additions & 0 deletions components/mjs/main/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { MathJax } from '#js/components/global.js';
import { hasWindow } from '#js/util/context.js';
import { setup, adaptor } from './node/setup.js';
import { makeInit } from './browser/browser.js';

if (!hasWindow) {
MathJax.init = makeInit(setup, adaptor);
}

export const init = MathJax.init;
export default MathJax;

4 changes: 4 additions & 0 deletions components/mjs/main/main.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import MathJax from './main.js';

export default MathJax;
export const init = MathJax.init;
17 changes: 17 additions & 0 deletions components/mjs/main/node/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"copy": {
"to": "[bundle]/main",
"from": ".",
"copy": [
"data.cjs",
"data.d.cts",
"node.mjs",
"node.d.mts",
"node.d.ts"
]
},
"webpack": {
"name": "main/node",
"noDTS": true
}
}
14 changes: 14 additions & 0 deletions components/mjs/main/node/data.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let REQUIRE;
try { REQUIRE = eval("require") } catch(_) { REQUIRE = () => {} }
const path = REQUIRE("path");
const fs = REQUIRE("fs");

let source = null;
if (fs.existsSync(path.resolve(__dirname, '..', '..', 'source.js'))) {
source = async () => {return (await import(/* webpackIgnore: true */ '../../source.js')).source};
}

module.exports.REQUIRE = REQUIRE;
module.exports.path = path;
module.exports.source = source;

16 changes: 16 additions & 0 deletions components/mjs/main/node/data.d.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type {
COMPONENT_LIST,
ADAPTOR_LIST,
MATHJAX_CONFIG,
MATHJAX_OBJECT,
MATHJAX_COMPONENTS,
ADAPTOR_DOM,
} from '@mathjax/src/mjs/types/mathjax.js';

export function init<
T extends COMPONENT_LIST<ADAPTOR_DOM<A>> = 'startup',
A extends ADAPTOR_LIST = 'liteDOM'
>(
config: MATHJAX_CONFIG<T, A>,
component?: MATHJAX_COMPONENTS<T, A>
): Promise<MATHJAX_OBJECT<T, A>>;
1 change: 1 addition & 0 deletions components/mjs/main/node/node.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./node.js').default;
19 changes: 19 additions & 0 deletions components/mjs/main/node/node.d.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type {
COMPONENT_LIST,
ADAPTOR_LIST,
MATHJAX_CONFIG,
MATHJAX_OBJECT,
MATHJAX_COMPONENTS,
ADAPTOR_DOM,
} from '@mathjax/src/mjs/types/mathjax.js';
import type { MathJaxObject } from '@mathjax/src/mjs/components/startup.js';

export function init<
T extends COMPONENT_LIST<ADAPTOR_DOM<A>> = 'startup',
A extends ADAPTOR_LIST = 'liteDOM'
>(
config: MATHJAX_CONFIG<T, A>,
component?: MATHJAX_COMPONENTS<T, A>
): Promise<MATHJAX_OBJECT<T, A>>;

export const MathJax: MathJaxObject;
Loading
Loading