DEV: adds chat support (#67)

This commit is also removing jquery code and simplifying code.

Note that discourse-math is currently causing jumpy-ness in topics or in chat as the resulting processed mathjax element has a different size than the initial text node.
This commit is contained in:
Joffrey JAFFEUX 2023-01-18 12:35:27 +01:00 committed by GitHub
parent f9cf94d24d
commit 6f59c46912
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 48 deletions

View File

@ -10,7 +10,7 @@ function initMathJax(opts) {
return; return;
} }
let extensions = ["toMathML.js", "Safe.js"]; const extensions = ["toMathML.js", "Safe.js"];
if (opts.enable_accessibility) { if (opts.enable_accessibility) {
extensions.push("[a11y]/accessibility-menu.js"); extensions.push("[a11y]/accessibility-menu.js");
@ -38,56 +38,47 @@ function ensureMathJax(opts) {
} }
function decorate(elem, isPreview) { function decorate(elem, isPreview) {
const $elem = $(elem); if (elem.dataset.appliedMathjax) {
if ($elem.data("applied-mathjax")) {
return; return;
} }
$elem.data("applied-mathjax", true);
let $mathWrapper, $math; elem.dataset.appliedMathjax = true;
if ($elem.hasClass("math")) { let tag, classList, type;
const tag = elem.tagName === "DIV" ? "div" : "span";
if (elem.classList.contains("math")) {
tag = elem.tagName === "DIV" ? "div" : "span";
const display = tag === "div" ? "; mode=display" : ""; const display = tag === "div" ? "; mode=display" : "";
const displayClass = tag === "div" ? "block-math" : "inline-math"; const displayClass = tag === "div" ? "block-math" : "inline-math";
const type = `math/tex${display}`; type = `math/tex${display}`;
const classList = `math-container ${displayClass} mathjax-math`; classList = `math-container ${displayClass} mathjax-math`;
} else if (elem.classList.contains("asciimath")) {
$mathWrapper = $( tag = "span";
`<${tag} class="${classList}" style="display: none;"> classList = "math-container inline-math ascii-math";
<script type="${type}"></script> type = "math/asciimath";
</${tag}>`
);
$math = $mathWrapper.children();
$math.text($elem.text());
$elem.after($mathWrapper);
} else if ($elem.hasClass("asciimath")) {
// asciimath is always inline
const classList = `math-container inline-math ascii-math`;
const type = `math/asciimath`;
$mathWrapper = $(
`<span class="${classList}" style="display: none;">
<script type="${type}"></script>
</span>`
);
$math = $mathWrapper.children();
$math.text($elem.text());
$elem.after($mathWrapper);
} }
const mathScript = document.createElement("script");
mathScript.type = type;
mathScript.innerText = elem.innerText;
const mathWrapper = document.createElement(tag);
mathWrapper.classList.add(classList.split(" "));
mathWrapper.style.display = "none";
mathWrapper.appendChild(mathScript);
elem.after(mathWrapper);
later( later(
this, this,
() => { () => {
window.MathJax.Hub.Queue(() => { window.MathJax.Hub.Queue(() => {
// don't bother processing previews removed from DOM // don't bother processing previews removed from DOM
if (elem.parentElement && elem.parentElement.offsetParent !== null) { if (elem?.parentElement?.offsetParent !== null) {
window.MathJax.Hub.Typeset($math[0], () => { window.MathJax.Hub.Typeset(mathScript, () => {
$elem.hide(); elem.style.display = "none";
$mathWrapper.show(); mathWrapper.style.display = "block";
}); });
} }
}); });
@ -96,34 +87,43 @@ function decorate(elem, isPreview) {
); );
} }
function mathjax($elem, opts) { function mathjax(elem, opts) {
if (!$elem || !$elem.find) { if (!elem) {
return; return;
} }
let mathElems; let mathElems;
if (opts.enable_asciimath) { if (opts.enable_asciimath) {
mathElems = $elem.find(".math, .asciimath"); mathElems = elem.querySelectorAll(".math, .asciimath");
} else { } else {
mathElems = $elem.find(".math"); mathElems = elem.querySelectorAll(".math");
} }
if (mathElems.length > 0) { if (mathElems.length > 0) {
const isPreview = $elem.hasClass("d-editor-preview"); const isPreview = elem.classList.contains("d-editor-preview");
ensureMathJax(opts).then(() => { ensureMathJax(opts).then(() => {
mathElems.each((idx, elem) => decorate(elem, isPreview)); mathElems.forEach((mathElem) => decorate(mathElem, isPreview));
}); });
} }
} }
function initializeMath(api, discourse_math_opts) { function initializeMath(api, discourseMathOptions) {
api.decorateCooked( api.decorateCookedElement(
function (elem) { (element) => {
mathjax(elem, discourse_math_opts); mathjax(element, discourseMathOptions);
}, },
{ id: "mathjax" } { id: "mathjax" }
); );
api.decorateChatMessage(
(element) => {
mathjax(element, discourseMathOptions);
},
{
id: "mathjax-chat",
}
);
} }
export default { export default {

View File

@ -0,0 +1,12 @@
.chat-message-text {
// huge selector complexity/specificity makes it hard to do differently
// this is done to limit the size of the chat message after MathJax has rendered
// ultimately we would want a better solution to avoid channel jumpyness
// topics suffer from the same issue
.MJXc-display,
.MathJax_CHTML {
margin: 0 !important;
padding: 0 !important;
overflow: hidden !important;
}
}

View File

@ -8,5 +8,10 @@
# transpile_js: true # transpile_js: true
register_asset "stylesheets/common/discourse-math.scss" register_asset "stylesheets/common/discourse-math.scss"
register_asset "stylesheets/ext/discourse-chat.scss"
enabled_site_setting :discourse_math_enabled enabled_site_setting :discourse_math_enabled
after_initialize do
chat&.enable_markdown_feature("discourse-math") if respond_to?(:chat) && SiteSetting.chat_enabled
end