Port the in-browser viewer to use MathJax v3

This commit is contained in:
Kovid Goyal 2020-06-20 00:34:16 +05:30
parent b7b7d9d681
commit 14c8099abb
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 64 additions and 6 deletions

View File

@ -15,6 +15,7 @@
if (for_pdf_renderer) document.title = "mathjax-load-complete";
else document.documentElement.dispatchEvent(new CustomEvent("calibre-mathjax-typeset-done"));
}
// also do any changes in mathjax.pyj for the in-browser reader
window.MathJax = {};
window.MathJax.options = {
renderActions: {

View File

@ -24,12 +24,69 @@ def postprocess(link_uid):
a.setAttribute('data-' + link_uid, JSON.stringify({'frag':href[1:]}))
def load_mathjax(src):
script = E.script(type='text/javascript')
script.async = True
script.src = src
document.head.appendChild(script)
def apply_mathjax(mathjax_files, link_uid, proceed):
if runtime.is_standalone_viewer:
document.documentElement.addEventListener("calibre-mathjax-typeset-done", def(ev):
postprocess(link_uid)
proceed()
)
script = E.script(type='text/javascript')
script.async = True
script.src = f'{runtime.FAKE_PROTOCOL}://{runtime.SANDBOX_HOST}/mathjax/startup.js'
load_mathjax(f'{runtime.FAKE_PROTOCOL}://{runtime.SANDBOX_HOST}/mathjax/startup.js')
return
window.MathJax = v'{}'
window.MathJax.startup = {
'ready': def ():
window.MathJax.startup.defaultReady()
# monkeypatch the method responsible for mapping font URLs
# to use our blob URLs, see https://github.com/mathjax/MathJax/issues/2458
window.MathJax.startup.output.font.addFontURLs = def (styles, fonts, url):
base = url.partition('/')[2]
for name in fonts:
clone = v'{}'
font = fonts[name]
for key in Object.keys(font):
clone[key] = font[key]
font_name = clone.src.partition('/')[2].partition('"')[0]
full_name = base + '/' + font_name
src = get_url(mathjax_files, full_name)
clone.src = clone.src.replace(/".+?"/, f'"{src}"')
styles[name] = clone
window.MathJax.startup.promise.then(def():
postprocess(link_uid)
proceed()
)
,
}
# also do any changes in pdf-mathjax-loader.js for the standalone
# viewer/editor/pdf outpu
window.MathJax.loader = {
'load': v"['input/tex-full', 'input/asciimath', 'input/mml', 'output/chtml']",
'require': def (url):
return new Promise(def (resolve, reject):
name = url.partition('/')[2]
script = document.createElement('script')
script.charset = 'UTF-8'
script.onload = def ():
resolve(url)
script.onerror = def ():
reject(url)
script.src = get_url(mathjax_files, name)
document.head.appendChild(script)
)
,
}
for s in document.scripts:
if s.type is 'text/x-mathjax-config':
es = document.createElement('script')
es.text = s.text
document.head.appendChild(es)
document.head.removeChild(es)
load_mathjax(get_url(mathjax_files, 'startup.js'))