fix: restore KaTeX assets and deployment checks
This commit is contained in:
@@ -1060,15 +1060,40 @@ function switchTool(tool) {
|
||||
window.HuluToolbox?.activate(tool);
|
||||
}
|
||||
|
||||
function normalizeLatexSource(source) {
|
||||
let normalized = String(source || "").trim();
|
||||
if (normalized.startsWith("$$") && normalized.endsWith("$$")) {
|
||||
normalized = normalized.slice(2, -2).trim();
|
||||
} else if (normalized.startsWith("\\[") && normalized.endsWith("\\]")) {
|
||||
normalized = normalized.slice(2, -2).trim();
|
||||
} else if (normalized.startsWith("$") && normalized.endsWith("$")) {
|
||||
normalized = normalized.slice(1, -1).trim();
|
||||
}
|
||||
return normalized.replace(/\\\\(?=[A-Za-z,;:!])/g, "\\");
|
||||
}
|
||||
|
||||
function renderLatexPreview(source) {
|
||||
const target = $("#latex-preview");
|
||||
target.replaceChildren();
|
||||
if (!source.trim()) return;
|
||||
if (typeof katex === "undefined") { target.textContent = source; return; }
|
||||
target.classList.remove("latex-preview-error");
|
||||
const normalized = normalizeLatexSource(source);
|
||||
if (!normalized) return;
|
||||
if (typeof globalThis.katex?.render !== "function") {
|
||||
target.classList.add("latex-preview-error");
|
||||
target.textContent = "公式渲染组件加载失败,请刷新页面后重试。";
|
||||
return;
|
||||
}
|
||||
try {
|
||||
katex.render(source, target, { throwOnError: false, displayMode: true });
|
||||
} catch {
|
||||
target.textContent = source;
|
||||
globalThis.katex.render(normalized, target, {
|
||||
displayMode: true,
|
||||
throwOnError: true,
|
||||
trust: false,
|
||||
maxExpand: 500,
|
||||
maxSize: 20,
|
||||
});
|
||||
} catch (error) {
|
||||
target.classList.add("latex-preview-error");
|
||||
target.textContent = `公式语法错误:${error.message}`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1077,7 +1102,10 @@ async function saveFormula() {
|
||||
try {
|
||||
await api("latex/documents/", {
|
||||
method: "POST",
|
||||
body: { title: `公式 ${new Date().toLocaleString()}`, source: $("#latex-source").value },
|
||||
body: {
|
||||
title: `公式 ${new Date().toLocaleString()}`,
|
||||
source: normalizeLatexSource($("#latex-source").value),
|
||||
},
|
||||
});
|
||||
showToast("公式及首个版本已保存");
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user