mirror of
https://github.com/kiwix/kiwix-js-pwa.git
synced 2025-09-08 11:48:26 -04:00
Enable SW mode
Former-commit-id: 4299774f0f2a88983fb35799e53c315f72d2309d [formerly 39a92426de9b44ffe73a018f65f8bfc825db0f00] Former-commit-id: dae887b44d1512cd2426a6b0eba1f1e89f24cea1
This commit is contained in:
parent
e696fb840d
commit
cdf798c41b
@ -2424,8 +2424,11 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'images', 'cookies', 'q', 'trans
|
||||
htmlArticle = htmlArticle.replace(/href\s*=\s*["']javascript:[^"']+["']/gi, 'href=""');
|
||||
|
||||
//MathJax detection:
|
||||
params.containsMathTexRaw = params.useMathJax && /stackexchange|askubuntu|superuser|stackoverflow|mathoverflow|serverfault|stackapps/i.test(params.storedFile) ?
|
||||
/(\$\$?)((?:\\(?!\\\$)[\s\S]|(?!\1)[\s\S])+)\1(?:[\s<.,;:?!'")([{\]-])/.test(htmlArticle) : false;
|
||||
params.containsMathTexRaw = params.useMathJax &&
|
||||
/stackexchange|askubuntu|superuser|stackoverflow|mathoverflow|serverfault|stackapps/i.test(params.storedFile) ?
|
||||
/[^\\](\$\$?)((?:\\\$|(?!\1)[\s\S])+)\1/.test(htmlArticle) : false;
|
||||
// Below regex is a more complex version, probably erroneous
|
||||
// /(\$\$?)((?:\\(?!\\\$)[\s\S]|(?!\1)[\s\S])+)\1(?:[\s<.,;:?!'")([{\]-])/.test(htmlArticle) : false;
|
||||
//Simplify any configuration script
|
||||
//if (params.containsMathTexRaw) htmlArticle = htmlArticle.replace(/(<script\s+[^>]*?type\s*=\s*['"]\s*text\/x-mathjax-config[^>]+>[^<]+?Hub\.Config\s*\(\s*{\s*)[^<]*?(tex2jax\s*:[^}]+?})\s*,[^<]+(<\/script>)/i, "$1$2});$3");
|
||||
// Remove MathJax config script and convert $ delimiters
|
||||
@ -2440,13 +2443,13 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'images', 'cookies', 'q', 'trans
|
||||
// });
|
||||
// }
|
||||
//Replace all TeX SVGs with MathJax scripts
|
||||
if (params.useMathJax) {
|
||||
htmlArticle = htmlArticle.replace(/<img\s+(?=[^>]+?math-fallback-image)[^>]*?alt\s*=\s*(['"])((?:[^"']|(?!\1)[\s\S])+)[^>]+>/ig, function (p0, p1, math) {
|
||||
// Remove any rogue ampersands in MathJax due to double escaping (by Wikipedia)
|
||||
math = math.replace(/&/g, '&');
|
||||
return '<script type="math/tex">' + math + '</script>';
|
||||
});
|
||||
}
|
||||
// if (params.useMathJax) {
|
||||
// htmlArticle = htmlArticle.replace(/<img\s+(?=[^>]+?math-fallback-image)[^>]*?alt\s*=\s*(['"])((?:[^"']|(?!\1)[\s\S])+)[^>]+>/ig, function (p0, p1, math) {
|
||||
// // Remove any rogue ampersands in MathJax due to double escaping (by Wikipedia)
|
||||
// math = math.replace(/&/g, '&');
|
||||
// return '<script type="math/tex">' + math + '</script>';
|
||||
// });
|
||||
// }
|
||||
params.containsMathTex = params.useMathJax ? /<script\s+type\s*=\s*['"]\s*math\/tex\s*['"]/i.test(htmlArticle) : false;
|
||||
params.containsMathSVG = params.useMathJax ? /<img\s+(?=[^>]+?math-fallback-image)[^>]*?alt\s*=\s*['"][^'"]+[^>]+>/i.test(htmlArticle) : false;
|
||||
|
||||
|
@ -59,6 +59,21 @@ define(['uiUtil'], function (uiUtil) {
|
||||
if (!imageUrl) { checkbatch(); return; }
|
||||
image.removeAttribute('data-kiwixurl');
|
||||
var title = decodeURIComponent(imageUrl);
|
||||
// Code below enables MathJax display in SW mode for WikiMedia
|
||||
var isMathFallback = false;
|
||||
if (params.useMathJax && image.alt && image.classList.length > 0) {
|
||||
for (var c = image.classList.length; c--;) {
|
||||
if (/math-fallback-image/.test(image.classList[c])) isMathFallback = true;
|
||||
}
|
||||
if (isMathFallback) {
|
||||
var mathTex = image.getAttribute('alt');
|
||||
if (mathTex && iframe.contentWindow.katex) {
|
||||
iframe.contentWindow.katex.render(mathTex, image.parentElement);
|
||||
extractorBusy--;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (params.contentInjectionMode === 'serviceworker') {
|
||||
image.addEventListener('load', function () {
|
||||
image.style.opacity = '1';
|
||||
@ -194,6 +209,7 @@ define(['uiUtil'], function (uiUtil) {
|
||||
* Prepares an array or collection of image nodes that have been disabled in Service Worker for manual extraction
|
||||
*/
|
||||
function prepareImagesServiceWorker () {
|
||||
loadMathJax();
|
||||
var doc = iframe.contentDocument.documentElement;
|
||||
documentImages = doc.getElementsByTagName('img');
|
||||
if (!documentImages.length) return;
|
||||
@ -235,7 +251,7 @@ define(['uiUtil'], function (uiUtil) {
|
||||
// User wishes to extract images manually
|
||||
prepareManualExtraction();
|
||||
}
|
||||
loadMathJax();
|
||||
//loadMathJax();
|
||||
//setTimeout(loadMathJax, 3000);
|
||||
|
||||
}
|
||||
@ -280,26 +296,32 @@ define(['uiUtil'], function (uiUtil) {
|
||||
}
|
||||
|
||||
function loadMathJax() {
|
||||
if (params.useMathJax && (params.containsMathTexRaw || params.containsMathTex || params.containsMathSVG)) {
|
||||
var doc = iframe.contentDocument;
|
||||
if (!params.useMathJax) return;
|
||||
var doc = iframe.contentDocument;
|
||||
var prefix = '';
|
||||
if (params.contentInjectionMode === 'serviceworker') {
|
||||
params.containsMathSVG = /<img\s+(?=[^>]+?math-fallback-image)[^>]*?alt\s*=\s*['"][^'"]+[^>]+>/i.test(doc.body.innerHTML);
|
||||
prefix = window.location.href.replace(/^((?!.*\?).*\/|.*\/(?=[^\/]*\?)).*$/, '$1');
|
||||
}
|
||||
if (params.containsMathTexRaw || params.containsMathTex || params.containsMathSVG) {
|
||||
var script1, script2, script3;
|
||||
var link = doc.createElement("link");
|
||||
link.rel = "stylesheet";
|
||||
link.href = "js/katex/katex.min.css";
|
||||
link.href = prefix + "js/katex/katex.min.css";
|
||||
doc.head.appendChild(link);
|
||||
script1 = doc.createElement("script");
|
||||
script1.type = "text/javascript";
|
||||
//script.src = "js/MathJax/MathJax.js?config=TeX-AMS_HTML-full";
|
||||
script1.src = "js/katex/katex.min.js";
|
||||
script1.src = prefix + "js/katex/katex.min.js";
|
||||
if (params.containsMathTex) {
|
||||
script2 = doc.createElement("script");
|
||||
script2.type = "text/javascript";
|
||||
script2.src = "js/katex/contrib/mathtex-script-type.min.js";
|
||||
script2.src = prefix + "js/katex/contrib/mathtex-script-type.min.js";
|
||||
}
|
||||
if (params.containsMathTexRaw) {
|
||||
script3 = doc.createElement("script");
|
||||
script3.type = "text/javascript";
|
||||
script3.src = "js/katex/contrib/auto-render.min.js";
|
||||
script3.src = prefix + "js/katex/contrib/auto-render.min.js";
|
||||
script3.onload = function() {
|
||||
iframe.contentWindow.renderMathInElement(doc.body, {
|
||||
delimiters: [{
|
||||
|
Loading…
x
Reference in New Issue
Block a user