Skip to content

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

:art: Content type filtering when inserting reference links via `((`, `[[` https://github.com/siyuan-note/siyuan/issues/8009

Liang Dingc31695159285
diff --git a/kernel/api/search.go b/kernel/api/search.go
index 31a5877c7c..1620b0392e 100644
--- a/kernel/api/search.go
+++ b/kernel/api/search.go
@@ -191,11 +191,16 @@ func searchRefBlock(c *gin.Context) {
return
}
+ onlyDoc := false
+ if nil != arg["onlyDoc"] {
+ onlyDoc = arg["onlyDoc"].(bool)
+ }
+
rootID := arg["rootID"].(string)
id := arg["id"].(string)
keyword := arg["k"].(string)
beforeLen := int(arg["beforeLen"].(float64))
- blocks, newDoc := model.SearchRefBlock(id, rootID, keyword, beforeLen)
+ blocks, newDoc := model.SearchRefBlock(id, rootID, keyword, beforeLen, onlyDoc)
ret.Data = map[string]interface{}{
"blocks": blocks,
"newDoc": newDoc,
diff --git a/kernel/model/search.go b/kernel/model/search.go
index d397a8bdf7..493d529a40 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -120,12 +120,12 @@ func searchEmbedBlock(embedBlockID, stmt string, excludeIDs []string, headingMod
return
}
-func SearchRefBlock(id, rootID, keyword string, beforeLen int) (ret []*Block, newDoc bool) {
+func SearchRefBlock(id, rootID, keyword string, beforeLen int, onlyDoc bool) (ret []*Block, newDoc bool) {
cachedTrees := map[string]*parse.Tree{}
if "" == keyword {
// 查询为空时默认的块引排序规则按最近使用优先 https://github.com/siyuan-note/siyuan/issues/3218
- refs := sql.QueryRefsRecent()
+ refs := sql.QueryRefsRecent(onlyDoc)
for _, ref := range refs {
tree := cachedTrees[ref.DefBlockRootID]
if nil == tree {
@@ -157,7 +157,7 @@ func SearchRefBlock(id, rootID, keyword string, beforeLen int) (ret []*Block, ne
return
}
- ret = fullTextSearchRefBlock(keyword, beforeLen)
+ ret = fullTextSearchRefBlock(keyword, beforeLen, onlyDoc)
tmp := ret[:0]
for _, b := range ret {
tree := cachedTrees[b.RootID]
@@ -624,7 +624,7 @@ func searchBySQL(stmt string, beforeLen int) (ret []*Block, matchedBlockCount, m
return
}
-func fullTextSearchRefBlock(keyword string, beforeLen int) (ret []*Block) {
+func fullTextSearchRefBlock(keyword string, beforeLen int, onlyDoc bool) (ret []*Block) {
keyword = gulu.Str.RemoveInvisible(keyword)
if ast.IsNodeIDPattern(keyword) {
@@ -646,7 +646,12 @@ func fullTextSearchRefBlock(keyword string, beforeLen int) (ret []*Block) {
"tag, " +
"snippet(" + table + ", 11, '" + search.SearchMarkLeft + "', '" + search.SearchMarkRight + "', '...', 64) AS content, " +
"fcontent, markdown, length, type, subtype, ial, sort, created, updated"
- stmt := "SELECT " + projections + " FROM " + table + " WHERE " + table + " MATCH '" + columnFilter() + ":(" + quotedKeyword + ")' AND type IN " + Conf.Search.TypeFilter()
+ stmt := "SELECT " + projections + " FROM " + table + " WHERE " + table + " MATCH '" + columnFilter() + ":(" + quotedKeyword + ")' AND type"
+ if onlyDoc {
+ stmt += " = 'd'"
+ } else {
+ stmt += " IN " + Conf.Search.TypeFilter()
+ }
orderBy := ` order by case
when name = '${keyword}' then 10
when alias = '${keyword}' then 20
diff --git a/kernel/sql/block_ref_query.go b/kernel/sql/block_ref_query.go
index 80cde1f183..348b39def2 100644
--- a/kernel/sql/block_ref_query.go
+++ b/kernel/sql/block_ref_query.go
@@ -335,8 +335,13 @@ func QueryRefIDsByDefID(defID string, containChildren bool) (refIDs, refTexts []
return
}
-func QueryRefsRecent() (ret []*Ref) {
- rows, err := query("SELECT * FROM refs GROUP BY def_block_id ORDER BY id desc LIMIT 32")
+func QueryRefsRecent(onlyDoc bool) (ret []*Ref) {
+ stmt := "SELECT * FROM refs"
+ if onlyDoc {
+ stmt = "SELECT * FROM refs WHERE def_block_type = 'd'"
+ }
+ stmt += " GROUP BY def_block_id ORDER BY id DESC LIMIT 32"
+ rows, err := query(stmt)
if nil != err {
logging.LogErrorf("sql query failed: %s", err)
return