Skip to content

Commit 97d270e

Browse files
committed
Update the mathjax configuration to add in some of the other needed
MathJax configuration that was missed. Also, instead of disabling all of eslint, just disable typescript rules. This file should probably be changed to a js file. I have upcoming changes to the eslint configuaration that will not run typescript rules on js files that will make this work correctly. Also, load mathajx files from the already installed npm package rather than using a cdn. Furthermore, install the mathjax files for the production build. Also change formData.append to formData.set.
1 parent 47800cc commit 97d270e

File tree

4 files changed

+88
-46
lines changed

4 files changed

+88
-46
lines changed

.jscpd.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"threshold": 5,
3-
"ignore": ["node_modules", "dist"]
3+
"ignore": ["node_modules", "dist", "**/*.pm", "**/*.ts"]
44
}

quasar.conf.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/* eslint-env node */
2-
/* eslint-disable @typescript-eslint/no-var-requires */
2+
/* eslint-disable @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access */
3+
/* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return */
34
const { configure } = require('quasar/wrappers');
45
const StyleLintPlugin = require('stylelint-webpack-plugin');
6+
const path = require('path');
57

68
module.exports = configure(function (ctx) {
79
return {
@@ -48,6 +50,27 @@ module.exports = configure(function (ctx) {
4850
files: ['**/*.{vue,html,css,scss,sass}'],
4951
exclude: ['node_modules', 'dist']
5052
}));
53+
54+
if (ctx.prod) {
55+
chain.plugin('copy-webpack')
56+
.tap(args => {
57+
args[0].patterns.push({
58+
from: path.resolve(__dirname, './node_modules/mathjax-full/es5'),
59+
to: path.resolve(__dirname, './dist/spa/mathjax'),
60+
toType: 'dir'
61+
});
62+
return args;
63+
});
64+
}
65+
},
66+
67+
extendWebpack(cfg) {
68+
if (cfg.optimization && cfg.optimization.minimizer instanceof Array) {
69+
cfg.optimization.minimizer.forEach((plugin) => {
70+
if (plugin.constructor.name == 'TerserPlugin')
71+
plugin.options.exclude = /.*mathjax.*/;
72+
});
73+
}
5174
}
5275
},
5376

@@ -64,6 +87,13 @@ module.exports = configure(function (ctx) {
6487
'^/renderer': ''
6588
}
6689
}
90+
},
91+
static: path.join(__dirname, 'node_modules/mathjax-full/es5'),
92+
historyApiFallback: {
93+
rewrites: [{
94+
from: /^\/webwork3\/mathjax\/.*$/,
95+
to: (context) => context.parsedUrl.pathname.replace(/^\/webwork3\/mathjax/, '')
96+
}]
6797
}
6898
}
6999
};

src/components/common/Problem.vue

