Skip to content

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

:bug: Preserve exports with deleted PDF annotation references https://github.com/siyuan-note/siyuan/issues/20081

Daniel458383c02b4f
diff --git a/app/appearance/langs/ar.json b/app/appearance/langs/ar.json
index 29aefabb7f..157ad92b24 100644
--- a/app/appearance/langs/ar.json
+++ b/app/appearance/langs/ar.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "يشير ${x} من الكتل إلى تعليق PDF هذا. سيؤدي حذفه إلى إبطال هذه المراجع. هل تريد المتابعة؟",
"ocrModelDefault": "القيمة الافتراضية للنموذج",
"ocrDetectionThreshold": "عتبة الكشف",
"ocrBoxThreshold": "عتبة الثقة لمربعات النص",
diff --git a/app/appearance/langs/de.json b/app/appearance/langs/de.json
index 3b6a0fd89e..3a7f449bbb 100644
--- a/app/appearance/langs/de.json
+++ b/app/appearance/langs/de.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "Diese PDF-Anmerkung wird von ${x} Blöcken referenziert. Durch das Löschen werden diese Referenzen ungültig. Fortfahren?",
"ocrModelDefault": "Modellstandard",
"ocrDetectionThreshold": "Erkennungsschwelle für Textbereiche",
"ocrBoxThreshold": "Konfidenzschwelle für Textrahmen",
diff --git a/app/appearance/langs/en.json b/app/appearance/langs/en.json
index d32fe7df6e..d2401ad104 100644
--- a/app/appearance/langs/en.json
+++ b/app/appearance/langs/en.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "This PDF annotation is referenced by ${x} blocks. Deleting it will invalidate these references. Continue?",
"ocrModelDefault": "Model default",
"ocrDetectionThreshold": "Detection threshold",
"ocrBoxThreshold": "Text box confidence threshold",
diff --git a/app/appearance/langs/es.json b/app/appearance/langs/es.json
index 11ca159e62..d1fac4d8f6 100644
--- a/app/appearance/langs/es.json
+++ b/app/appearance/langs/es.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "${x} bloques hacen referencia a esta anotación PDF. Al eliminarla, estas referencias dejarán de ser válidas. ¿Continuar?",
"ocrModelDefault": "Predeterminado del modelo",
"ocrDetectionThreshold": "Umbral de detección",
"ocrBoxThreshold": "Umbral de confianza de los cuadros de texto",
diff --git a/app/appearance/langs/fr.json b/app/appearance/langs/fr.json
index 8d9d5fd9c6..741807583d 100644
--- a/app/appearance/langs/fr.json
+++ b/app/appearance/langs/fr.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "Cette annotation PDF est référencée par ${x} blocs. Sa suppression rendra ces références invalides. Continuer ?",
"ocrModelDefault": "Valeur par défaut du modèle",
"ocrDetectionThreshold": "Seuil de détection",
"ocrBoxThreshold": "Seuil de confiance des zones de texte",
diff --git a/app/appearance/langs/he.json b/app/appearance/langs/he.json
index 70c3833851..dd3b0c8e5d 100644
--- a/app/appearance/langs/he.json
+++ b/app/appearance/langs/he.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "${x} בלוקים מפנים להערת PDF זו. מחיקתה תהפוך את ההפניות האלה ללא תקפות. להמשיך?",
"ocrModelDefault": "ברירת המחדל של המודל",
"ocrDetectionThreshold": "סף זיהוי אזורים",
"ocrBoxThreshold": "סף ביטחון לתיבות טקסט",
diff --git a/app/appearance/langs/hi.json b/app/appearance/langs/hi.json
index a539e1f1fa..7eaf258455 100644
--- a/app/appearance/langs/hi.json
+++ b/app/appearance/langs/hi.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "${x} ब्लॉक इस PDF टिप्पणी का संदर्भ देते हैं। इसे हटाने से ये संदर्भ अमान्य हो जाएंगे। जारी रखें?",
"ocrModelDefault": "मॉडल का डिफ़ॉल्ट",
"ocrDetectionThreshold": "पहचान क्षेत्र की सीमा",
"ocrBoxThreshold": "टेक्स्ट बॉक्स विश्वास सीमा",
diff --git a/app/appearance/langs/id.json b/app/appearance/langs/id.json
index 3609a7b606..79c891e3aa 100644
--- a/app/appearance/langs/id.json
+++ b/app/appearance/langs/id.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "Anotasi PDF ini dirujuk oleh ${x} blok. Menghapusnya akan membuat rujukan tersebut tidak valid. Lanjutkan?",
"ocrModelDefault": "Bawaan model",
"ocrDetectionThreshold": "Ambang deteksi",
"ocrBoxThreshold": "Ambang keyakinan kotak teks",
diff --git a/app/appearance/langs/it.json b/app/appearance/langs/it.json
index 1d71ae9f60..453f065734 100644
--- a/app/appearance/langs/it.json
+++ b/app/appearance/langs/it.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "Questa annotazione PDF è referenziata da ${x} blocchi. Eliminandola, questi riferimenti diventeranno non validi. Continuare?",
"ocrModelDefault": "Predefinito del modello",
"ocrDetectionThreshold": "Soglia di rilevamento",
"ocrBoxThreshold": "Soglia di confidenza dei riquadri di testo",
diff --git a/app/appearance/langs/ja.json b/app/appearance/langs/ja.json
index b6ca594959..bb096144b3 100644
--- a/app/appearance/langs/ja.json
+++ b/app/appearance/langs/ja.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "この PDF 注釈は ${x} 個のブロックから参照されています。削除すると、これらの参照が無効になります。続行しますか?",
"ocrModelDefault": "モデルの既定値",
"ocrDetectionThreshold": "検出しきい値",
"ocrBoxThreshold": "テキスト領域の信頼度しきい値",
diff --git a/app/appearance/langs/ko.json b/app/appearance/langs/ko.json
index 540500d0ca..4b195fcc22 100644
--- a/app/appearance/langs/ko.json
+++ b/app/appearance/langs/ko.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "이 PDF 주석은 ${x}개의 블록에서 참조됩니다. 삭제하면 이 참조가 무효화됩니다. 계속하시겠습니까?",
"ocrModelDefault": "모델 기본값",
"ocrDetectionThreshold": "검출 임계값",
"ocrBoxThreshold": "텍스트 영역 신뢰도 임계값",
diff --git a/app/appearance/langs/nl.json b/app/appearance/langs/nl.json
index a9f72d7db5..fb92bb0d72 100644
--- a/app/appearance/langs/nl.json
+++ b/app/appearance/langs/nl.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "${x} blokken verwijzen naar deze PDF-annotatie. Als u deze verwijdert, worden deze verwijzingen ongeldig. Doorgaan?",
"ocrModelDefault": "Modelstandaard",
"ocrDetectionThreshold": "Detectiedrempel",
"ocrBoxThreshold": "Betrouwbaarheidsdrempel voor tekstvakken",
diff --git a/app/appearance/langs/pl.json b/app/appearance/langs/pl.json
index 499068fb51..3e2d7aca43 100644
--- a/app/appearance/langs/pl.json
+++ b/app/appearance/langs/pl.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "Ta adnotacja PDF jest przywoływana przez ${x} bloków. Jej usunięcie unieważni te odwołania. Kontynuować?",
"ocrModelDefault": "Domyślna wartość modelu",
"ocrDetectionThreshold": "Próg wykrywania",
"ocrBoxThreshold": "Próg pewności obszarów tekstu",
diff --git a/app/appearance/langs/pt-BR.json b/app/appearance/langs/pt-BR.json
index badb7129de..bf31351ffd 100644
--- a/app/appearance/langs/pt-BR.json
+++ b/app/appearance/langs/pt-BR.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "Esta anotação PDF é referenciada por ${x} blocos. Ao excluí-la, essas referências ficarão inválidas. Continuar?",
"ocrModelDefault": "Padrão do modelo",
"ocrDetectionThreshold": "Limiar de detecção",
"ocrBoxThreshold": "Limiar de confiança das caixas de texto",
diff --git a/app/appearance/langs/ru.json b/app/appearance/langs/ru.json
index f850181e5c..c38cb5b977 100644
--- a/app/appearance/langs/ru.json
+++ b/app/appearance/langs/ru.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "На эту аннотацию PDF ссылаются ${x} блоков. После её удаления эти ссылки станут недействительными. Продолжить?",
"ocrModelDefault": "По умолчанию для модели",
"ocrDetectionThreshold": "Порог обнаружения",
"ocrBoxThreshold": "Порог уверенности текстовых областей",
diff --git a/app/appearance/langs/sk.json b/app/appearance/langs/sk.json
index aea5bc4d79..6069d32449 100644
--- a/app/appearance/langs/sk.json
+++ b/app/appearance/langs/sk.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "Na túto anotáciu PDF odkazuje ${x} blokov. Jej odstránením sa tieto odkazy stanú neplatnými. Pokračovať?",
"ocrModelDefault": "Predvolená hodnota modelu",
"ocrDetectionThreshold": "Prah detekcie",
"ocrBoxThreshold": "Prah spoľahlivosti textových oblastí",
diff --git a/app/appearance/langs/sr.json b/app/appearance/langs/sr.json
index ca2cee357a..7535eff8ef 100644
--- a/app/appearance/langs/sr.json
+++ b/app/appearance/langs/sr.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "На ову PDF белешку упућује ${x} блокова. Брисањем ће ове референце постати неважеће. Наставити?",
"ocrModelDefault": "Подразумевана вредност модела",
"ocrDetectionThreshold": "Праг детекције",
"ocrBoxThreshold": "Праг поузданости текстуалних области",
diff --git a/app/appearance/langs/th.json b/app/appearance/langs/th.json
index eb019b0ba8..5bb665da56 100644
--- a/app/appearance/langs/th.json
+++ b/app/appearance/langs/th.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "มี ${x} บล็อกอ้างอิงคำอธิบายประกอบ PDF นี้ การลบจะทำให้การอ้างอิงเหล่านี้ใช้ไม่ได้ ต้องการดำเนินการต่อหรือไม่?",
"ocrModelDefault": "ค่าเริ่มต้นของโมเดล",
"ocrDetectionThreshold": "เกณฑ์การตรวจจับ",
"ocrBoxThreshold": "เกณฑ์ความเชื่อมั่นของกรอบข้อความ",
diff --git a/app/appearance/langs/tr.json b/app/appearance/langs/tr.json
index 9a10db3a51..21defa6a28 100644
--- a/app/appearance/langs/tr.json
+++ b/app/appearance/langs/tr.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "Bu PDF açıklamasına ${x} bloktan başvuruluyor. Silindiğinde bu başvurular geçersiz olacaktır. Devam edilsin mi?",
"ocrModelDefault": "Model varsayılanı",
"ocrDetectionThreshold": "Algılama eşiği",
"ocrBoxThreshold": "Metin kutusu güven eşiği",
diff --git a/app/appearance/langs/uk.json b/app/appearance/langs/uk.json
index 0b04d8396e..b4aa768a5a 100644
--- a/app/appearance/langs/uk.json
+++ b/app/appearance/langs/uk.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "На цю анотацію PDF посилаються ${x} блоків. Після її видалення ці посилання стануть недійсними. Продовжити?",
"ocrModelDefault": "Типове значення моделі",
"ocrDetectionThreshold": "Поріг виявлення",
"ocrBoxThreshold": "Поріг впевненості текстових областей",
diff --git a/app/appearance/langs/zh-CN.json b/app/appearance/langs/zh-CN.json
index 84d49d6836..efbeb048e0 100644
--- a/app/appearance/langs/zh-CN.json
+++ b/app/appearance/langs/zh-CN.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "此 PDF 标注被 ${x} 个块引用,删除后这些引用将失效。是否继续?",
"ocrModelDefault": "模型默认",
"ocrDetectionThreshold": "检测阈值",
"ocrBoxThreshold": "文本框置信度阈值",
diff --git a/app/appearance/langs/zh-TW.json b/app/appearance/langs/zh-TW.json
index 0aeb6f19cc..eddd77c020 100644
--- a/app/appearance/langs/zh-TW.json
+++ b/app/appearance/langs/zh-TW.json
@@ -1,4 +1,5 @@
{
+ "pdfAnnotationDeleteRefConfirm": "此 PDF 標註被 ${x} 個區塊引用,刪除後這些引用將失效。是否繼續?",
"ocrModelDefault": "模型預設",
"ocrDetectionThreshold": "偵測閾值",
"ocrBoxThreshold": "文字框信賴度閾值",
diff --git a/app/src/asset/anno.ts b/app/src/asset/anno.ts
index 77f49e4c47..d7421e5632 100644
--- a/app/src/asset/anno.ts
+++ b/app/src/asset/anno.ts
@@ -436,17 +436,8 @@ export const initAnno = (element: HTMLElement, pdf: any) => {
processed = true;
break;
} else if (type === "remove") {
- const urlPath = pdf.appConfig.file.replace(location.origin, "").substr(1);
- const config = getConfig(pdf);
const id = rectElement.getAttribute("data-node-id");
- delete config[id];
- getRectElementsByNodeId(element, id).forEach(item => {
- item.remove();
- });
- fetchPost("/api/asset/setFileAnnotation", {
- path: urlPath + ".sya",
- data: JSON.stringify(config),
- });
+ removeAnno(pdf, element, id);
hideToolbar(element);
event.preventDefault();
event.stopPropagation();
@@ -1153,6 +1144,45 @@ async function getRectImgData(pdfObj: any, pageNumber: number, position: number[
return {blob, rotation: totalRotation, displayWidth};
}
+const removeAnno = (pdf: any, element: HTMLElement, id: string) => {
+ const file = pdf.appConfig.file;
+ const urlPath = file.replace(location.origin, "").substr(1);
+ const isCurrent = () => element.isConnected && getRegisteredPdfInstance(element) === pdf && pdf.appConfig.file === file;
+ const remove = () => {
+ if (!isCurrent()) {
+ return;
+ }
+ const config = getConfig(pdf);
+ if (!config?.[id]) {
+ return;
+ }
+ delete config[id];
+ getRectElementsByNodeId(element, id).forEach(item => {
+ item.remove();
+ });
+ fetchPost("/api/asset/setFileAnnotation", {
+ path: urlPath + ".sya",
+ data: JSON.stringify(config),
+ });
+ };
+ fetchPost("/api/block/getRefIDsByFileAnnotationID", {
+ id,
+ notebook: new URL(file, location.origin).searchParams.get("box") || "",
+ }, (response) => {
+ if (response.code !== 0 || !isCurrent()) {
+ return;
+ }
+ const count = response.data.refDefs.length;
+ if (count > 0) {
+ confirmDialog(window.siyuan.languages.deleteOpConfirm,
+ window.siyuan.languages.pdfAnnotationDeleteRefConfirm.replace("${x}", count.toString()), remove,
+ undefined, true);
+ } else {
+ remove();
+ }
+ });
+};
+
const setConfig = (pdf: any, id: string, data: IPdfAnno) => {
const config = getConfig(pdf);
config[id] = data;
diff --git a/app/tests/pdfAnnotationDelete.test.js b/app/tests/pdfAnnotationDelete.test.js
new file mode 100644
index 0000000000..8d1fd091a5
--- /dev/null
+++ b/app/tests/pdfAnnotationDelete.test.js
@@ -0,0 +1,96 @@
+const assert = require("node:assert/strict");
+const {readFileSync} = require("node:fs");
+const {test} = require("node:test");
+const {runInNewContext} = require("node:vm");
+const ts = require("typescript");
+
+const source = ts.createSourceFile("anno.ts", readFileSync("src/asset/anno.ts", "utf8"), ts.ScriptTarget.ES2021, true);
+const declaration = source.statements.find(statement => ts.isVariableStatement(statement) &&
+ statement.declarationList.declarations.some(item => item.name.getText(source) === "removeAnno"));
+const code = ts.transpileModule(declaration.getText(source) + "\nremoveAnno;", {
+ compilerOptions: {target: ts.ScriptTarget.ES2021},
+}).outputText;
+
+const setup = (file = "http://localhost/assets/document.pdf") => {
+ const requests = [];
+ const confirmations = [];
+ const config = {annotation: {page: 1}, other: {page: 2}};
+ const pdf = {appConfig: {file, config}};
+ const element = {isConnected: true};
+ let registered = pdf;
+ let removed = 0;
+ const removeAnno = runInNewContext(code, {
+ URL,
+ location: {origin: "http://localhost"},
+ window: {siyuan: {languages: {deleteOpConfirm: "Delete", pdfAnnotationDeleteRefConfirm: "Referenced by ${x} blocks"}}},
+ getRegisteredPdfInstance: () => registered,
+ getConfig: () => config,
+ getRectElementsByNodeId: (owner, id) => {
+ assert.equal(owner, element);
+ assert.equal(id, "annotation");
+ return [{remove: () => { removed++; }}];
+ },
+ fetchPost: (url, data, callback) => { requests.push({url, data, callback}); },
+ confirmDialog: (title, text, confirm, cancel, isDelete) => { confirmations.push({title, text, confirm, cancel, isDelete}); },
+ });
+ removeAnno(pdf, element, "annotation");
+ return {requests, confirmations, config, pdf, element, removed: () => removed, replaceViewer: () => { registered = {}; }};
+};
+
+test("referenced PDF annotations remain intact until deletion is confirmed", () => {
+ const state = setup("http://localhost/assets/document.pdf?box=20261003000000-abcdefg");
+ const query = state.requests[0];
+ assert.equal(query.url, "/api/block/getRefIDsByFileAnnotationID");
+ assert.equal(query.data.id, "annotation");
+ assert.equal(query.data.notebook, "20261003000000-abcdefg");
+ query.callback({code: 0, data: {refDefs: [{refID: "first"}, {refID: "second"}]}});
+ assert.equal(state.requests.length, 1);
+ assert.equal(state.removed(), 0);
+ assert.ok(state.config.annotation);
+ const confirmation = state.confirmations[0];
+ assert.equal(confirmation.text, "Referenced by 2 blocks");
+ assert.equal(confirmation.isDelete, true);
+ // 确认时读取最新配置,保留等待期间新增的标注。
+ state.config.latest = {page: 3};
+ confirmation.confirm();
+ assert.equal(state.removed(), 1);
+ assert.equal(state.config.annotation, undefined);
+ const save = state.requests[1];
+ assert.equal(save.url, "/api/asset/setFileAnnotation");
+ assert.equal(save.data.path, "assets/document.pdf?box=20261003000000-abcdefg.sya");
+ assert.deepEqual(JSON.parse(save.data.data), {other: {page: 2}, latest: {page: 3}});
+});
+
+test("unreferenced PDF annotations are deleted without a confirmation", () => {
+ const state = setup();
+ assert.equal(state.requests[0].data.notebook, "");
+ state.requests[0].callback({code: 0, data: {refDefs: []}});
+ assert.equal(state.confirmations.length, 0);
+ assert.equal(state.removed(), 1);
+ assert.equal(state.requests[1].data.path, "assets/document.pdf.sya");
+});
+
+test("a failed reference query preserves PDF annotations", () => {
+ const state = setup();
+ state.requests[0].callback({code: -1});
+ assert.equal(state.requests.length, 1);
+ assert.ok(state.config.annotation);
+ assert.equal(state.removed(), 0);
+});
+
+test("closing or replacing the PDF viewer prevents delayed annotation deletion", () => {
+ for (const invalidate of [state => { state.element.isConnected = false; }, state => { state.replaceViewer(); },
+ state => { state.pdf.appConfig.file = "http://localhost/assets/other.pdf"; }]) {
+ const pending = setup();
+ invalidate(pending);
+ pending.requests[0].callback({code: 0, data: {refDefs: []}});
+ assert.equal(pending.removed(), 0);
+ assert.equal(pending.requests.length, 1);
+ const confirmed = setup();
+ confirmed.requests[0].callback({code: 0, data: {refDefs: [{refID: "block"}]}});
+ invalidate(confirmed);
+ confirmed.confirmations[0].confirm();
+ assert.equal(confirmed.removed(), 0);
+ assert.ok(confirmed.config.annotation);
+ }
+});
diff --git a/kernel/model/export.go b/kernel/model/export.go
index 5ea2a390ae..c7fa8566ea 100644
--- a/kernel/model/export.go
+++ b/kernel/model/export.go
@@ -4283,7 +4283,7 @@ func processFileAnnotationRef(refID string, n *ast.Node, fileAnnotationRefMode i
return fmt.Errorf("resolve file annotation asset [%s]: %w", assetLink, err)
}
sya := absPath + ".sya"
- // 以实际资源所属笔记本认证标注密文,读取失败时保留源文件并中止导出。
+ // 以实际资源所属笔记本认证标注密文,读取已有标注失败时保留源文件并中止导出。
assetBoxID := ExtractBoxIDFromAssetsPath(absPath)
var dek []byte
if IsEncryptedBox(assetBoxID) {
@@ -4297,6 +4297,11 @@ func processFileAnnotationRef(refID string, n *ast.Node, fileAnnotationRefMode i
}
syaData, readErr := os.ReadFile(sya)
if readErr != nil {
+ if os.IsNotExist(readErr) {
+ // 删除最后一个标注会移除标注文件,导出时仍保留引用文字。
+ n.InsertBefore(&ast.Node{Type: ast.NodeText, Tokens: []byte(n.TextMarkTextContent)})
+ return nil
+ }
return fmt.Errorf("read file annotation [%s]: %w", sya, readErr)
}
if nil != dek {
@@ -4315,12 +4320,20 @@ func processFileAnnotationRef(refID string, n *ast.Node, fileAnnotationRefMode i
if err = gulu.JSON.UnmarshalJSON(syaData, &syaJSON); err != nil {
return fmt.Errorf("parse file annotation [%s]: %w", sya, err)
}
+ if nil == syaJSON {
+ return fmt.Errorf("invalid file annotation data in [%s]", sya)
+ }
annotationData, found := syaJSON[annotationID]
+ if !found {
+ // 标注已删除时保留引用文字,不生成无法定位页码的链接。
+ n.InsertBefore(&ast.Node{Type: ast.NodeText, Tokens: []byte(n.TextMarkTextContent)})
+ return nil
+ }
pageIndex := annotationData.Page
if 0 < len(annotationData.Pages) {
pageIndex = annotationData.Pages[0].Index
}
- if !found || nil == pageIndex || *pageIndex < 0 {
+ if nil == pageIndex || *pageIndex < 0 {
return fmt.Errorf("missing or invalid annotation [%s] in [%s]", annotationID, sya)
}
pageStr := strconv.Itoa(*pageIndex + 1)
diff --git a/kernel/model/export_annotation_test.go b/kernel/model/export_annotation_test.go
new file mode 100644
index 0000000000..f0c5674992
--- /dev/null
+++ b/kernel/model/export_annotation_test.go
@@ -0,0 +1,175 @@
+package model
+
+import (
+ "bytes"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/88250/lute/ast"
+ "github.com/siyuan-note/siyuan/kernel/conf"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func TestExportTreeMissingFileAnnotationRef(t *testing.T) {
+ fixture := setupFileOperationTest(t)
+ originalWorkspaceDir := util.WorkspaceDir
+ util.WorkspaceDir = filepath.Dir(util.DataDir)
+ t.Cleanup(func() { util.WorkspaceDir = originalWorkspaceDir })
+ Conf.Export, Conf.Editor = conf.NewExport(), conf.NewEditor()
+ assetPath := filepath.Join(util.DataDir, "assets", "document.pdf")
+ if err := os.MkdirAll(filepath.Dir(assetPath), 0755); err != nil {
+ t.Fatal(err)
+ }
+ for _, file := range []struct{ path, data string }{{assetPath, "PDF"}, {assetPath + ".sya", `{}`}} {
+ if err := os.WriteFile(file.path, []byte(file.data), 0644); err != nil {
+ t.Fatal(err)
+ }
+ }
+ for _, syaExists := range []bool{true, false} {
+ if !syaExists {
+ if err := os.Remove(assetPath + ".sya"); err != nil {
+ t.Fatal(err)
+ }
+ }
+ for _, wysiwyg := range []bool{false, true} {
+ for _, mode := range []int{0, 1} {
+ tree := treenode.NewTree(fixture.box.ID, "/"+ast.NewNodeID()+".sy", "/Annotation", "Annotation")
+ node := &ast.Node{Type: ast.NodeTextMark, TextMarkType: "file-annotation-ref",
+ TextMarkFileAnnotationRefID: "assets/document.pdf/20260817235351-rp33lbv", TextMarkTextContent: "retained annotation"}
+ tree.Root.FirstChild.AppendChild(node)
+ exported, err := exportTree(tree, true, true, wysiwyg, true,
+ 0, 0, mode, "#", "#", "", "", false, "", false, true, nil, nil)
+ if err != nil {
+ t.Fatalf("wysiwyg=%v mode=%d: %v", wysiwyg, mode, err)
+ }
+ foundText := false
+ ast.Walk(exported.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
+ if entering {
+ if n.Type == ast.NodeText && string(n.Tokens) == "retained annotation" {
+ foundText = true
+ }
+ if n.Type == ast.NodeLink || n.IsTextMarkType("file-annotation-ref") {
+ t.Fatal("export retained a dangling annotation link")
+ }
+ }
+ return ast.WalkContinue
+ })
+ if !foundText {
+ t.Fatal("export lost annotation text")
+ }
+ if node.TextMarkFileAnnotationRefID == "" {
+ t.Fatal("export modified the source reference")
+ }
+ }
+ }
+ }
+}
+
+func TestProcessFileAnnotationRef(t *testing.T) {
+ const annotationID = "20260817235351-rp33lbv"
+ for _, format := range []string{"plain", "encrypted", "legacy encrypted"} {
+ t.Run(format, func(t *testing.T) {
+ fixture := setupFileOperationTest(t)
+ boxID := fixture.box.ID
+ var dek []byte
+ assetDir := filepath.Join(util.DataDir, "assets")
+ if format != "plain" {
+ boxID = "20261003000000-abcdefg"
+ t.Cleanup(prepareEncryptedBoxLifecycleTest(t, boxID))
+ var err error
+ dek, err = GetDEKIfUnlocked(boxID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { clear(dek) })
+ assetDir = filepath.Join(util.DataDir, boxID, "assets")
+ }
+ originalWorkspaceDir := util.WorkspaceDir
+ util.WorkspaceDir = filepath.Dir(util.DataDir)
+ t.Cleanup(func() { util.WorkspaceDir = originalWorkspaceDir })
+ if err := os.MkdirAll(assetDir, 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(assetDir, "document.pdf"), []byte("PDF"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ for _, scenario := range []struct {
+ name, data, wantDest string
+ wantErr bool
+ }{
+ {"deleted", `{}`, "", false},
+ {"page", `{"` + annotationID + `":{"page":2}}`, "assets/document.pdf#page=3", false},
+ {"pages", `{"` + annotationID + `":{"pages":[{"index":4}]}}`, "assets/document.pdf#page=5", false},
+ {"invalid page", `{"` + annotationID + `":{"page":-1}}`, "", true},
+ {"missing page", `{"` + annotationID + `":{}}`, "", true},
+ {"invalid JSON", `{broken`, "", true},
+ {"null JSON", `null`, "", true},
+ {"missing file", "", "", false},
+ {"corrupt ciphertext", `{}`, "", format != "plain"},
+ } {
+ t.Run(scenario.name, func(t *testing.T) {
+ syaPath := filepath.Join(assetDir, "document.pdf.sya")
+ data := []byte(scenario.data)
+ if len(data) > 0 && format != "plain" {
+ if format == "legacy encrypted" {
+ data = legacyEncryptedAssetFixture(t, boxID, "document.pdf.sya", "document.pdf.sya", dek, data)
+ } else {
+ var err error
+ data, err = EncryptAsset(boxID, "document.pdf.sya", "document.pdf.sya", dek, data)
+ if err != nil {
+ t.Fatal(err)
+ }
+ }
+ if scenario.name == "corrupt ciphertext" {
+ data[len(data)-5] ^= 1
+ }
+ }
+ if len(data) == 0 {
+ if err := os.Remove(syaPath); err != nil && !os.IsNotExist(err) {
+ t.Fatal(err)
+ }
+ } else if err := os.WriteFile(syaPath, data, 0644); err != nil {
+ t.Fatal(err)
+ }
+ for _, mode := range []int{0, 1} {
+ paragraph := &ast.Node{Type: ast.NodeParagraph}
+ node := &ast.Node{Type: ast.NodeTextMark, TextMarkType: "file-annotation-ref", TextMarkTextContent: "annotation text"}
+ paragraph.AppendChild(node)
+ refID := "assets/document.pdf/" + annotationID
+ err := processFileAnnotationRef(refID, node, mode, boxID)
+ if (err != nil) != scenario.wantErr {
+ t.Fatalf("mode=%d: unexpected error %v", mode, err)
+ }
+ if scenario.wantErr {
+ if paragraph.FirstChild != node {
+ t.Fatal("failed resolution produced exported content")
+ }
+ } else if scenario.wantDest == "" {
+ if text := paragraph.FirstChild; text.Type != ast.NodeText || string(text.Tokens) != node.TextMarkTextContent {
+ t.Fatal("deleted annotation did not retain its text")
+ }
+ } else {
+ link := paragraph.FirstChild
+ if link.Type != ast.NodeLink || string(link.ChildByType(ast.NodeLinkDest).Tokens) != scenario.wantDest {
+ t.Fatal("valid annotation lost its page link")
+ }
+ text := string(link.ChildByType(ast.NodeLinkText).Tokens)
+ if !strings.HasSuffix(text, node.TextMarkTextContent) || (mode == 1 && text != node.TextMarkTextContent) {
+ t.Fatalf("invalid anchor text %q", text)
+ }
+ }
+ if len(data) > 0 {
+ stored, err := os.ReadFile(syaPath)
+ if err != nil || !bytes.Equal(stored, data) {
+ t.Fatalf("export modified the annotation file: %v", err)
+ }
+ }
+ }
+ })
+ }
+ })
+ }
+}