Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Wait for math scaling before exporting PDF https://github.com/siyuan-note/siyuan/issues/18496

Daniel929e8003edda
diff --git a/app/src/protyle/export/index.ts b/app/src/protyle/export/index.ts
index 4694eed793..2c65af44eb 100644
--- a/app/src/protyle/export/index.ts
+++ b/app/src/protyle/export/index.ts
@@ -384,7 +384,7 @@ ${getIconScript(servePath)}
<script src="${servePath}stage/protyle/js/lute/lute.min.js?${Constants.SIYUAN_VERSION}"></script>
<script>
const previewElement = document.getElementById('preview');
- const fixBlockWidth = () => {
+ const fixBlockWidth = async () => {
const isLandscape = document.querySelector("#landscape").checked;
let width = 800
let height = 1131
@@ -438,7 +438,7 @@ ${getIconScript(servePath)}
previewElement.querySelectorAll('[data-type="NodeCodeBlock"][data-subtype="mermaid"] svg').forEach((item) => {
item.style.maxHeight = height + "px";
})
- Protyle.mathRender(previewElement, "${servePath}stage/protyle", true);
+ await Protyle.mathRender(previewElement, "${servePath}stage/protyle", true);
previewElement.querySelectorAll("table").forEach(item => {
if (item.clientWidth > item.parentElement.clientWidth) {
item.style.zoom = (item.parentElement.clientWidth / item.clientWidth).toFixed(2) - 0.01;
@@ -712,7 +712,7 @@ ${getIconScript(servePath)}
document.body.classList.add("exporting");
previewElement.style.zoom = "";
previewElement.style.padding = "6px 0 0 0";
- fixBlockWidth();
+ await fixBlockWidth();
actionElement.remove();
ipcRenderer.send("${Constants.SIYUAN_EXPORT_PDF}", exportConfig);
});
diff --git a/app/src/protyle/render/mathRender.ts b/app/src/protyle/render/mathRender.ts
index 69e4f9e65e..97021ee8df 100644
--- a/app/src/protyle/render/mathRender.ts
+++ b/app/src/protyle/render/mathRender.ts
@@ -6,6 +6,22 @@ import {hasClosestBlock} from "../util/hasClosest";
import {looseJsonParse} from "../../util/functions";
import {genRenderFrame} from "./util";
+const fitMathWidth = (mathElement: HTMLElement, blockElement: HTMLElement | false, isBlock: boolean) => {
+ return new Promise<void>((resolve) => {
+ setTimeout(() => {
+ if (isBlock) {
+ const katexElement = mathElement.querySelector(".katex-display") as HTMLElement;
+ if (katexElement && katexElement.clientWidth < katexElement.scrollWidth) {
+ katexElement.firstElementChild?.setAttribute("style", `font-size:${katexElement.clientWidth * 100 / katexElement.scrollWidth}%`);
+ }
+ } else if (blockElement && mathElement.offsetWidth > blockElement.clientWidth) {
+ mathElement.firstElementChild?.setAttribute("style", `font-size:${blockElement.clientWidth * 100 / mathElement.offsetWidth}%`);
+ }
+ resolve();
+ });
+ });
+};
+
export const mathRender = (element: Element, cdn = Constants.PROTYLE_CDN, maxWidth = false) => {
let mathElements: Element[] | NodeListOf<Element> = [];
if (element.getAttribute("data-subtype") === "math" && element.getAttribute("data-render") !== "true") {
@@ -17,8 +33,9 @@ export const mathRender = (element: Element, cdn = Constants.PROTYLE_CDN, maxWid
return;
}
addStyle(`${cdn}/js/katex/katex.min.css?v=0.16.9`, "protyleKatexStyle");
- addScript(`${cdn}/js/katex/katex.min.js?v=0.16.9`, "protyleKatexScript").then(() => {
- addScript(`${cdn}/js/katex/mhchem.min.js?v=0.16.9`, "protyleKatexMhchemScript").then(() => {
+ return addScript(`${cdn}/js/katex/katex.min.js?v=0.16.9`, "protyleKatexScript").then(() => {
+ return addScript(`${cdn}/js/katex/mhchem.min.js?v=0.16.9`, "protyleKatexMhchemScript").then(() => {
+ const resizePromises: Promise<void>[] = [];
mathElements.forEach((mathElement: HTMLElement) => {
mathElement.setAttribute("data-render", "true");
let macros = {};
@@ -101,20 +118,9 @@ export const mathRender = (element: Element, cdn = Constants.PROTYLE_CDN, maxWid
}
}
- // export pdf
+ // 导出 PDF 时等待公式完成宽度适配后再继续
if (maxWidth) {
- setTimeout(() => {
- if (isBlock) {
- const katexElement = mathElement.querySelector(".katex-display");
- if (katexElement.clientWidth < katexElement.scrollWidth) {
- katexElement.firstElementChild.setAttribute("style", `font-size:${katexElement.clientWidth * 100 / katexElement.scrollWidth}%`);
- }
- } else {
- if (blockElement && mathElement.offsetWidth > blockElement.clientWidth) {
- mathElement.firstElementChild.setAttribute("style", `font-size:${blockElement.clientWidth * 100 / mathElement.offsetWidth}%`);
- }
- }
- });
+ resizePromises.push(fitMathWidth(mathElement, blockElement, isBlock));
}
} catch (e) {
if (isBlock) {
@@ -128,6 +134,7 @@ export const mathRender = (element: Element, cdn = Constants.PROTYLE_CDN, maxWid
}
}
});
+ return Promise.all(resizePromises).then(() => undefined);
});
});
};