mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 23:20:13 +01:00
🎨 Query embed block supports executing JavaScript https://github.com/siyuan-note/siyuan/issues/9648
This commit is contained in:
parent
7a0aab7eac
commit
e24a724630
5 changed files with 59 additions and 3 deletions
|
|
@ -145,6 +145,7 @@ func ServeAPI(ginServer *gin.Engine) {
|
||||||
ginServer.Handle("POST", "/api/search/searchWidget", model.CheckAuth, searchWidget)
|
ginServer.Handle("POST", "/api/search/searchWidget", model.CheckAuth, searchWidget)
|
||||||
ginServer.Handle("POST", "/api/search/searchRefBlock", model.CheckAuth, searchRefBlock)
|
ginServer.Handle("POST", "/api/search/searchRefBlock", model.CheckAuth, searchRefBlock)
|
||||||
ginServer.Handle("POST", "/api/search/searchEmbedBlock", model.CheckAuth, searchEmbedBlock)
|
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/fullTextSearchBlock", model.CheckAuth, fullTextSearchBlock)
|
||||||
ginServer.Handle("POST", "/api/search/searchAsset", model.CheckAuth, searchAsset)
|
ginServer.Handle("POST", "/api/search/searchAsset", model.CheckAuth, searchAsset)
|
||||||
ginServer.Handle("POST", "/api/search/findReplace", model.CheckAuth, model.CheckReadonly, findReplace)
|
ginServer.Handle("POST", "/api/search/findReplace", model.CheckAuth, model.CheckReadonly, findReplace)
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
func searchEmbedBlock(c *gin.Context) {
|
||||||
ret := gulu.Ret.NewResult()
|
ret := gulu.Ret.NewResult()
|
||||||
defer c.JSON(http.StatusOK, ret)
|
defer c.JSON(http.StatusOK, ret)
|
||||||
|
|
|
||||||
|
|
@ -580,7 +580,7 @@ func getBlock(id string, tree *parse.Tree) (ret *Block, err error) {
|
||||||
return
|
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]
|
tree, _ := trees[sqlBlock.RootID]
|
||||||
if nil == tree {
|
if nil == tree {
|
||||||
tree, _ = loadTreeByBlockID(sqlBlock.RootID)
|
tree, _ = loadTreeByBlockID(sqlBlock.RootID)
|
||||||
|
|
|
||||||
|
|
@ -215,7 +215,13 @@ func IndexEmbedBlockJob() {
|
||||||
|
|
||||||
func autoIndexEmbedBlock(embedBlocks []*sql.Block) {
|
func autoIndexEmbedBlock(embedBlocks []*sql.Block) {
|
||||||
for i, embedBlock := range embedBlocks {
|
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, "}}")
|
stmt = strings.TrimSuffix(stmt, "}}")
|
||||||
if !strings.Contains(strings.ToLower(stmt), "select") {
|
if !strings.Contains(strings.ToLower(stmt), "select") {
|
||||||
continue
|
continue
|
||||||
|
|
|
||||||
|
|
@ -54,12 +54,28 @@ type EmbedBlock struct {
|
||||||
BlockPaths []*BlockPath `json:"blockPaths"`
|
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) {
|
func SearchEmbedBlock(embedBlockID, stmt string, excludeIDs []string, headingMode int, breadcrumb bool) (ret []*EmbedBlock) {
|
||||||
return searchEmbedBlock(embedBlockID, stmt, excludeIDs, headingMode, breadcrumb)
|
return searchEmbedBlock(embedBlockID, stmt, excludeIDs, headingMode, breadcrumb)
|
||||||
}
|
}
|
||||||
|
|
||||||
func searchEmbedBlock(embedBlockID, stmt string, excludeIDs []string, headingMode int, breadcrumb bool) (ret []*EmbedBlock) {
|
func searchEmbedBlock(embedBlockID, stmt string, excludeIDs []string, headingMode int, breadcrumb bool) (ret []*EmbedBlock) {
|
||||||
sqlBlocks := sql.SelectBlocksRawStmtNoParse(stmt, Conf.Search.Limit)
|
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.Block
|
var tmp []*sql.Block
|
||||||
for _, b := range sqlBlocks {
|
for _, b := range sqlBlocks {
|
||||||
if "query_embed" == b.Type { // 嵌入块不再嵌入
|
if "query_embed" == b.Type { // 嵌入块不再嵌入
|
||||||
|
|
@ -91,7 +107,7 @@ func searchEmbedBlock(embedBlockID, stmt string, excludeIDs []string, headingMod
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, sb := range sqlBlocks {
|
for _, sb := range sqlBlocks {
|
||||||
block, blockPaths := getEmbeddedBlock(embedBlockID, trees, sb, headingMode, breadcrumb)
|
block, blockPaths := getEmbeddedBlock(trees, sb, headingMode, breadcrumb)
|
||||||
if nil == block {
|
if nil == block {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue