Skip to content

Commit 57b6ca7

Browse files
authored
Merge pull request #14 from drgrice1/mathjax-4.1.0-no-dark-mode
Add a no-dark-mode extension for MathJax.
2 parents f5cd470 + a4b4e5f commit 57b6ca7

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

htdocs/js/MathJaxConfig/mathjax-config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
if (!window.MathJax) {
22
window.MathJax = {
33
tex: { packages: { '[+]': webworkConfig?.showMathJaxErrors ? [] : ['noerrors'] } },
4-
loader: { load: ['input/asciimath', '[tex]/noerrors'] },
4+
loader: {
5+
load: ['input/asciimath', '[tex]/noerrors', '[no-dark-mode]'],
6+
paths: { 'no-dark-mode': webworkConfig?.mathJaxDarkModeUrl ?? './no-dark-mode.js' }
7+
},
58
startup: {
69
ready() {
710
const AM = MathJax.InputJax.AsciiMath.AM;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
if (MathJax.loader) MathJax.loader.checkVersion('[no-dark-mode]', '4.1.0', 'extension');
2+
3+
for (const [immediate, extension, ready] of [
4+
[
5+
MathJax._.ui?.dialog,
6+
'core',
7+
() => {
8+
const { DraggableDialog } = MathJax._.ui.dialog.DraggableDialog;
9+
delete DraggableDialog.styles['@media (prefers-color-scheme: dark)'];
10+
}
11+
],
12+
13+
[
14+
MathJax._.a11y?.explorer,
15+
'a11y/explorer',
16+
() => {
17+
const Region = MathJax._.a11y.explorer.Region;
18+
for (const region of ['LiveRegion', 'HoverRegion', 'ToolTip']) {
19+
Region[region].style.styles['@media (prefers-color-scheme: dark)'] = {};
20+
}
21+
Region.LiveRegion.style.styles['@media (prefers-color-scheme: dark)']['mjx-ignore'] = { ignore: 1 };
22+
MathJax.startup.extendHandler((handler) => {
23+
delete handler.documentClass.speechStyles['@media (prefers-color-scheme: dark) /* explorer */'];
24+
return handler;
25+
});
26+
}
27+
],
28+
29+
[
30+
MathJax._.output?.chtml,
31+
'output/chtml',
32+
() => {
33+
const { CHTML } = MathJax._.output.chtml_ts;
34+
delete CHTML.commonStyles['@media (prefers-color-scheme: dark)'];
35+
const { ChtmlMaction } = MathJax._.output.chtml.Wrappers.maction;
36+
delete ChtmlMaction.styles['@media (prefers-color-scheme: dark) /* chtml maction */'];
37+
}
38+
],
39+
40+
[
41+
MathJax._.output?.svg,
42+
'output/svg',
43+
() => {
44+
const { SVG } = MathJax._.output.svg_ts;
45+
delete SVG.commonStyles['@media (prefers-color-scheme: dark)'];
46+
const { SvgMaction } = MathJax._.output.svg.Wrappers.maction;
47+
delete SvgMaction.styles['@media (prefers-color-scheme: dark) /* svg maction */'];
48+
}
49+
]
50+
]) {
51+
if (immediate) {
52+
ready();
53+
} else {
54+
const config = MathJax.config.loader;
55+
config[extension] ??= {};
56+
config[extension].extraLoads ??= [];
57+
const check = config[extension].checkReady;
58+
config[extension].checkReady = async () => {
59+
if (check) await check();
60+
return ready();
61+
};
62+
}
63+
}

lib/WeBWorK/ContentGenerator.pm

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use MIME::Base64;
3232
use Scalar::Util qw(weaken);
3333
use HTML::Entities;
3434
use Encode;
35-
use Mojo::JSON qw(encode_json);
35+
use Mojo::JSON qw(encode_json true);
3636

3737
use WeBWorK::File::Scoring qw(parse_scoring_file);
3838
use WeBWorK::Localize;
@@ -690,8 +690,12 @@ accessed by JavaScript files to obtain various webwork2 settings.
690690
691691
=cut
692692

693-
sub webwork_js_config ($c) {
694-
return encode_json({ webwork_url => $c->location });
693+
sub webwork_js_config ($c, $showMathJaxErrors = 0) {
694+
return encode_json({
695+
webwork_url => $c->location,
696+
mathJaxDarkModeUrl => getAssetURL($c->ce, 'js/MathJaxConfig/no-dark-mode.js'),
697+
$showMathJaxErrors ? (showMathJaxErrors => true) : ()
698+
});
695699
}
696700

697701
=item warnings()

templates/RPCRenderFormats/default.html.ep

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
% for (@$extra_css_files) {
2020
%= stylesheet $_->{file}
2121
% }
22-
<script>const webworkConfig = <%= $showMathJaxErrors ? '{ showMathJaxErrors: true }' : 'null' %>;</script>
22+
<script>const webworkConfig = <%== $c->webwork_js_config($showMathJaxErrors) %>;</script>
2323
% for (@$third_party_js) {
2424
%= javascript $_->[0], %{ $_->[1] // {} }
2525
% }

0 commit comments

Comments
 (0)