Skip to content

Commit e9be4a6

Browse files
committed
Merge: MathJax 4 (PR #2818)
2 parents f22ebc5 + f773390 commit e9be4a6

4 files changed

Lines changed: 149 additions & 191 deletions

File tree

js/mathjax_startup.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/***************************************************************
2+
* Implements startup of MathJax v4
3+
***************************************************************/
4+
5+
// Base config options. Will be supplemented by optional parts later
6+
let mathJaxOpts = {
7+
"tex": {
8+
"inlineMath": [
9+
[
10+
"\\(",
11+
"\\)"
12+
]
13+
],
14+
"tags": "none",
15+
"tagSide": "right",
16+
"tagIndent": ".8em",
17+
"packages": {
18+
"[+]": [
19+
"amscd",
20+
"color",
21+
"knowl"
22+
]
23+
}
24+
},
25+
"options": {
26+
"ignoreHtmlClass": "tex2jax_ignore|ignore-math",
27+
"processHtmlClass": "process-math",
28+
},
29+
"chtml": {
30+
"scale": 0.98,
31+
"mtextInheritFont": true
32+
},
33+
"loader": {
34+
"load": [
35+
"input/asciimath",
36+
"[tex]/amscd",
37+
"[tex]/color",
38+
]
39+
}
40+
};
41+
42+
43+
export function startMathJax(opts) {
44+
if(opts.hasWebworkReps || opts.hasSage) {
45+
mathJaxOpts['renderActions'] = {
46+
"findScript": [
47+
10,
48+
function (doc) {
49+
document.querySelectorAll('script[type^="math/tex"]').forEach(function (node) {
50+
var display = !!node.type.match(/; *mode=display/);
51+
var math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
52+
var text = document.createTextNode('');
53+
node.parentNode.replaceChild(text, node);
54+
math.start = { node: text, delim: '', n: 0 };
55+
math.end = { node: text, delim: '', n: 0 };
56+
doc.math.push(math);
57+
});
58+
},
59+
""
60+
]
61+
}
62+
}
63+
64+
if(opts.isReact) {
65+
mathJaxOpts['startup'] = {
66+
typeset: false,
67+
}
68+
} else {
69+
mathJaxOpts['startup'] = {
70+
ready() {
71+
const { Configuration } = MathJax._.input.tex.Configuration;
72+
const configuration = Configuration.create("knowl", {
73+
handler: {
74+
macro: ["knowl"]
75+
}
76+
});
77+
78+
const NodeUtil = MathJax._.input.tex.NodeUtil.default;
79+
80+
function GetArgumentMML(parser, name) {
81+
const arg = parser.ParseArg(name);
82+
if (!NodeUtil.isInferred(arg)) {
83+
return arg;
84+
}
85+
const children = NodeUtil.getChildren(arg);
86+
if (children.length === 1) {
87+
return children[0];
88+
}
89+
const mrow = parser.create("node", "mrow");
90+
NodeUtil.copyChildren(arg, mrow);
91+
NodeUtil.copyAttributes(arg, mrow);
92+
return mrow;
93+
};
94+
95+
const CommandMap = MathJax._.input.tex.TokenMap.CommandMap;
96+
new CommandMap(
97+
"knowl",
98+
{
99+
knowl(parser, name) {
100+
const url = parser.GetArgument(name);
101+
const arg = GetArgumentMML(parser, name);
102+
const mrow = parser.create("node", "mrow", [arg], { tabindex: '0', "data-knowl": url });
103+
parser.Push(mrow);
104+
}
105+
}
106+
);
107+
108+
MathJax.startup.defaultReady();
109+
},
110+
pageReady() {
111+
return MathJax.startup.defaultPageReady().then(rsMathReady);
112+
},
113+
}
114+
}
115+
116+
if(opts.htmlPresentation) {
117+
mathJaxOpts['options']['menuOptions'] = {
118+
"settings": {
119+
"zoom": "Click",
120+
"zscale": "300%"
121+
}
122+
}
123+
}
124+
125+
// Apply the options
126+
window.MathJax = mathJaxOpts;
127+
128+
// Lets Runestone know that MathJax is ready
129+
const runestoneMathReady = new Promise((resolve) => window.rsMathReady = resolve);
130+
window.runestoneMathReady = runestoneMathReady;
131+
}
132+

js/mathjaxknowl3.js

Lines changed: 0 additions & 48 deletions
This file was deleted.

xsl/pretext-common.xsl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,6 @@ $inline-solution-back|$divisional-solution-back|$worksheet-solution-back|$readin
411411
<!-- HTML only, a developer must elect to use this CSS file -->
412412
<xsl:param name="debug.developer.css" select="'no'"/>
413413

414-
<!-- HTML only, testing early-releases of MathJax 4 -->
415-
<!-- See: https://github.com/mathjax/MathJax/releases -->
416-
<!-- https://github.com/mathjax/MathJax-src/releases/tag/4.0.0-alpha.1 -->
417-
<xsl:param name="debug.mathjax4" select="'no'"/>
418-
<xsl:variable name="mathjax4-testing" select="$debug.mathjax4 = 'yes'"/>
419-
420414
<!-- A permanent string parameter to control the creation of -->
421415
<!-- "View Source" knowls, which is a developer task, not a -->
422416
<!-- publisher task (though it could be?). So permanent, but -->

xsl/pretext-html.xsl

Lines changed: 17 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -13147,152 +13147,32 @@ TODO:
1314713147
<!-- Autobold extension is critical for captions (bold'ed) that -->
1314813148
<!-- have mathematics in them (suggested by P. Krautzberger) -->
1314913149
<xsl:template name="mathjax">
13150-
<!-- mathjax configuration -->
13151-
<xsl:element name="script">
13152-
<xsl:text>&#xa;</xsl:text>
13153-
<xsl:text>var runestoneMathReady = new Promise((resolve) => window.rsMathReady = resolve);&#xa;</xsl:text>
13154-
<xsl:text>window.MathJax = </xsl:text>
13155-
<xsl:call-template name="json">
13156-
<xsl:with-param name="content">
13157-
<map xmlns="http://www.w3.org/2005/xpath-functions">
13158-
<map key="tex">
13159-
<array key="inlineMath">
13160-
<array>
13161-
<string>\(</string>
13162-
<string>\)</string>
13163-
</array>
13164-
</array>
13165-
<string key="tags">none</string>
13166-
<string key="tagSide">right</string>
13167-
<string key="tagIndent">.8em</string>
13168-
<map key="packages">
13169-
<array key="[+]">
13170-
<string>base</string>
13171-
<string>ams</string>
13172-
<string>amscd</string>
13173-
<string>color</string>
13174-
<string>newcommand</string>
13175-
<!-- necessary for \text{} in math -->
13176-
<string>textmacros</string>
13177-
<string>knowl</string>
13178-
</array>
13179-
</map>
13180-
</map>
13181-
<map key="options">
13182-
<string key="ignoreHtmlClass">tex2jax_ignore|ignore-math</string>
13183-
<string key="processHtmlClass">process-math</string>
13184-
<xsl:if test="$b-has-webwork-reps or $b-has-sage">
13185-
<map key="renderActions">
13186-
<array key="findScript">
13187-
<number>10</number>
13188-
<raw>
13189-
<xsl:text>function (doc) {&#xa;</xsl:text>
13190-
<xsl:text> document.querySelectorAll('script[type^="math/tex"]').forEach(function(node) {&#xa;</xsl:text>
13191-
<xsl:text> var display = !!node.type.match(/; *mode=display/);&#xa;</xsl:text>
13192-
<xsl:text> var math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);&#xa;</xsl:text>
13193-
<xsl:text> var text = document.createTextNode('');&#xa;</xsl:text>
13194-
<xsl:text> node.parentNode.replaceChild(text, node);&#xa;</xsl:text>
13195-
<xsl:text> math.start = {node: text, delim: '', n: 0};&#xa;</xsl:text>
13196-
<xsl:text> math.end = {node: text, delim: '', n: 0};&#xa;</xsl:text>
13197-
<xsl:text> doc.math.push(math);&#xa;</xsl:text>
13198-
<xsl:text> });&#xa;</xsl:text>
13199-
<xsl:text> }</xsl:text>
13200-
</raw>
13201-
<string></string>
13202-
</array>
13203-
</map>
13204-
</xsl:if>
13205-
</map>
13206-
<map key="chtml">
13207-
<number key="scale">0.98</number>
13208-
<boolean key="mtextInheritFont">true</boolean>
13209-
</map>
13210-
<map key="loader">
13211-
<array key="load">
13212-
<string>input/asciimath</string>
13213-
<string>[tex]/amscd</string>
13214-
<string>[tex]/color</string>
13215-
<string>[tex]/newcommand</string>
13216-
<!-- necessary for \text{} in math -->
13217-
<string>[tex]/textmacros</string>
13218-
<string>[pretext]/mathjaxknowl3.js</string>
13219-
</array>
13220-
<map key="paths">
13221-
<string key="pretext">
13222-
<xsl:value-of select="$html.js.dir"/>
13223-
</string>
13224-
</map>
13225-
</map>
13226-
<map key="startup">
13227-
<xsl:choose>
13228-
<xsl:when test="$b-debug-react">
13229-
<boolean key="typeset">false</boolean>
13230-
</xsl:when>
13231-
<xsl:otherwise>
13232-
<!-- tell Runestone components that MathJax is all loaded -->
13233-
<raw>
13234-
<xsl:text>pageReady() {&#xa;</xsl:text>
13235-
<xsl:text> return MathJax.startup.defaultPageReady().then(function () {&#xa;</xsl:text>
13236-
<xsl:text> console.log("in ready function");&#xa;</xsl:text>
13237-
<xsl:text> rsMathReady();&#xa;</xsl:text>
13238-
<xsl:text> }&#xa;</xsl:text>
13239-
<xsl:text> )}</xsl:text>
13240-
</raw>
13241-
</xsl:otherwise>
13242-
</xsl:choose>
13243-
</map>
13244-
<!-- optional presentation mode gets clickable, large math -->
13245-
<xsl:if test="$b-html-presentation">
13246-
<map key="options">
13247-
<map key="menuOptions">
13248-
<map key="settings">
13249-
<string key="zoom">Click</string>
13250-
<string key="zscale">300%</string>
13251-
</map>
13252-
</map>
13253-
</map>
13254-
</xsl:if>
13255-
</map>
13256-
</xsl:with-param>
13257-
</xsl:call-template>
13258-
<xsl:text>;</xsl:text>
13259-
<xsl:text>&#xa;</xsl:text>
13260-
</xsl:element>
13261-
<!-- mathjax javascript -->
13262-
<xsl:element name="script">
13263-
<!-- probably should be universal, but only adding for MJ 4 -->
13264-
<!-- TODO: make a literal "script" element with this attribute -->
13265-
<xsl:if test="$mathjax4-testing">
13266-
<xsl:attribute name="type">
13267-
<xsl:text>text/javascript</xsl:text>
13268-
</xsl:attribute>
13269-
</xsl:if>
13150+
<!-- MathJax 4 configuration via JavaScript module -->
13151+
<script type="module">
13152+
<xsl:text>import { startMathJax } from './</xsl:text>
13153+
<xsl:value-of select="$html.js.dir"/>
13154+
<xsl:text>/mathjax_startup.js';&#xa;</xsl:text>
13155+
<xsl:text>startMathJax({&#xa;</xsl:text>
13156+
<xsl:text>hasWebworkReps: </xsl:text><xsl:value-of select="$b-has-webwork-reps"/><xsl:text>,&#xa;</xsl:text>
13157+
<xsl:text>hasSage: </xsl:text><xsl:value-of select="$b-has-sage"/><xsl:text>,&#xa;</xsl:text>
13158+
<xsl:text>isReact: </xsl:text><xsl:value-of select="$b-debug-react"/><xsl:text>,&#xa;</xsl:text>
13159+
<xsl:text>htmlPresentation: </xsl:text><xsl:value-of select="$b-html-presentation"/><xsl:text>,&#xa;</xsl:text>
13160+
<xsl:text>});&#xa;</xsl:text>
13161+
</script>
13162+
<!-- MathJax 4 CDN -->
13163+
<script defer="true">
1327013164
<xsl:attribute name="src">
13165+
<xsl:text>https://cdn.jsdelivr.net/npm/mathjax@4/</xsl:text>
1327113166
<xsl:choose>
13272-
<xsl:when test="$mathjax4-testing">
13273-
<xsl:text>https://cdn.jsdelivr.net/npm/mathjax@4/</xsl:text>
13274-
</xsl:when>
13275-
<xsl:otherwise>
13276-
<xsl:text>https://cdn.jsdelivr.net/npm/mathjax@3/es5/</xsl:text>
13277-
</xsl:otherwise>
13278-
</xsl:choose>
13279-
<!-- CHTML is the default, SVG is for debugging -->
13280-
<xsl:choose>
13281-
<!-- SVG filename identical for v3, v4 -->
13282-
<!-- NB: is tex-mml-svg.js new for v4? -->
1328313167
<xsl:when test="$debug.mathjax.svg = 'yes'">
1328413168
<xsl:text>tex-svg.js</xsl:text>
1328513169
</xsl:when>
13286-
<!-- new filename (default) for v4 -->
13287-
<xsl:when test="$mathjax4-testing">
13288-
<xsl:text>tex-mml-chtml.js</xsl:text>
13289-
</xsl:when>
1329013170
<xsl:otherwise>
13291-
<xsl:text>tex-chtml.js</xsl:text>
13171+
<xsl:text>tex-mml-chtml.js</xsl:text>
1329213172
</xsl:otherwise>
1329313173
</xsl:choose>
1329413174
</xsl:attribute>
13295-
</xsl:element>
13175+
</script>
1329613176
</xsl:template>
1329713177

1329813178
<!-- SageCell Javascript-->

0 commit comments

Comments
 (0)