Skip to content

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

:art: Export related flashcard data when exporting .sy.zip https://github.com/siyuan-note/siyuan/issues/9372

Daniela8806968d051
diff --git a/kernel/model/export.go b/kernel/model/export.go
index 212e62261d..08c70152f3 100644
--- a/kernel/model/export.go
+++ b/kernel/model/export.go
@@ -46,6 +46,7 @@ import (
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/logging"
+ "github.com/siyuan-note/riff"
"github.com/siyuan-note/siyuan/kernel/av"
"github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/sql"
@@ -1382,6 +1383,27 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) (
})
}
+ // 导出闪卡 Export related flashcard data when exporting .sy.zip https://github.com/siyuan-note/siyuan/issues/9372
+ exportStorageRiffDir := filepath.Join(exportFolder, "storage", "riff")
+ deckID := ast.NewNodeID()
+ deck, loadErr := riff.LoadDeck(exportStorageRiffDir, deckID, Conf.Flashcard.RequestRetention, Conf.Flashcard.MaximumInterval, Conf.Flashcard.Weights)
+ for _, tree := range trees {
+ cards := getTreeFlashcards(tree.ID)
+ if nil != loadErr {
+ logging.LogErrorf("load deck [%s] failed: %s", name, loadErr)
+ continue
+ }
+
+ for _, card := range cards {
+ deck.AddCard(card.ID(), card.BlockID())
+ }
+ }
+ if 0 < deck.CountCards() {
+ if saveErr := deck.Save(); nil != saveErr {
+ logging.LogErrorf("save deck [%s] failed: %s", name, saveErr)
+ }
+ }
+
// 导出自定义排序
sortPath := filepath.Join(util.DataDir, box.ID, ".siyuan", "sort.json")
fullSortIDs := map[string]int{}
diff --git a/kernel/model/flashcard.go b/kernel/model/flashcard.go
index 600b11d776..8717d6cd87 100644
--- a/kernel/model/flashcard.go
+++ b/kernel/model/flashcard.go
@@ -148,6 +148,12 @@ func GetNotebookFlashcards(boxID string, page int) (blocks []*Block, total, page
func GetTreeFlashcards(rootID string, page int) (blocks []*Block, total, pageCount int) {
blocks = []*Block{}
+ cards := getTreeSubTreeFlashcards(rootID)
+ blocks, total, pageCount = getCardsBlocks(cards, page)
+ return
+}
+
+func getTreeSubTreeFlashcards(rootID string) (ret []riff.Card) {
deck := Decks[builtinDeckID]
if nil == deck {
return
@@ -162,9 +168,26 @@ func GetTreeFlashcards(rootID string, page int) (blocks []*Block, total, pageCou
}
}
allBlockIDs = gulu.Str.RemoveDuplicatedElem(allBlockIDs)
- cards := deck.GetCardsByBlockIDs(allBlockIDs)
+ ret = deck.GetCardsByBlockIDs(allBlockIDs)
+ return
+}
- blocks, total, pageCount = getCardsBlocks(cards, page)
+func getTreeFlashcards(rootID string) (ret []riff.Card) {
+ deck := Decks[builtinDeckID]
+ if nil == deck {
+ return
+ }
+
+ var allBlockIDs []string
+ deckBlockIDs := deck.GetBlockIDs()
+ treeBlockIDsMap, _ := getTreeBlocks(rootID)
+ for _, blockID := range deckBlockIDs {
+ if treeBlockIDsMap[blockID] {
+ allBlockIDs = append(allBlockIDs, blockID)
+ }
+ }
+ allBlockIDs = gulu.Str.RemoveDuplicatedElem(allBlockIDs)
+ ret = deck.GetCardsByBlockIDs(allBlockIDs)
return
}
@@ -420,6 +443,16 @@ func getTreeSubTreeChildBlocks(rootID string) (treeBlockIDsMap map[string]bool,
return
}
+func getTreeBlocks(rootID string) (treeBlockIDsMap map[string]bool, treeBlockIDs []string) {
+ treeBlockIDsMap = map[string]bool{}
+ bts := treenode.GetBlockTreesByRootID(rootID)
+ for _, bt := range bts {
+ treeBlockIDsMap[bt.ID] = true
+ treeBlockIDs = append(treeBlockIDs, bt.ID)
+ }
+ return
+}
+
func getBoxBlocks(boxID string) (blockIDsMap map[string]bool, blockIDs []string) {
blockIDsMap = map[string]bool{}
bts := treenode.GetBlockTreesByBoxID(boxID)
diff --git a/kernel/treenode/blocktree.go b/kernel/treenode/blocktree.go
index 609483ee66..12787a5818 100644
--- a/kernel/treenode/blocktree.go
+++ b/kernel/treenode/blocktree.go
@@ -226,6 +226,21 @@ func GetBlockTreesByPathPrefix(pathPrefix string) (ret []*BlockTree) {
return
}
+func GetBlockTreesByRootID(rootID string) (ret []*BlockTree) {
+ blockTrees.Range(func(key, value interface{}) bool {
+ slice := value.(*btSlice)
+ slice.m.Lock()
+ for _, b := range slice.data {
+ if b.RootID == rootID {
+ ret = append(ret, b)
+ }
+ }
+ slice.m.Unlock()
+ return true
+ })
+ return
+}
+
func RemoveBlockTreesByPathPrefix(pathPrefix string) {
var ids []string
blockTrees.Range(func(key, value interface{}) bool {