From 19082e9d5f1ee48acde0a21a062ae5f20545f8b2 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Thu, 12 Jun 2025 18:08:44 +0800 Subject: [PATCH] :art: Improve export of empty documents with subdocuments https://github.com/siyuan-note/siyuan/issues/15009 --- kernel/model/export.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/kernel/model/export.go b/kernel/model/export.go index 81466c135..33e667589 100644 --- a/kernel/model/export.go +++ b/kernel/model/export.go @@ -1993,13 +1993,13 @@ func ExportMarkdownContent(id string, refMode, embedMode int, addYfm bool) (hPat return } -func exportMarkdownContent(id, ext string, exportRefMode int, defBlockIDs []string, singleFile bool, treeCache *map[string]*parse.Tree) (hPath, exportedMd string) { +func exportMarkdownContent(id, ext string, exportRefMode int, defBlockIDs []string, singleFile bool, treeCache *map[string]*parse.Tree) (tree *parse.Tree, exportedMd string, isEmpty bool) { tree, err := loadTreeWithCache(id, treeCache) if err != nil { logging.LogErrorf("load tree by block id [%s] failed: %s", id, err) return } - hPath = tree.HPath + isEmpty = nil == tree.Root.FirstChild.FirstChild exportedMd = exportMarkdownContent0(tree, "", false, ext, exportRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode, Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker, @@ -3128,7 +3128,11 @@ func exportPandocConvertZip(baseFolderName string, docPaths, defBlockIDs []strin luteEngine := util.NewLute() for i, p := range docPaths { id := util.GetTreeID(p) - hPath, md := exportMarkdownContent(id, ext, exportRefMode, defBlockIDs, false, treeCache) + tree, md, isEmpty := exportMarkdownContent(id, ext, exportRefMode, defBlockIDs, false, treeCache) + if nil == tree { + continue + } + hPath := tree.HPath dir, name = path.Split(hPath) dir = util.FilterFilePath(dir) // 导出文档时未移除不支持的文件名符号 https://github.com/siyuan-note/siyuan/issues/4590 name = util.FilterFileName(name) @@ -3147,8 +3151,17 @@ func exportPandocConvertZip(baseFolderName string, docPaths, defBlockIDs []strin continue } + if isEmpty { + entries, readErr := os.ReadDir(filepath.Join(util.DataDir, tree.Box, strings.TrimSuffix(tree.Path, ".sy"))) + if nil == readErr && 0 < len(entries) { + // 如果文档内容为空并且存在子文档则仅导出文件夹 + // Improve export of empty documents with subdocuments https://github.com/siyuan-note/siyuan/issues/15009 + continue + } + } + // 解析导出后的标准 Markdown,汇总 assets - tree := parse.Parse("", gulu.Str.ToBytes(md), luteEngine.ParseOptions) + tree = parse.Parse("", gulu.Str.ToBytes(md), luteEngine.ParseOptions) var assets []string assets = append(assets, assetsLinkDestsInTree(tree)...) for _, asset := range assets {