Vanessa 2025-03-11 23:43:05 +08:00
parent 650493dcdb
commit 15ec12e4db
4 changed files with 67 additions and 34 deletions

View file

@ -4,6 +4,7 @@ import {Constants} from "../../constants";
import {hasNextSibling, hasPreviousSibling} from "../wysiwyg/getBlock";
import {hasClosestBlock} from "../util/hasClosest";
import {looseJsonParse} from "../../util/functions";
import {genRenderFrame} from "./util";
export const mathRender = (element: Element, cdn = Constants.PROTYLE_CDN, maxWidth = false) => {
let mathElements: Element[] = [];
@ -24,43 +25,40 @@ export const mathRender = (element: Element, cdn = Constants.PROTYLE_CDN, maxWid
return;
}
mathElement.setAttribute("data-render", "true");
let renderElement = mathElement;
if (mathElement.tagName === "DIV") {
renderElement = mathElement.firstElementChild as HTMLElement;
}
let macros = {};
try {
macros = looseJsonParse(window.siyuan.config.editor.katexMacros || "{}");
} catch (e) {
console.warn("KaTex macros is not JSON", e);
}
const isBlock = mathElement.tagName === "DIV";
try {
renderElement.innerHTML = window.katex.renderToString(Lute.UnEscapeHTMLStr(mathElement.getAttribute("data-content")), {
displayMode: mathElement.tagName === "DIV",
const mathHTML = window.katex.renderToString(Lute.UnEscapeHTMLStr(mathElement.getAttribute("data-content")), {
displayMode: isBlock,
output: "html",
macros,
trust: true, // REF: https://katex.org/docs/supported#html
strict: (errorCode) => errorCode === "unicodeTextInMathMode" ? "ignore" : "warn",
});
renderElement.classList.remove("ft__error");
const blockElement = hasClosestBlock(mathElement);
if (mathElement.tagName === "DIV") {
renderElement.firstElementChild.setAttribute("contenteditable", "false");
if (renderElement.childElementCount < 2) {
// 不能使用 contenteditable="false",否则光标无法移动到该块
renderElement.insertAdjacentHTML("beforeend", `<span style="position: absolute;right: 0;top: 0;">${Constants.ZWSP}</span>`);
}
if (isBlock) {
genRenderFrame(mathElement);
mathElement.firstElementChild.firstElementChild.classList.remove("ft__error");
mathElement.firstElementChild.firstElementChild.setAttribute("contenteditable", "false");
mathElement.firstElementChild.firstElementChild.innerHTML = mathHTML;
// https://github.com/siyuan-note/siyuan/issues/3541
const baseElements = renderElement.querySelectorAll(".base");
const baseElements = mathElement.querySelectorAll(".base");
if (baseElements.length > 0) {
baseElements[baseElements.length - 1].insertAdjacentHTML("afterend", "<span class='fn__flex-1'></span>");
}
// https://github.com/siyuan-note/siyuan/issues/4334
const newlineElement = renderElement.querySelector(".katex-html > .newline");
const newlineElement = mathElement.querySelector(".katex-html > .newline");
if (newlineElement) {
newlineElement.parentElement.style.display = "block";
}
} else {
mathElement.classList.remove("ft__error");
mathElement.innerHTML = mathHTML;
if (blockElement && mathElement.getBoundingClientRect().width > blockElement.clientWidth) {
mathElement.style.maxWidth = "100%";
mathElement.style.overflowX = "auto";
@ -110,7 +108,7 @@ export const mathRender = (element: Element, cdn = Constants.PROTYLE_CDN, maxWid
// export pdf
if (maxWidth) {
setTimeout(() => {
if (mathElement.tagName === "DIV") {
if (isBlock) {
const katexElement = mathElement.querySelector(".katex-display");
if (katexElement.clientWidth < katexElement.scrollWidth) {
katexElement.firstElementChild.setAttribute("style", `font-size:${katexElement.clientWidth * 100 / katexElement.scrollWidth}%`);
@ -123,8 +121,15 @@ export const mathRender = (element: Element, cdn = Constants.PROTYLE_CDN, maxWid
});
}
} catch (e) {
renderElement.innerHTML = e.message;
renderElement.classList.add("ft__error");
if (isBlock) {
genRenderFrame(mathElement);
mathElement.firstElementChild.firstElementChild.setAttribute("contenteditable", "false");
mathElement.firstElementChild.firstElementChild.innerHTML = e.message;
mathElement.firstElementChild.firstElementChild.classList.add("ft__error");
} else {
mathElement.innerHTML = e.message;
mathElement.classList.add("ft__error");
}
}
});
});