mirror of
https://gitee.com/gitea/gitea.git
synced 2024-12-15 01:51:30 +08:00
1e7f3c16a4
In #22447 it was noticed that display environments were not working correctly. This was due to the setting displayMode not being set. Further it was noticed that the error was not being displayed correctly. This PR fixes both of these issues by forcibly setting the displayMode setting and corrects an error in displayError. Fix #22447 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
function displayError(el, err) {
|
|
const target = targetElement(el);
|
|
target.classList.remove('is-loading');
|
|
const errorNode = document.createElement('div');
|
|
errorNode.setAttribute('class', 'ui message error markup-block-error mono');
|
|
errorNode.textContent = err.str || err.message || String(err);
|
|
target.before(errorNode);
|
|
}
|
|
|
|
function targetElement(el) {
|
|
// The target element is either the current element if it has the `is-loading` class or the pre that contains it
|
|
return el.classList.contains('is-loading') ? el : el.closest('pre');
|
|
}
|
|
|
|
export async function renderMath() {
|
|
const els = document.querySelectorAll('.markup code.language-math');
|
|
if (!els.length) return;
|
|
|
|
const [{default: katex}] = await Promise.all([
|
|
import(/* webpackChunkName: "katex" */'katex'),
|
|
import(/* webpackChunkName: "katex" */'katex/dist/katex.css'),
|
|
]);
|
|
|
|
for (const el of els) {
|
|
const source = el.textContent;
|
|
const displayMode = el.classList.contains('display');
|
|
const nodeName = displayMode ? 'p' : 'span';
|
|
|
|
try {
|
|
const tempEl = document.createElement(nodeName);
|
|
katex.render(source, tempEl, {
|
|
maxSize: 25,
|
|
maxExpand: 50,
|
|
displayMode,
|
|
});
|
|
targetElement(el).replaceWith(tempEl);
|
|
} catch (error) {
|
|
displayError(el, error);
|
|
}
|
|
}
|
|
}
|