+26-23
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<template>
2-
<div v-html="html" />
2+
<div ref="renderDiv" v-html="html" />
33
</template>
4+
45
<script lang="ts">
5-
import { defineComponent, Ref, ref, watch } from 'vue';
6+
import type { Ref } from 'vue';
7+
import { defineComponent, ref, watch } from 'vue';
68
import axios from 'axios';
79
810
import './mathjax-config';
@@ -11,7 +13,7 @@ import 'mathjax-full/es5/tex-chtml.js';
1113
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1214
interface Window {
1315
// eslint-disable-next-line @typescript-eslint/no-explicit-any
14-
MathJax: any
16+
MathJax: any
1517
}
1618
1719
export default defineComponent({
@@ -25,36 +27,36 @@ export default defineComponent({
2527
setup(props){
2628
const html: Ref<string> = ref('');
2729
const _file: Ref<string> = ref(props.file);
30+
const renderDiv = ref<HTMLElement>();
2831
29-
async function loadProblem() {
32+
async function loadProblem() {
3033
const formData = new FormData();
3134
formData.set('problemSeed', '1');
3235
formData.set('sourceFilePath', _file.value);
3336
formData.set('outputFormat', 'static');
3437
35-
const response = await axios.post('/renderer/render-api', formData,
36-
{ headers: { 'Content-Type': 'multipart/form-data' } });
38+
let value;
39+
try {
40+
const response = await axios.post('/renderer/render-api', formData,
41+
{ headers: { 'Content-Type': 'multipart/form-data' } });
42+
43+
value = (response.data as { renderedHTML: string }).renderedHTML;
44+
} catch(e) {
45+
return;
46+
}
3747
38-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
39-
let value = response.data.renderedHTML as string;
48+
if (!value) return;
4049
41-
// extract only the form element.
50+
// Extract only the form element.
4251
const match = /<form(.*)>(.*)<\/form>/s.exec(value);
52+
if (match) html.value = match[0];
4353
44-
// replace the script tags with \( \) or \[ \]
45-
if (match) {
46-
html.value = match[0];
47-
// const m0 = match[0].replace(/<script type="math\/tex mode=display">(.+?)<\/script>/g, '\\[$1\\]');
48-
// html.value = m0.replace(/<script type="math\/tex">(.+?)<\/script>/g, '\\($1\\)');
54+
/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call */
55+
if (window.MathJax && typeof window.MathJax.typesetPromise == 'function') {
56+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
57+
window.MathJax.startup.promise.then(() => window.MathJax.typesetPromise([renderDiv.value]));
4958
}
50-
void Promise.resolve()
51-
.then(() => {
52-
/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call */
53-
window.MathJax.typesetPromise();
54-
/* eslint-enable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call */
55-
});
56-
// .catch(); // need to figure out a more robust way of handling an error like this.
57-
59+
/* eslint-enable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call */
5860
}
5961
6062
watch(() => props.file, () => {
@@ -63,7 +65,8 @@ export default defineComponent({
6365
});
6466
6567
return {
66-
html
68+
html,
69+
renderDiv
6770
};
6871
}
6972
});
+30-21
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,47 @@
1-
/* eslint-disable */
1+
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any */
2+
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */
3+
/* eslint-disable @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call */
24

35
interface Window {
4-
MathJax: any
6+
MathJax: any
57
}
68

79
window.MathJax = {
10+
tex: {
11+
packages: { '[+]': ['noerrors'] }
12+
},
813
loader: {
914
paths: {
10-
mathjax: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5'
15+
mathjax: `${process.env.VUE_ROUTER_BASE ?? ''}mathjax`
16+
},
17+
load: ['input/asciimath', '[tex]/noerrors']
18+
},
19+
startup: {
20+
ready: () => {
21+
const AM = window.MathJax.InputJax.AsciiMath.AM;
22+
for (let i = 0; i < AM.symbols.length; ++i) {
23+
if (AM.symbols[i].input == '**') {
24+
AM.symbols[i] = { input: '**', tag: 'msup', output: '^', tex: null, ttype: AM.TOKEN.INFIX };
25+
}
26+
}
27+
return window.MathJax.startup.defaultReady();
1128
}
1229
},
1330
options: {
1431
renderActions: {
15-
findScript: [10, function (doc: any) {
16-
document.querySelectorAll('script[type^="math/tex"]').forEach(function(node) {
17-
const nodeType = node.getAttribute('type');
18-
19-
const display = nodeType && !!nodeType.match(/; *mode=display/);
32+
findScript: [10, (doc: any) => {
33+
document.querySelectorAll('script[type^="math/tex"]').forEach((node: Element) => {
34+
const display = !!/; *mode=display/.exec((node as HTMLScriptElement).type);
2035
const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
2136
const text = document.createTextNode('');
22-
if (node.parentNode) {
23-
node.parentNode.replaceChild(text, node);
24-
math.start = {node: text, delim: '', n: 0};
25-
math.end = {node: text, delim: '', n: 0};
26-
doc.math.push(math);
27-
}
37+
node.parentNode?.replaceChild(text, node);
38+
math.start = { node: text, delim: '', n: 0 };
39+
math.end = { node: text, delim: '', n: 0 };
40+
doc.math.push(math);
2841
});
2942
}, '']
30-
}
43+
},
44+
ignoreHtmlClass: 'tex2jax_ignore'
3145
}
32-
// tex: {
33-
// inlineMath: [
34-
// ['$', '$'],
35-
// ['\\(', '\\)']
36-
// ]
37-
// }
46+
3847
};

0 commit comments

Comments
 (0)