Skip to content

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

:bug: Preserve footnotes for documents excluded from Markdown exports https://github.com/siyuan-note/siyuan/issues/20063

Daniel6616c82e0b92
diff --git a/kernel/model/asset_download_read.go b/kernel/model/asset_download_read.go
index 782468b7a1..f7bd78c5c7 100644
--- a/kernel/model/asset_download_read.go
+++ b/kernel/model/asset_download_read.go
@@ -219,7 +219,7 @@ func prepareExportAssetsInScope(boxID string, docPaths []string, includeFootnote
order := []string{}
footnotes := map[string]*refAsFootnotes{}
depth := 0
- collectFootnotesDefs0(tree, tree.Root, &order, footnotes, &depth)
+ collectFootnotesDefs0(tree, tree.Root, &order, footnotes, &depth, nil)
defs, defsErr := resolveFootnotesDefs(&order, footnotes, tree, map[string]bool{},
Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight, false)
if defsErr != nil {
diff --git a/kernel/model/asset_download_regression_test.go b/kernel/model/asset_download_regression_test.go
index a628e8faac..e8d1b0adaf 100644
--- a/kernel/model/asset_download_regression_test.go
+++ b/kernel/model/asset_download_regression_test.go
@@ -78,7 +78,7 @@ func TestAssetDownloadFootnoteExport(t *testing.T) {
Conf.Export.BlockRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
- Conf.Export.AddTitle, "", Conf.Export.InlineMemo, true, true, nil)
+ Conf.Export.AddTitle, "", Conf.Export.InlineMemo, true, nil, nil)
if prepareErr != nil {
t.Fatal(prepareErr)
}
diff --git a/kernel/model/export.go b/kernel/model/export.go
index d0914ddc67..091611826f 100644
--- a/kernel/model/export.go
+++ b/kernel/model/export.go
@@ -481,7 +481,7 @@ func Export2Liandi(id string) (err error) {
".md", 3, 1, 1,
"#", "#",
"", "",
- false, false, nil, true, false, nil, nil)
+ false, false, nil, nil, false, nil, nil)
result := gulu.Ret.NewResult()
request := httpclient.NewCloudRequest30s()
request = request.
@@ -1045,7 +1045,7 @@ func ExportPreview(id string, fillCSSVar bool, avPublishFilter AVExportPublishFi
blockRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
"#", "#", // 这里固定使用 # 包裹标签,否则无法正确解析标签 https://github.com/siyuan-note/siyuan/issues/13857
Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
- Conf.Export.AddTitle, "", Conf.Export.InlineMemo, true, true, avPublishFilter, accessChecker...)
+ Conf.Export.AddTitle, "", Conf.Export.InlineMemo, true, nil, avPublishFilter, accessChecker...)
if nil != exportTreeErr {
return exportTreeErr
}
@@ -1234,7 +1234,7 @@ func exportMarkdownHTML(id, savePath string, docx, merge bool, mergeHeadingOptio
blockRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
- Conf.Export.AddTitle, "", Conf.Export.InlineMemo, true, true, nil)
+ Conf.Export.AddTitle, "", Conf.Export.InlineMemo, true, nil, nil)
if nil != exportTreeErr {
return exportTreeErr
}
@@ -1415,7 +1415,7 @@ func exportHTMLWithTitle(id, savePath string, pdf, keepFold, merge, addTitle boo
blockRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
- addTitle, customTitle, Conf.Export.InlineMemo, true, true, nil)
+ addTitle, customTitle, Conf.Export.InlineMemo, true, nil, nil)
if nil != exportTreeErr {
return exportTreeErr
}
@@ -2140,7 +2140,7 @@ func ExportStdMarkdown(id string, assetsDestSpace2Underscore, fillCSSVar, adjust
".md", Conf.Export.BlockRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
- Conf.Export.AddTitle, Conf.Export.InlineMemo, defBlockIDs, true, fillCSSVar, nil, avPublishFilter, accessChecker...)
+ Conf.Export.AddTitle, Conf.Export.InlineMemo, defBlockIDs, nil, fillCSSVar, nil, avPublishFilter, accessChecker...)
return nil
}); exportErr != nil {
logging.LogErrorf("export std markdown [%s] failed: %s", id, exportErr)
@@ -3063,7 +3063,7 @@ func ExportMarkdownContent(id string, refMode, embedMode int, addYfm, fillCSSVar
".md", refMode, embedMode, Conf.Export.FileAnnotationRefMode,
Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
- addTitle, Conf.Export.InlineMemo, nil, true, fillCSSVar, nil, nil)
+ addTitle, Conf.Export.InlineMemo, nil, nil, fillCSSVar, nil, nil)
docIAL := parse.IAL2Map(tree.Root.KramdownIAL)
if addYfm {
exportedMd = yfm(docIAL) + exportedMd
@@ -3076,7 +3076,7 @@ func ExportMarkdownContent(id string, refMode, embedMode int, addYfm, fillCSSVar
return
}
-func exportMarkdownContent(rootID, ext string, exportRefMode int, defBlockIDs []string, singleFile bool,
+func exportMarkdownContent(rootID, ext string, exportRefMode int, defBlockIDs []string, references *markdownExportReferences,
boxPaths map[string]string) (tree *parse.Tree, exportedMd string, isEmpty bool) {
tree, err := LoadTreeByBlockID(rootID)
if err != nil {
@@ -3103,7 +3103,7 @@ func exportMarkdownContent(rootID, ext string, exportRefMode int, defBlockIDs []
ext, exportRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
- Conf.Export.AddTitle, Conf.Export.InlineMemo, defBlockIDs, singleFile, false, boxPaths, nil)
+ Conf.Export.AddTitle, Conf.Export.InlineMemo, defBlockIDs, references, false, boxPaths, nil)
docIAL := parse.IAL2Map(tree.Root.KramdownIAL)
if Conf.Export.MarkdownYFM {
// 导出 Markdown 时在文档头添加 YFM 开关 https://github.com/siyuan-note/siyuan/issues/7727
@@ -3115,13 +3115,13 @@ func exportMarkdownContent(rootID, ext string, exportRefMode int, defBlockIDs []
func exportMarkdownContent0(id string, tree *parse.Tree, cloudAssetsBase string, assetsDestSpace2Underscore, adjustHeadingLv, imgTag bool,
ext string, blockRefMode, blockEmbedMode, fileAnnotationRefMode int,
tagOpenMarker, tagCloseMarker string, blockRefTextLeft, blockRefTextRight string,
- addTitle, inlineMemo bool, defBlockIDs []string, singleFile, fillCSSVar bool, boxPaths map[string]string,
+ addTitle, inlineMemo bool, defBlockIDs []string, references *markdownExportReferences, fillCSSVar bool, boxPaths map[string]string,
avPublishFilter AVExportPublishFilter, accessChecker ...EmbedBlockAccessChecker) (ret string) {
tree, exportTreeErr := exportTree(tree, false, false, false, false,
blockRefMode, blockEmbedMode, fileAnnotationRefMode,
tagOpenMarker, tagCloseMarker,
blockRefTextLeft, blockRefTextRight,
- addTitle, "", inlineMemo, 0 < len(defBlockIDs), singleFile, avPublishFilter, accessChecker...)
+ addTitle, "", inlineMemo, 0 < len(defBlockIDs), references, avPublishFilter, accessChecker...)
if nil != exportTreeErr {
logging.LogErrorf("prepare Markdown export failed: %s", exportTreeErr)
return ""
@@ -3184,7 +3184,7 @@ func exportMarkdownContent0(id string, tree *parse.Tree, cloudAssetsBase string,
}
if 4 == blockRefMode { // 脚注+锚点哈希
- if n.IsBlock() && gulu.Str.Contains(n.ID, defBlockIDs) {
+ if n.IsBlock() && (gulu.Str.Contains(n.ID, defBlockIDs) || references != nil && references.anchorIDs[n.ID]) {
// 如果是定义块,则在开头处添加锚点
anchorSpan := treenode.NewSpanAnchor(n.ID)
if ast.NodeDocument != n.Type {
@@ -3209,10 +3209,10 @@ func exportMarkdownContent0(id string, tree *parse.Tree, cloudAssetsBase string,
if treenode.IsBlockRef(n) {
// 如果是引用元素,则将其转换为超链接,指向 xxx.md#block-id
defID, linkText := getExportBlockRefLinkText(n, blockRefTextLeft, blockRefTextRight, tree.Box)
- if gulu.Str.Contains(defID, defBlockIDs) {
+ if references.contains(defID, tree.Box) || references == nil && gulu.Str.Contains(defID, defBlockIDs) {
var href string
bt := getExportBlockTreeInBox(defID, tree.Box)
- if len(boxPaths) > 0 {
+ if len(boxPaths) > 0 || references != nil && !IsEncryptedBox(tree.Box) {
bt = getExportBlockTree(defID)
}
if nil != bt {
@@ -3277,7 +3277,7 @@ func exportTree(tree *parse.Tree, wysiwyg, richTableCells, keepFold, avHiddenCol
blockRefMode, blockEmbedMode, fileAnnotationRefMode int,
tagOpenMarker, tagCloseMarker string,
blockRefTextLeft, blockRefTextRight string,
- addTitle bool, customTitle string, inlineMemo, addDocAnchorSpan, singleFile bool,
+ addTitle bool, customTitle string, inlineMemo, addDocAnchorSpan bool, references *markdownExportReferences,
avPublishFilter AVExportPublishFilter, accessChecker ...EmbedBlockAccessChecker) (ret *parse.Tree, err error) {
luteEngine := NewLute()
ret = tree
@@ -3300,9 +3300,9 @@ func exportTree(tree *parse.Tree, wysiwyg, richTableCells, keepFold, avHiddenCol
// 收集引用转脚注+锚点哈希(可能跨文档递归)
var refFootnoteOrder []string // 按顺序存储 defID
refFootnotesByID := make(map[string]*refAsFootnotes)
- if 4 == blockRefMode && singleFile {
+ if 4 == blockRefMode {
depth = 0
- collectFootnotesDefs0(ret, ret.Root, &refFootnoteOrder, refFootnotesByID, &depth)
+ collectFootnotesDefs0(ret, ret.Root, &refFootnoteOrder, refFootnotesByID, &depth, references)
}
currentTreeNodeIDs := map[string]bool{}
@@ -4162,7 +4162,7 @@ func blockLink2Ref(currentTree *parse.Tree) {
})
}
-func collectFootnotesDefs(currentTree *parse.Tree, id string, refFootnoteOrder *[]string, refFootnotesByID map[string]*refAsFootnotes, depth *int) {
+func collectFootnotesDefs(currentTree *parse.Tree, id string, refFootnoteOrder *[]string, refFootnotesByID map[string]*refAsFootnotes, depth *int, references *markdownExportReferences) {
*depth++
if 4096 < *depth {
return
@@ -4177,20 +4177,27 @@ func collectFootnotesDefs(currentTree *parse.Tree, id string, refFootnoteOrder *
logging.LogErrorf("not found node [%s] in tree [%s]", id, t.Root.ID)
return
}
- collectFootnotesDefs0(currentTree, node, refFootnoteOrder, refFootnotesByID, depth)
+ collectFootnotesDefs0(currentTree, node, refFootnoteOrder, refFootnotesByID, depth, references)
if ast.NodeHeading == node.Type {
children := treenode.HeadingChildren(node)
for _, c := range children {
- collectFootnotesDefs0(currentTree, c, refFootnoteOrder, refFootnotesByID, depth)
+ collectFootnotesDefs0(currentTree, c, refFootnoteOrder, refFootnotesByID, depth, references)
}
}
}
-func addRefFootnoteAndRecurse(currentTree *parse.Tree, defID, anchorText string, refFootnoteOrder *[]string, refFootnotesByID map[string]*refAsFootnotes, depth *int) {
+func addRefFootnoteAndRecurse(currentTree *parse.Tree, defID, anchorText string, refFootnoteOrder *[]string, refFootnotesByID map[string]*refAsFootnotes, depth *int, references *markdownExportReferences) {
+ if references.contains(defID, currentTree.Box) {
+ references.anchorIDs[defID] = true
+ return
+ }
if nil != refFootnotesByID[defID] {
return
}
if isNodeInTree(defID, currentTree) {
+ if references != nil {
+ references.anchorIDs[defID] = true
+ }
// 当前文档内不转换脚注,直接使用锚点哈希 https://github.com/siyuan-note/siyuan/issues/13283
return
}
@@ -4202,10 +4209,10 @@ func addRefFootnoteAndRecurse(currentTree *parse.Tree, defID, anchorText string,
refNum: strconv.Itoa(len(*refFootnoteOrder)),
refAnchorText: anchorText,
}
- collectFootnotesDefs(currentTree, defID, refFootnoteOrder, refFootnotesByID, depth)
+ collectFootnotesDefs(currentTree, defID, refFootnoteOrder, refFootnotesByID, depth, references)
}
-func collectFootnotesDefs0(currentTree *parse.Tree, node *ast.Node, refFootnoteOrder *[]string, refFootnotesByID map[string]*refAsFootnotes, depth *int) {
+func collectFootnotesDefs0(currentTree *parse.Tree, node *ast.Node, refFootnoteOrder *[]string, refFootnotesByID map[string]*refAsFootnotes, depth *int, references *markdownExportReferences) {
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering {
return ast.WalkContinue
@@ -4213,7 +4220,7 @@ func collectFootnotesDefs0(currentTree *parse.Tree, node *ast.Node, refFootnoteO
if treenode.IsBlockRef(n) {
defID, refText, _ := treenode.GetBlockRef(n)
- addRefFootnoteAndRecurse(currentTree, defID, refText, refFootnoteOrder, refFootnotesByID, depth)
+ addRefFootnoteAndRecurse(currentTree, defID, refText, refFootnoteOrder, refFootnotesByID, depth, references)
return ast.WalkSkipChildren
} else if treenode.IsBlockLink(n) {
defID := strings.TrimPrefix(n.TextMarkAHref, "siyuan://blocks/")
@@ -4225,7 +4232,7 @@ func collectFootnotesDefs0(currentTree *parse.Tree, node *ast.Node, refFootnoteO
anchorText = sql.GetRefText(defID)
}
}
- addRefFootnoteAndRecurse(currentTree, defID, anchorText, refFootnoteOrder, refFootnotesByID, depth)
+ addRefFootnoteAndRecurse(currentTree, defID, anchorText, refFootnoteOrder, refFootnotesByID, depth, references)
return ast.WalkSkipChildren
}
return ast.WalkContinue
@@ -4347,7 +4354,7 @@ func exportPandocConvertZip(boxID, baseFolderName string, docPaths, defBlockIDs
func exportPandocConvertZip0(boxID, baseFolderName string, docPaths, defBlockIDs []string, pandocFrom, pandocTo, ext string,
boxPaths map[string]string) (zipPath string) {
defer util.ClearPushProgress(100)
- if err := prepareExportAssets(boxID, docPaths); err != nil {
+ if err := prepareExportAssets(boxID, docPaths, true); err != nil {
util.PushErrMsg(err.Error(), 7000)
return
}
@@ -4390,6 +4397,16 @@ func exportPandocConvertZip0(boxID, baseFolderName string, docPaths, defBlockIDs
}
exportRefMode := Conf.Export.BlockRefMode
+ var references *markdownExportReferences
+ if exportRefMode == 4 {
+ var err error
+ references, err = prepareMarkdownExportReferences(docPaths, defBlockIDs)
+ if err != nil {
+ logging.LogErrorf("prepare Markdown export references failed: %s", err)
+ util.PushErrMsg(err.Error(), 7000)
+ return ""
+ }
+ }
wrotePathHash := map[string]string{}
assetsPathMap, err := allAssetAbsPaths()
if nil != err {
@@ -4402,7 +4419,7 @@ func exportPandocConvertZip0(boxID, baseFolderName string, docPaths, defBlockIDs
luteEngine.SetExportNormalizeTaskListMarker(true)
for i, p := range docPaths {
rootID := util.GetTreeID(p)
- tree, md, isEmpty := exportMarkdownContent(rootID, ext, exportRefMode, defBlockIDs, false, boxPaths)
+ tree, md, isEmpty := exportMarkdownContent(rootID, ext, exportRefMode, defBlockIDs, references, boxPaths)
if nil == tree {
continue
}
diff --git a/kernel/model/export_attribute_view_test.go b/kernel/model/export_attribute_view_test.go
index 16c078f3fa..2cfd30b968 100644
--- a/kernel/model/export_attribute_view_test.go
+++ b/kernel/model/export_attribute_view_test.go
@@ -126,7 +126,7 @@ func TestAttributeViewExportDoesNotCrossEncryptedBoundary(t *testing.T) {
t.Fatal("CSV export crossed from a normal document to an encrypted attribute view")
}
- exported, exportErr := exportTree(tree, false, false, false, true, 0, 0, 0, "", "", "", "", false, "", false, false, false, nil)
+ exported, exportErr := exportTree(tree, false, false, false, true, 0, 0, 0, "", "", "", "", false, "", false, false, nil, nil)
if exportErr != nil {
t.Fatal(exportErr)
}
diff --git a/kernel/model/export_footnotes_test.go b/kernel/model/export_footnotes_test.go
index 328937668f..dd46e2491b 100644
--- a/kernel/model/export_footnotes_test.go
+++ b/kernel/model/export_footnotes_test.go
@@ -41,7 +41,7 @@ func TestExportFootnotesAcrossNotebooks(t *testing.T) {
writeExportRelatedTestTree(t, tree)
}
exported, err := exportTree(prepareExportTree(getExportBlockTree(source.ID)), true, true, false, true,
- 4, 0, 0, "#", "#", "", "", false, "", false, true, true, nil)
+ 4, 0, 0, "#", "#", "", "", false, "", false, true, nil, nil)
if err != nil {
t.Fatal(err)
}
@@ -74,7 +74,7 @@ func TestExportFootnotesRejectCrossCryptoBoundary(t *testing.T) {
var order []string
refs := map[string]*refAsFootnotes{}
depth := 0
- collectFootnotesDefs0(source, source.Root, &order, refs, &depth)
+ collectFootnotesDefs0(source, source.Root, &order, refs, &depth, nil)
if len(order) != 1 || order[0] != target.Root.FirstChild.ID {
t.Fatalf("collected references beyond the crypto boundary: %v", order)
}
@@ -113,7 +113,7 @@ func TestExportFootnotesStayWithinBlockScope(t *testing.T) {
t.Run(scenario.name, func(t *testing.T) {
tree := prepareExportTree(getExportBlockTree(scenario.id))
exported, err := exportTree(tree, true, true, false, true,
- 4, 0, 0, "#", "#", "", "", false, "", false, true, true, nil)
+ 4, 0, 0, "#", "#", "", "", false, "", false, true, nil, nil)
if err != nil {
t.Fatal(err)
}
@@ -231,7 +231,7 @@ func TestExportFootnotesHeadingAndContainerScopes(t *testing.T) {
} {
t.Run(scenario.name, func(t *testing.T) {
tree, err := exportTree(prepareExportTree(getExportBlockTree(scenario.id)), true, true, false, true,
- 4, 0, 0, "#", "#", "", "", false, "", false, true, true, nil)
+ 4, 0, 0, "#", "#", "", "", false, "", false, true, nil, nil)
if err != nil {
t.Fatal(err)
}
@@ -265,7 +265,7 @@ func TestExportFootnotesUseMaterializedTableContent(t *testing.T) {
writeAssetDownloadDocumentTest(t, tree)
}
exported, err := exportTree(prepareExportTree(getExportBlockTree(table.ID)), true, true, false, true,
- 4, 0, 0, "#", "#", "", "", false, "", false, true, true, nil)
+ 4, 0, 0, "#", "#", "", "", false, "", false, true, nil, nil)
if err != nil {
t.Fatal(err)
}
diff --git a/kernel/model/export_markdown_refs.go b/kernel/model/export_markdown_refs.go
new file mode 100644
index 0000000000..22b78b3784
--- /dev/null
+++ b/kernel/model/export_markdown_refs.go
@@ -0,0 +1,61 @@
+// SiYuan - From thought to insight, with agents
+// Copyright (c) 2020-present, b3log.org
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package model
+
+import (
+ "github.com/88250/lute/parse"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+// markdownExportReferences 按本次导出的文档集合区分文件链接和脚注,并记录正文及脚注需要的锚点。
+// 单文件预览和内容导出使用 nil,外部引用全部按脚注处理。
+type markdownExportReferences struct {
+ docIDs map[string]bool
+ anchorIDs map[string]bool
+}
+
+func (refs *markdownExportReferences) contains(defID, sourceBoxID string) bool {
+ if refs == nil {
+ return false
+ }
+ var bt *treenode.BlockTree
+ if IsEncryptedBox(sourceBoxID) {
+ bt = treenode.GetBlockTreeInExactBox(defID, sourceBoxID)
+ } else {
+ bt = treenode.GetBlockTree(defID)
+ }
+ return bt != nil && IsSameCryptoBoundary(sourceBoxID, bt.BoxID) && refs.docIDs[bt.RootID]
+}
+
+// prepareMarkdownExportReferences 在输出任何文件前收集锚点,避免脚注引用的目标已先行输出而缺失锚点。
+func prepareMarkdownExportReferences(docPaths []string, defBlockIDs []string) (*markdownExportReferences, error) {
+ refs := &markdownExportReferences{docIDs: map[string]bool{}, anchorIDs: map[string]bool{}}
+ for _, docPath := range docPaths {
+ refs.docIDs[util.GetTreeID(docPath)] = true
+ }
+ for _, id := range defBlockIDs {
+ refs.anchorIDs[id] = true
+ }
+ for _, docPath := range docPaths {
+ tree, err := LoadTreeByBlockID(util.GetTreeID(docPath))
+ if err != nil {
+ return nil, err
+ }
+ depth := 0
+ resolveEmbedR(tree.Root, Conf.Export.BlockEmbedMode, NewLute(), &[]string{}, &depth)
+ collectMarkdownExportReferences(tree, refs)
+ }
+ return refs, nil
+}
+
+func collectMarkdownExportReferences(tree *parse.Tree, refs *markdownExportReferences) {
+ finishTabTitles := treenode.MaterializeTabTitles(tree.Root)
+ defer finishTabTitles()
+ var order []string
+ footnotes := map[string]*refAsFootnotes{}
+ depth := 0
+ collectFootnotesDefs0(tree, tree.Root, &order, footnotes, &depth, refs)
+}
diff --git a/kernel/model/export_markdown_refs_test.go b/kernel/model/export_markdown_refs_test.go
new file mode 100644
index 0000000000..f0a319cfc4
--- /dev/null
+++ b/kernel/model/export_markdown_refs_test.go
@@ -0,0 +1,247 @@
+//go:build fts5
+
+package model
+
+import (
+ "archive/zip"
+ "os"
+ "path"
+ "path/filepath"
+ "strconv"
+ "strings"
+ "testing"
+
+ "github.com/88250/lute/ast"
+ "github.com/88250/lute/parse"
+ "github.com/siyuan-note/siyuan/kernel/conf"
+ "github.com/siyuan-note/siyuan/kernel/sql"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func setupMarkdownReferencesTest(t *testing.T, boxIDs ...string) {
+ t.Helper()
+ setupExportPublishTest(t)
+ Conf.Export.IncludeSubDocs = false
+ Conf.Export.IncludeRelatedDocs = false
+ for _, boxID := range boxIDs {
+ boxConf := conf.NewBoxConf()
+ boxConf.Name, boxConf.Closed = boxID, false
+ if err := (&Box{ID: boxID}).SaveConf(boxConf); err != nil {
+ t.Fatal(err)
+ }
+ markRuntimeNormalBox(boxID)
+ t.Cleanup(func() { forgetRuntimeNormalBox(boxID) })
+ }
+}
+
+func markdownArchiveDocument(t *testing.T, archive *zip.ReadCloser, title string) string {
+ t.Helper()
+ for _, file := range archive.File {
+ if path.Base(file.Name) == title+".md" {
+ return readZipFile(t, file, file.Name)
+ }
+ }
+ t.Fatalf("missing Markdown document %q", title)
+ return ""
+}
+
+func TestExportMarkdownReferenceScope(t *testing.T) {
+ for _, scenario := range []struct {
+ name string
+ includeSubDocs bool
+ includeRelated bool
+ selectTarget bool
+ wantDocs int
+ wantFootnotes int
+ }{
+ {"single document", false, false, false, 1, 2},
+ {"selected documents", false, false, true, 2, 1},
+ {"include children", true, false, false, 2, 1},
+ {"include related", false, true, false, 3, 0},
+ } {
+ t.Run(scenario.name, func(t *testing.T) {
+ const boxID = "20261003000000-box0001"
+ setupMarkdownReferencesTest(t, boxID)
+ source := newFootnoteTestDoc(t, boxID, "Source")
+ target := newFootnoteTestDoc(t, boxID, "Target")
+ target.Path, target.HPath = "/"+source.ID+"/"+target.ID+".sy", "/Source/Target"
+ external := newFootnoteTestDoc(t, boxID, "External")
+ source.Root.FirstChild.AppendChild(newFootnoteTestRef(target.Root.FirstChild.ID))
+ source.Root.FirstChild.AppendChild(newFootnoteTestRef(external.Root.FirstChild.ID))
+ local := treenode.NewParagraph(ast.NewNodeID())
+ local.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte("local target")})
+ source.Root.AppendChild(local)
+ source.Root.FirstChild.AppendChild(newFootnoteTestRef(local.ID))
+ target.Root.FirstChild.AppendChild(newFootnoteTestRef(external.Root.FirstChild.ID))
+ external.Root.FirstChild.AppendChild(newFootnoteTestRef(target.Root.FirstChild.ID))
+ external.Root.FirstChild.AppendChild(newFootnoteTestRef(local.ID))
+ for _, tree := range []*parse.Tree{source, target, external} {
+ writeAssetDownloadDocumentTest(t, tree)
+ sql.IndexTreeQueue(tree)
+ }
+ sql.FlushQueue()
+ Conf.Export.IncludeSubDocs, Conf.Export.IncludeRelatedDocs = scenario.includeSubDocs, scenario.includeRelated
+ ids := []string{source.ID}
+ if scenario.selectTarget {
+ ids = append(ids, target.ID)
+ }
+ _, zipPath := ExportPandocConvertZip(ids, "", ".md")
+ archive := openExportArchive(t, zipPath)
+ md := markdownArchiveDocument(t, archive, "Source")
+ if got := strings.Count(md, "]: "); got != scenario.wantFootnotes {
+ t.Fatalf("expected %d footnotes, got %d: %s", scenario.wantFootnotes, got, md)
+ }
+ if !strings.Contains(md, "](#"+local.ID+")") || !strings.Contains(md, `id="`+local.ID+`"`) {
+ t.Fatalf("lost local reference or anchor: %s", md)
+ }
+ if scenario.wantDocs > 1 {
+ if !strings.Contains(md, "Source/Target.md#"+target.Root.FirstChild.ID) {
+ t.Fatalf("exported target did not retain its file link: %s", md)
+ }
+ } else if strings.Contains(md, "Target.md") || strings.Contains(md, "External.md") {
+ t.Fatalf("link points to a document outside the archive: %s", md)
+ }
+ docCount := 0
+ for _, file := range archive.File {
+ if strings.HasSuffix(file.Name, ".md") {
+ docCount++
+ }
+ }
+ if docCount != scenario.wantDocs {
+ t.Fatalf("expected %d documents, got %d", scenario.wantDocs, docCount)
+ }
+ })
+ }
+}
+
+func TestExportMarkdownFootnoteLinksToEarlierDocument(t *testing.T) {
+ const boxID = "20261003000001-box0002"
+ setupMarkdownReferencesTest(t, boxID)
+ first := newFootnoteTestDoc(t, boxID, "First")
+ source := newFootnoteTestDoc(t, boxID, "Source")
+ external := newFootnoteTestDoc(t, boxID, "External")
+ source.Root.FirstChild.AppendChild(newFootnoteTestRef(external.Root.FirstChild.ID))
+ external.Root.FirstChild.AppendChild(newFootnoteTestRef(first.Root.FirstChild.ID))
+ for _, tree := range []*parse.Tree{first, source, external} {
+ writeAssetDownloadDocumentTest(t, tree)
+ }
+ zipPath := exportPandocConvertZip(boxID, "Ordered", []string{first.Path, source.Path}, nil, "", "", ".md")
+ archive := openExportArchive(t, zipPath)
+ md := markdownArchiveDocument(t, archive, "Source")
+ if !strings.Contains(md, "[^1]:") || !strings.Contains(md, "First.md#"+first.Root.FirstChild.ID) || strings.Contains(md, "[^2]") {
+ t.Fatalf("footnote did not link to the exported document: %s", md)
+ }
+ if firstMD := markdownArchiveDocument(t, archive, "First"); !strings.Contains(firstMD, `id="`+first.Root.FirstChild.ID+`"`) {
+ t.Fatalf("earlier document is missing the footnote target anchor: %s", firstMD)
+ }
+}
+
+func TestExportMarkdownFootnoteAssetsAndCrossNotebook(t *testing.T) {
+ const sourceBox = "20261003000002-box0003"
+ const targetBox = "20261003000003-box0004"
+ setupMarkdownReferencesTest(t, sourceBox, targetBox)
+ source := newFootnoteTestDoc(t, sourceBox, "Source")
+ target := newFootnoteTestDoc(t, targetBox, "Target")
+ source.Root.FirstChild.AppendChild(newFootnoteTestRef(target.ID))
+ asset := parse.Parse("", []byte("![image](assets/footnote.png)"), util.NewLute().ParseOptions)
+ target.Root.AppendChild(asset.Root.FirstChild)
+ for _, tree := range []*parse.Tree{source, target} {
+ writeAssetDownloadDocumentTest(t, tree)
+ }
+ assetPath := filepath.Join(util.DataDir, "assets", "footnote.png")
+ if err := os.MkdirAll(filepath.Dir(assetPath), 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(assetPath, []byte("footnote image"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ _, zipPath := ExportPandocConvertZip([]string{source.ID}, "", ".md")
+ archive := openExportArchive(t, zipPath)
+ md := markdownArchiveDocument(t, archive, "Source")
+ if !strings.Contains(md, "[^1]:") || !strings.Contains(md, "Target") || !strings.Contains(md, "assets/footnote.png") {
+ t.Fatalf("cross-notebook footnote lost content: %s", md)
+ }
+ if got := readArchiveFile(t, archive, "assets/footnote.png"); got != "footnote image" {
+ t.Fatalf("unexpected footnote asset: %s", got)
+ }
+ archive = openExportArchive(t, ExportNotebooksMarkdown([]string{sourceBox, targetBox}))
+ md = markdownArchiveDocument(t, archive, "Source")
+ if strings.Contains(md, "[^1]") || !strings.Contains(md, "../"+targetBox+"/Target.md") {
+ t.Fatalf("included cross-notebook document should use a file link: %s", md)
+ }
+}
+
+func TestExportMarkdownFootnoteDeferredAsset(t *testing.T) {
+ setupExportPublishTest(t)
+ fixture, _, _, _ := prepareAssetDownloadDocumentTest(t)
+ Conf.Search = conf.NewSearch()
+ Conf.Export.IncludeSubDocs, Conf.Export.IncludeRelatedDocs = false, false
+ source := newFootnoteTestDoc(t, fixture.box.ID, "Source")
+ target := newFootnoteTestDoc(t, fixture.box.ID, "Target")
+ source.Root.FirstChild.AppendChild(newFootnoteTestRef(target.Root.FirstChild.ID))
+ asset := parse.Parse("", []byte("![image](assets/file.bin)"), util.NewLute().ParseOptions)
+ target.Root.FirstChild.AppendChild(asset.Root.FirstChild.FirstChild)
+ for _, tree := range []*parse.Tree{source, target} {
+ writeAssetDownloadDocumentTest(t, tree)
+ }
+ if _, err := os.Stat(filepath.Join(util.DataDir, "assets", "file.bin")); !os.IsNotExist(err) {
+ t.Fatalf("fixture asset must start deferred: %v", err)
+ }
+ _, zipPath := ExportPandocConvertZip([]string{source.ID}, "", ".md")
+ archive := openExportArchive(t, zipPath)
+ if got := readArchiveFile(t, archive, "assets/file.bin"); got != "version one" {
+ t.Fatalf("deferred footnote asset was not exported: %q", got)
+ }
+}
+
+func TestExportMarkdownFootnotesRespectCryptoBoundary(t *testing.T) {
+ const sourceBox = "20261003000004-box0005"
+ const targetBox = "20261003000005-box0006"
+ setupMarkdownReferencesTest(t, sourceBox, targetBox)
+ source := newFootnoteTestDoc(t, sourceBox, "Source")
+ target := newFootnoteTestDoc(t, targetBox, "Private")
+ source.Root.FirstChild.AppendChild(newFootnoteTestRef(target.ID))
+ for _, tree := range []*parse.Tree{source, target} {
+ writeAssetDownloadDocumentTest(t, tree)
+ }
+ markRuntimeEncryptedBox(targetBox)
+ t.Cleanup(func() { forgetRuntimeEncryptedBox(targetBox) })
+ refs := &markdownExportReferences{docIDs: map[string]bool{source.ID: true, target.ID: true}, anchorIDs: map[string]bool{}}
+ if refs.contains(target.ID, sourceBox) {
+ t.Fatal("export membership bypassed the crypto boundary")
+ }
+ _, zipPath := ExportPandocConvertZip([]string{source.ID}, "", ".md")
+ archive := openExportArchive(t, zipPath)
+ md := markdownArchiveDocument(t, archive, "Source")
+ if strings.Contains(md, "Private") || strings.Contains(md, ".md#") {
+ t.Fatalf("export traversed the crypto boundary: %s", md)
+ }
+ if len(archive.File) != 1 {
+ t.Fatalf("export included content beyond the crypto boundary: %v", archive.File)
+ }
+}
+
+func TestExportMarkdownOtherReferenceModes(t *testing.T) {
+ for _, mode := range []int{2, 3} {
+ t.Run(strconv.Itoa(mode), func(t *testing.T) {
+ const boxID = "20261003000006-box0007"
+ setupMarkdownReferencesTest(t, boxID)
+ Conf.Export.BlockRefMode = mode
+ source := newFootnoteTestDoc(t, boxID, "Source")
+ target := newFootnoteTestDoc(t, boxID, "Target")
+ source.Root.FirstChild.AppendChild(newFootnoteTestRef(target.Root.FirstChild.ID))
+ for _, tree := range []*parse.Tree{source, target} {
+ writeAssetDownloadDocumentTest(t, tree)
+ }
+ _, zipPath := ExportPandocConvertZip([]string{source.ID}, "", ".md")
+ md := markdownArchiveDocument(t, openExportArchive(t, zipPath), "Source")
+ if strings.Contains(md, "[^") || strings.Contains(md, "Target.md") || !strings.Contains(md, "reference") {
+ t.Fatalf("unexpected reference mode %d output: %s", mode, md)
+ }
+ if strings.Contains(md, "siyuan://blocks/"+target.Root.FirstChild.ID) != (mode == 2) {
+ t.Fatalf("reference mode %d lost its link behavior: %s", mode, md)
+ }
+ })
+ }
+}
diff --git a/kernel/model/file_annotation_test.go b/kernel/model/file_annotation_test.go
index c0b676bc7c..89de99f4b6 100644
--- a/kernel/model/file_annotation_test.go
+++ b/kernel/model/file_annotation_test.go
@@ -170,7 +170,7 @@ func TestFileAnnotationExportRejectsInvalidData(t *testing.T) {
root := &ast.Node{Type: ast.NodeDocument, ID: "20260912000000-root001"}
root.AppendChild(node.Parent)
tree := &parse.Tree{Root: root, ID: root.ID}
- if _, err := exportTree(tree, false, false, false, false, 0, 0, 0, "#", "#", "", "", false, "", false, false, true, nil); err == nil {
+ if _, err := exportTree(tree, false, false, false, false, 0, 0, 0, "#", "#", "", "", false, "", false, false, nil, nil); err == nil {
t.Fatalf("invalid annotation data exported: %s", data)
}
if node.Parent == nil || node.Previous != nil {
diff --git a/kernel/model/table_cell_rich_test.go b/kernel/model/table_cell_rich_test.go
index dd9d8f394f..b0a05842ee 100644
--- a/kernel/model/table_cell_rich_test.go
+++ b/kernel/model/table_cell_rich_test.go
@@ -298,7 +298,7 @@ func TestTableCellRichFootnoteExport(t *testing.T) {
TextMarkBlockRefID: table.ID, TextMarkBlockRefSubtype: "s", TextMarkTextContent: "Rich target"})
writeAssetDownloadDocumentTest(t, referring)
exported, err := exportTree(prepareExportTree(getExportBlockTree(referring.ID)), true, true, false, true,
- 4, 0, 0, "#", "#", "", "", false, "", false, true, true, nil)
+ 4, 0, 0, "#", "#", "", "", false, "", false, true, nil, nil)
if err != nil {
t.Fatal(err)
}