From 5cf09d8043f526ba3ae97d59aebbcece8aff2b87 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Tue, 23 Dec 2025 17:10:41 +0800 Subject: [PATCH] :art: `Move` supports searching by document block ID https://github.com/siyuan-note/siyuan/issues/16674 Signed-off-by: Daniel <845765@qq.com> --- kernel/api/filetree.go | 2 +- kernel/model/file.go | 74 +++++++++++++++++++++++------------------- 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/kernel/api/filetree.go b/kernel/api/filetree.go index 39fdff05c..3339dd6cc 100644 --- a/kernel/api/filetree.go +++ b/kernel/api/filetree.go @@ -1043,7 +1043,7 @@ func searchDocs(c *gin.Context) { } k := arg["k"].(string) - ret.Data = model.SearchDocsByKeyword(k, flashcard, excludeIDs) + ret.Data = model.SearchDocs(k, flashcard, excludeIDs) } func listDocsByPath(c *gin.Context) { diff --git a/kernel/model/file.go b/kernel/model/file.go index fba93a55d..339bd4c4e 100644 --- a/kernel/model/file.go +++ b/kernel/model/file.go @@ -139,7 +139,7 @@ func (box *Box) moveCorruptedData(filePath string) { logging.LogWarnf("moved corrupted data file [%s] to [%s]", filePath, to) } -func SearchDocsByKeyword(keyword string, flashcard bool, excludeIDs []string) (ret []map[string]string) { +func SearchDocs(keyword string, flashcard bool, excludeIDs []string) (ret []map[string]string) { ret = []map[string]string{} var deck *riff.Deck @@ -159,11 +159,46 @@ func SearchDocsByKeyword(keyword string, flashcard bool, excludeIDs []string) (r boxes[box.ID] = box } - keywords := strings.Fields(keyword) + keyword = strings.TrimSpace(keyword) + var rootBlocks []*sql.Block - if 0 < len(keywords) { - for _, box := range boxes { - if gulu.Str.Contains(box.Name, keywords) { + if ast.IsNodeIDPattern(keyword) { + rootBlocks = sql.QueryRootBlockByCondition("id='"+keyword+"'", 1) + } else { + keywords := strings.Fields(keyword) + if 0 < len(keywords) { + for _, box := range boxes { + if gulu.Str.Contains(box.Name, keywords) { + if flashcard { + newFlashcardCount, dueFlashcardCount, flashcardCount := countBoxFlashcard(box.ID, deck, deckBlockIDs) + if 0 < flashcardCount { + ret = append(ret, map[string]string{"path": "/", "hPath": box.Name + "/", "box": box.ID, "boxIcon": box.Icon, "newFlashcardCount": strconv.Itoa(newFlashcardCount), "dueFlashcardCount": strconv.Itoa(dueFlashcardCount), "flashcardCount": strconv.Itoa(flashcardCount)}) + } + } else { + ret = append(ret, map[string]string{"path": "/", "hPath": box.Name + "/", "box": box.ID, "boxIcon": box.Icon}) + } + } + } + + var condition string + for i, k := range keywords { + condition += "(hpath LIKE '%" + k + "%'" + namCondition := Conf.Search.NAMFilter(k) + condition += " " + namCondition + condition += ")" + + if i < len(keywords)-1 { + condition += " AND " + } + } + + for _, excludeID := range excludeIDs { + condition += fmt.Sprintf(" AND path NOT LIKE '%%%s%%' ", excludeID) + } + + rootBlocks = sql.QueryRootBlockByCondition(condition, Conf.Search.Limit) + } else { + for _, box := range boxes { if flashcard { newFlashcardCount, dueFlashcardCount, flashcardCount := countBoxFlashcard(box.ID, deck, deckBlockIDs) if 0 < flashcardCount { @@ -174,35 +209,6 @@ func SearchDocsByKeyword(keyword string, flashcard bool, excludeIDs []string) (r } } } - - var condition string - for i, k := range keywords { - condition += "(hpath LIKE '%" + k + "%'" - namCondition := Conf.Search.NAMFilter(k) - condition += " " + namCondition - condition += ")" - - if i < len(keywords)-1 { - condition += " AND " - } - } - - for _, excludeID := range excludeIDs { - condition += fmt.Sprintf(" AND path NOT LIKE '%%%s%%' ", excludeID) - } - - rootBlocks = sql.QueryRootBlockByCondition(condition, Conf.Search.Limit) - } else { - for _, box := range boxes { - if flashcard { - newFlashcardCount, dueFlashcardCount, flashcardCount := countBoxFlashcard(box.ID, deck, deckBlockIDs) - if 0 < flashcardCount { - ret = append(ret, map[string]string{"path": "/", "hPath": box.Name + "/", "box": box.ID, "boxIcon": box.Icon, "newFlashcardCount": strconv.Itoa(newFlashcardCount), "dueFlashcardCount": strconv.Itoa(dueFlashcardCount), "flashcardCount": strconv.Itoa(flashcardCount)}) - } - } else { - ret = append(ret, map[string]string{"path": "/", "hPath": box.Name + "/", "box": box.ID, "boxIcon": box.Icon}) - } - } } for _, rootBlock := range rootBlocks {