Skip to content

Commit 8448689

Browse files
authored
Merge pull request #1286 from mathjax/update/mathmap-config
Improve mathmap configuration
2 parents 81c1fa5 + c570b6b commit 8448689

File tree

12 files changed

+89
-43
lines changed

12 files changed

+89
-43
lines changed

components/mjs/a11y/speech/speech.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@ import {SpeechHandler} from '#js/a11y/speech.js';
77

88
if (MathJax.loader) {
99
let path = Package.resolvePath('[sre]', false);
10+
let maps = Package.resolvePath('[mathmaps]', false);
1011
if (hasWindow) {
1112
path = new URL(path, location).href;
13+
maps = new URL(maps, location).href;
1214
} else {
1315
const REQUIRE = typeof require !== 'undefined' ? require : MathJax.config.loader.require;
1416
if (REQUIRE?.resolve) {
15-
path = REQUIRE.resolve(`${path}/require.mjs`).replace(/\/[^\/]*$/, '');
17+
path = REQUIRE.resolve(`${path}/package.json`).replace(/\/[^\/]*$/, '');
18+
maps = REQUIRE.resolve(`${maps}/base.json`).replace(/\/[^\/]*$/, '');
1619
} else {
17-
path = '';
20+
path = maps = '';
1821
}
1922
}
2023
if (path) {
21-
combineDefaults(MathJax.config, 'options', { worker: { path } });
24+
combineDefaults(MathJax.config, 'options', { worker: { path, maps } });
2225
}
2326
}
2427

components/mjs/dependencies.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ export const dependencies = {
6767
export const paths = {
6868
tex: '[mathjax]/input/tex/extensions',
6969
mml: '[mathjax]/input/mml/extensions',
70-
sre: '[mathjax]/sre'
70+
sre: '[mathjax]/sre',
71+
mathmaps: '[sre]/mathmaps',
7172
};
7273

7374
export const provides = {

components/mjs/source.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export const source = {
7575
'a11y/complexity': `${src}/a11y/complexity/complexity.js`,
7676
'a11y/explorer': `${src}/a11y/explorer/explorer.js`,
7777
'a11y/sre': `${src}/a11y/sre/sre.js`,
78+
'[mathmaps]': `${src}/../../bundle/sre/mathmaps`,
7879
'ui/lazy': `${src}/ui/lazy/lazy.js`,
7980
'ui/menu': `${src}/ui/menu/menu.js`,
8081
'ui/safe': `${src}/ui/safe/safe.js`,

lab/build/init.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

lab/build/sre.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
import './init.js';
21
export * from '#js/a11y/sre/sre.js';

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,6 @@
170170
"@mathjax/mathjax-newcm-font": "4.0.0-rc.2",
171171
"mhchemparser": "^4.2.1",
172172
"mj-context-menu": "^0.9.1",
173-
"speech-rule-engine": "5.0.0-alpha.6"
173+
"speech-rule-engine": "5.0.0-alpha.7"
174174
}
175175
}

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ts/a11y/speech.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ export function SpeechMathDocumentMixin<
221221
}),
222222
worker: {
223223
path: sreRoot(),
224+
maps: sreRoot().replace(/[cm]js\/a11y\/sre$/, 'bundle/sre/mathmaps'),
224225
worker: 'speech-worker.js',
225226
debug: false,
226227
},

ts/a11y/sre/speech-worker.ts

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,20 @@ declare const SRE: any;
4747
// Set up for node worker_threads versus browser webworker
4848
//
4949
if (typeof self === 'undefined') {
50-
self = global.self = global; // for node, make self be the global object
50+
//
51+
// For node, make self be the global object
52+
//
53+
self = global.self = global;
5154

52-
global.DedicatedWorkerGlobalScope = global.constructor; // so SRE knows we are a worker
55+
//
56+
// This is so SRE knows we are a worker
57+
//
58+
global.DedicatedWorkerGlobalScope = global.constructor;
5359

5460
//
55-
// Create addEventListener() and postMessage() function
61+
// Create addEventListener() and postMessage() functions
5662
//
57-
const { parentPort } = await import(
63+
const { parentPort, workerData } = await import(
5864
/* webpackIgnore: true */ 'node:worker_threads'
5965
);
6066
global.addEventListener = (
@@ -68,25 +74,38 @@ declare const SRE: any;
6874
};
6975

7076
//
71-
// SRE needs require(), and we use it to load mathmaps
77+
// Get the path to the mathmaps
78+
//
79+
global.maps = workerData.maps;
80+
81+
//
82+
// We use require() to load mathmaps in node
7283
//
7384
if (!global.require) {
7485
await import(/* webpackIgnore: true */ './require.mjs');
7586
}
76-
87+
global.getMap = (file: string) =>
88+
Promise.resolve(JSON.stringify(global.require(file)));
89+
} else {
7790
//
78-
// Custom loader for mathmaps
91+
// For web workers, make global be the self object
7992
//
80-
global.SREfeature = {
81-
custom: (locale: string) => {
82-
const file = 'speech-rule-engine/lib/mathmaps/' + locale + '.json';
83-
return Promise.resolve(JSON.stringify(global.require(file)));
84-
},
85-
};
86-
} else {
87-
global = (self as any).global = self; // for web workers make global be the self object
93+
global = (self as any).global = self;
94+
//
95+
// We use fetch() to load mathmaps in web workers
96+
//
97+
global.getMap = (file: string) =>
98+
fetch(file)
99+
.then((data) => data.json())
100+
.catch((err) => console.log(err));
88101
}
89-
global.exports = self; // lets SRE get defined as a global variable
102+
103+
//
104+
// Custom loader for mathmaps
105+
//
106+
global.SREfeature = {
107+
custom: (locale: string) => global.getMap(`${global.maps}/${locale}.json`),
108+
};
90109

91110
//
92111
// Load SRE

ts/a11y/sre/worker_threads.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ declare module 'node:worker_threads' {
33
on(kind: string, listener: (event: Event) => void): void;
44
postMessage(msg: any): void;
55
};
6+
const workerData: any;
67
}

0 commit comments

Comments
 (0)