{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# OPTIONS_GHC -Wall #-}
module Web.Page.Mathjax
( mathjaxPage,
mathjax27Page,
mathjaxSvgPage,
mathjax27SvgPage,
)
where
import Control.Lens
import Data.Text (Text)
import Lucid
import Text.InterpolatedString.Perl6
import Web.Page.Types
import Prelude
mathjax3Lib :: Html ()
mathjax3Lib =
with
(script_ mempty)
[ src_ "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-svg.js",
id_ "MathJax-script",
async_ ""
]
mathjax27Lib :: Html ()
mathjax27Lib =
with
(script_ mempty)
[ src_ "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_SVG"
]
jqueryLib :: Html ()
jqueryLib =
with
(script_ mempty)
[ src_ "https://code.jquery.com/jquery-3.3.1.slim.min.js",
integrity_ "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo",
crossorigin_ "anonymous"
]
mathjaxPage :: Page
mathjaxPage =
mempty
& #jsGlobal .~ PageJsText scriptMathjaxConfig
& #libsJs
.~ [ mathjax3Lib
]
mathjax27Page :: Page
mathjax27Page =
mempty
& #jsOnLoad .~ PageJsText scriptMathjaxConfig
& #libsJs
.~ [ mathjax27Lib
]
mathjaxSvgPage :: Text -> Page
mathjaxSvgPage cl =
mempty
& #jsGlobal .~ PageJsText (scriptMathjaxConfigSvg cl)
& #libsJs
.~ [ mathjax3Lib,
jqueryLib
]
mathjax27SvgPage :: Text -> Page
mathjax27SvgPage cl =
mempty
& #jsGlobal .~ PageJsText (scriptMathjax27ConfigSvg cl)
& #libsJs
.~ [ mathjax27Lib,
jqueryLib
]
scriptMathjaxConfig :: Text
scriptMathjaxConfig =
[q|
MathJax = {
tex: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ]
}
};
|]
scriptMathjax27ConfigSvg :: Text -> Text
scriptMathjax27ConfigSvg cl =
[qq|
setTimeout(() => \{
MathJax.Hub.Config(\{
tex2jax: \{
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
processEscapes: true
}
});
MathJax.Hub.Register.StartupHook("End", function() \{
setTimeout(() => \{
$('.{cl}').each(function()\{
var m = $('text>span>svg');
m.remove();
$(this).append(m);
});
}, 1);
});
}, 1);
|]
scriptMathjaxConfigSvg :: Text -> Text
scriptMathjaxConfigSvg cl =
[qq|
window.MathJax = \{
tex: \{
inlineMath: [ ['$','$'], ["\\(","\\)"] ]
},
startup: \{
ready: () => \{
MathJax.startup.defaultReady();
MathJax.startup.promise.then(() => \{
var xs = document.querySelectorAll('.{cl}');
xs.forEach((x) =>
\{ x.querySelectorAll('text').forEach((t) =>
\{t.querySelectorAll('mjx-container>svg').forEach((s) =>
\{
Array.from(t.attributes).forEach((a) => s.setAttribute(a.name, a.value));
x.appendChild(s);
});
});
});
});
}
}
};
|]