An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Query embed block supports executing JavaScript https://github.com/siyuan-note/siyuan/issues/9648
Daniele24a7246305c
diff --git a/kernel/api/router.go b/kernel/api/router.goindex 58650b14a9..ee95f32e89 100644--- a/kernel/api/router.go+++ b/kernel/api/router.go@@ -145,6 +145,7 @@ func ServeAPI(ginServer *gin.Engine) {ginServer.Handle("POST", "/api/search/searchWidget", model.CheckAuth, searchWidget)ginServer.Handle("POST", "/api/search/searchRefBlock", model.CheckAuth, searchRefBlock)ginServer.Handle("POST", "/api/search/searchEmbedBlock", model.CheckAuth, searchEmbedBlock)+ ginServer.Handle("POST", "/api/search/getEmbedBlock", model.CheckAuth, getEmbedBlock)ginServer.Handle("POST", "/api/search/fullTextSearchBlock", model.CheckAuth, fullTextSearchBlock)ginServer.Handle("POST", "/api/search/searchAsset", model.CheckAuth, searchAsset)ginServer.Handle("POST", "/api/search/findReplace", model.CheckAuth, model.CheckReadonly, findReplace)diff --git a/kernel/api/search.go b/kernel/api/search.goindex 0c29d130ed..671f7646b8 100644--- a/kernel/api/search.go+++ b/kernel/api/search.go@@ -185,6 +185,39 @@ func searchTemplate(c *gin.Context) {}}+func getEmbedBlock(c *gin.Context) {+ // Query embed block supports executing JavaScript https://github.com/siyuan-note/siyuan/issues/9648+ ret := gulu.Ret.NewResult()+ defer c.JSON(http.StatusOK, ret)++ arg, ok := util.JsonArg(c, ret)+ if !ok {+ return+ }++ embedBlockID := arg["embedBlockID"].(string)+ includeIDsArg := arg["includeIDs"].([]interface{})+ var includeIDs []string+ for _, includeID := range includeIDsArg {+ includeIDs = append(includeIDs, includeID.(string))+ }+ headingMode := 0 // 0:带标题下方块+ headingModeArg := arg["headingMode"]+ if nil != headingModeArg {+ headingMode = int(headingModeArg.(float64))+ }+ breadcrumb := false+ breadcrumbArg := arg["breadcrumb"]+ if nil != breadcrumbArg {+ breadcrumb = breadcrumbArg.(bool)+ }++ blocks := model.GetEmbedBlock(embedBlockID, includeIDs, headingMode, breadcrumb)+ ret.Data = map[string]interface{}{+ "blocks": blocks,+ }+}+func searchEmbedBlock(c *gin.Context) {ret := gulu.Ret.NewResult()defer c.JSON(http.StatusOK, ret)diff --git a/kernel/model/block.go b/kernel/model/block.goindex 863b8edeb0..73eda0c126 100644--- a/kernel/model/block.go+++ b/kernel/model/block.go@@ -580,7 +580,7 @@ func getBlock(id string, tree *parse.Tree) (ret *Block, err error) {return}-func getEmbeddedBlock(embedBlockID string, trees map[string]*parse.Tree, sqlBlock *sql.Block, headingMode int, breadcrumb bool) (block *Block, blockPaths []*BlockPath) {+func getEmbeddedBlock(trees map[string]*parse.Tree, sqlBlock *sql.Block, headingMode int, breadcrumb bool) (block *Block, blockPaths []*BlockPath) {tree, _ := trees[sqlBlock.RootID]if nil == tree {tree, _ = loadTreeByBlockID(sqlBlock.RootID)diff --git a/kernel/model/index.go b/kernel/model/index.goindex 19e6d9b8f8..ca0780a743 100644--- a/kernel/model/index.go+++ b/kernel/model/index.go@@ -215,7 +215,13 @@ func IndexEmbedBlockJob() {func autoIndexEmbedBlock(embedBlocks []*sql.Block) {for i, embedBlock := range embedBlocks {- stmt := strings.TrimPrefix(embedBlock.Markdown, "{{")+ md := strings.TrimSpace(embedBlock.Markdown)+ if strings.Contains(md, "//js") {+ // js 嵌入块不支持自动索引+ continue+ }++ stmt := strings.TrimPrefix(md, "{{")stmt = strings.TrimSuffix(stmt, "}}")if !strings.Contains(strings.ToLower(stmt), "select") {continuediff --git a/kernel/model/search.go b/kernel/model/search.goindex 53a07f2a94..caa8cc8952 100644--- a/kernel/model/search.go+++ b/kernel/model/search.go@@ -54,12 +54,28 @@ type EmbedBlock struct {BlockPaths []*BlockPath `json:"blockPaths"`}+func GetEmbedBlock(embedBlockID string, includeIDs []string, headingMode int, breadcrumb bool) (ret []*EmbedBlock) {+ return getEmbedBlock(embedBlockID, includeIDs, headingMode, breadcrumb)+}++func getEmbedBlock(embedBlockID string, includeIDs []string, headingMode int, breadcrumb bool) (ret []*EmbedBlock) {+ stmt := "SELECT * FROM `blocks` WHERE `id` IN ('" + strings.Join(includeIDs, "','") + "')"+ sqlBlocks := sql.SelectBlocksRawStmtNoParse(stmt, 1024)+ ret = buildEmbedBlock(embedBlockID, []string{}, headingMode, breadcrumb, sqlBlocks)+ return+}+func SearchEmbedBlock(embedBlockID, stmt string, excludeIDs []string, headingMode int, breadcrumb bool) (ret []*EmbedBlock) {return searchEmbedBlock(embedBlockID, stmt, excludeIDs, headingMode, breadcrumb)}func searchEmbedBlock(embedBlockID, stmt string, excludeIDs []string, headingMode int, breadcrumb bool) (ret []*EmbedBlock) {sqlBlocks := sql.SelectBlocksRawStmtNoParse(stmt, Conf.Search.Limit)+ ret = buildEmbedBlock(embedBlockID, excludeIDs, headingMode, breadcrumb, sqlBlocks)+ return+}++func buildEmbedBlock(embedBlockID string, excludeIDs []string, headingMode int, breadcrumb bool, sqlBlocks []*sql.Block) (ret []*EmbedBlock) {var tmp []*sql.Blockfor _, b := range sqlBlocks {if "query_embed" == b.Type { // 嵌入块不再嵌入@@ -91,7 +107,7 @@ func searchEmbedBlock(embedBlockID, stmt string, excludeIDs []string, headingMod}for _, sb := range sqlBlocks {- block, blockPaths := getEmbeddedBlock(embedBlockID, trees, sb, headingMode, breadcrumb)+ block, blockPaths := getEmbeddedBlock(trees, sb, headingMode, breadcrumb)if nil == block {continue}