From 65fbfc5a1f11997fd6baa91bdc9dbd778d6d923d Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sun, 17 Dec 2023 22:15:02 +0800 Subject: [PATCH 1/3] :art: Find replace supports selecting element types https://github.com/siyuan-note/siyuan/issues/9895 --- kernel/api/search.go | 4 ++-- kernel/model/search.go | 28 ++++++++++++++-------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/kernel/api/search.go b/kernel/api/search.go index 69613e520..d4d42b520 100644 --- a/kernel/api/search.go +++ b/kernel/api/search.go @@ -83,8 +83,8 @@ func findReplace(c *gin.Context) { } replaceTypes := map[string]bool{} - // text, img-text, img-title, img-src, a-text, a-title, a-href, code, em, strong, inline-math, inline-memo, kbd, mark, s, sub, sup, tag, u - // doc-title, code-block, math-block, html-block + // text, imgText, imgTitle, imgSrc, aText, aTitle, aHref, code, em, strong, inlineMath, inlineMemo, kbd, mark, s, sub, sup, tag, u + // docTitle, codeBlock, mathBlock, htmlBlock if nil != arg["replaceTypes"] { replaceTypesArg := arg["replaceTypes"].(map[string]interface{}) for t, b := range replaceTypesArg { diff --git a/kernel/model/search.go b/kernel/model/search.go index 18f1e5545..b561217f1 100644 --- a/kernel/model/search.go +++ b/kernel/model/search.go @@ -353,7 +353,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids } if ast.NodeDocument == node.Type { - if !replaceTypes["doc-title"] { + if !replaceTypes["docTitle"] { continue } @@ -383,37 +383,37 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids replaceNodeTokens(n, method, keyword, replacement, r) case ast.NodeLinkDest: - if !replaceTypes["img-src"] { + if !replaceTypes["imgSrc"] { return ast.WalkContinue } replaceNodeTokens(n, method, keyword, replacement, r) case ast.NodeLinkText: - if !replaceTypes["img-text"] { + if !replaceTypes["imgText"] { return ast.WalkContinue } replaceNodeTokens(n, method, keyword, replacement, r) case ast.NodeLinkTitle: - if !replaceTypes["img-title"] { + if !replaceTypes["imgTitle"] { return ast.WalkContinue } replaceNodeTokens(n, method, keyword, replacement, r) case ast.NodeCodeBlockCode: - if !replaceTypes["code-block"] { + if !replaceTypes["codeBlock"] { return ast.WalkContinue } replaceNodeTokens(n, method, keyword, replacement, r) case ast.NodeMathBlockContent: - if !replaceTypes["math-block"] { + if !replaceTypes["mathBlock"] { return ast.WalkContinue } replaceNodeTokens(n, method, keyword, replacement, r) case ast.NodeHTMLBlock: - if !replaceTypes["html-block"] { + if !replaceTypes["htmlBlock"] { return ast.WalkContinue } @@ -434,7 +434,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids } } } else if n.IsTextMarkType("a") { - if replaceTypes["a-text"] { + if replaceTypes["aText"] { if 0 == method { if bytes.Contains(n.Tokens, []byte(keyword)) { n.TextMarkTextContent = strings.ReplaceAll(n.TextMarkTextContent, keyword, replacement) @@ -446,7 +446,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids } } - if replaceTypes["a-title"] { + if replaceTypes["aTitle"] { if 0 == method { if strings.Contains(n.TextMarkATitle, keyword) { n.TextMarkATitle = strings.ReplaceAll(n.TextMarkATitle, keyword, replacement) @@ -458,7 +458,7 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids } } - if replaceTypes["a-href"] { + if replaceTypes["aHref"] { if 0 == method { if strings.Contains(n.TextMarkAHref, keyword) { n.TextMarkAHref = strings.ReplaceAll(n.TextMarkAHref, keyword, replacement) @@ -524,8 +524,8 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids } replaceNodeTextMarkTextContent(n, method, keyword, replacement, r) - } else if n.IsTextMarkType("inline-math") { - if !replaceTypes["inline-math"] { + } else if n.IsTextMarkType("inlineMath") { + if !replaceTypes["inlineMath"] { return ast.WalkContinue } @@ -538,8 +538,8 @@ func FindReplace(keyword, replacement string, replaceTypes map[string]bool, ids n.TextMarkInlineMathContent = r.ReplaceAllString(n.TextMarkInlineMathContent, replacement) } } - } else if n.IsTextMarkType("inline-memo") { - if !replaceTypes["inline-memo"] { + } else if n.IsTextMarkType("inlineMemo") { + if !replaceTypes["inlineMemo"] { return ast.WalkContinue } From 32ce33d0fb5bd3a03c6ab6d3809f40dc13f4480b Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sun, 17 Dec 2023 22:34:35 +0800 Subject: [PATCH 2/3] :art: Add internal kernel API `/api/setting/addVirtualBlockRefInclude` and `addVirtualBlockRefExclude` https://github.com/siyuan-note/siyuan/issues/9909 --- kernel/api/router.go | 2 ++ kernel/api/setting.go | 40 ++++++++++++++++++++++++++++++++++++++ kernel/model/virutalref.go | 36 ++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/kernel/api/router.go b/kernel/api/router.go index 571f78f01..4d05190c8 100644 --- a/kernel/api/router.go +++ b/kernel/api/router.go @@ -308,6 +308,8 @@ func ServeAPI(ginServer *gin.Engine) { ginServer.Handle("POST", "/api/setting/setAI", model.CheckAuth, model.CheckReadonly, setAI) ginServer.Handle("POST", "/api/setting/setBazaar", model.CheckAuth, model.CheckReadonly, setBazaar) ginServer.Handle("POST", "/api/setting/refreshVirtualBlockRef", model.CheckAuth, model.CheckReadonly, refreshVirtualBlockRef) + ginServer.Handle("POST", "/api/setting/addVirtualBlockRefInclude", model.CheckAuth, model.CheckReadonly, addVirtualBlockRefInclude) + ginServer.Handle("POST", "/api/setting/addVirtualBlockRefExclude", model.CheckAuth, model.CheckReadonly, addVirtualBlockRefExclude) ginServer.Handle("POST", "/api/graph/resetGraph", model.CheckAuth, model.CheckReadonly, resetGraph) ginServer.Handle("POST", "/api/graph/resetLocalGraph", model.CheckAuth, model.CheckReadonly, resetLocalGraph) diff --git a/kernel/api/setting.go b/kernel/api/setting.go index c422fb400..b6bd898c3 100644 --- a/kernel/api/setting.go +++ b/kernel/api/setting.go @@ -29,6 +29,46 @@ import ( "github.com/siyuan-note/siyuan/kernel/util" ) +func addVirtualBlockRefExclude(c *gin.Context) { + // Add internal kernel API `/api/setting/addVirtualBlockRefExclude` https://github.com/siyuan-note/siyuan/issues/9909 + + ret := gulu.Ret.NewResult() + defer c.JSON(http.StatusOK, ret) + + arg, ok := util.JsonArg(c, ret) + if !ok { + return + } + + keywordsArg := arg["keywords"] + var keywords []string + for _, k := range keywordsArg.([]interface{}) { + keywords = append(keywords, k.(string)) + } + + model.AddVirtualBlockRefExclude(keywords) +} + +func addVirtualBlockRefInclude(c *gin.Context) { + // Add internal kernel API `/api/setting/addVirtualBlockRefInclude` https://github.com/siyuan-note/siyuan/issues/9909 + + ret := gulu.Ret.NewResult() + defer c.JSON(http.StatusOK, ret) + + arg, ok := util.JsonArg(c, ret) + if !ok { + return + } + + keywordsArg := arg["keywords"] + var keywords []string + for _, k := range keywordsArg.([]interface{}) { + keywords = append(keywords, k.(string)) + } + + model.AddVirtualBlockRefInclude(keywords) +} + func refreshVirtualBlockRef(c *gin.Context) { // Add internal kernel API `/api/setting/refreshVirtualBlockRef` https://github.com/siyuan-note/siyuan/issues/9829 diff --git a/kernel/model/virutalref.go b/kernel/model/virutalref.go index 7b2718e8c..a1741e0c4 100644 --- a/kernel/model/virutalref.go +++ b/kernel/model/virutalref.go @@ -113,6 +113,42 @@ func ResetVirtualBlockRefCache() { CacheVirtualBlockRefJob() } +func AddVirtualBlockRefInclude(keyword []string) { + if 1 > len(keyword) { + return + } + + Conf.m.Lock() + defer Conf.m.Unlock() + + include := strings.ReplaceAll(Conf.Editor.VirtualBlockRefInclude, "\\,", "__comma@sep__") + includes := strings.Split(include, ",") + includes = append(includes, keyword...) + includes = gulu.Str.RemoveDuplicatedElem(includes) + Conf.Editor.VirtualBlockRefInclude = strings.Join(includes, ",") + Conf.Save() + + ResetVirtualBlockRefCache() +} + +func AddVirtualBlockRefExclude(keyword []string) { + if 1 > len(keyword) { + return + } + + Conf.m.Lock() + defer Conf.m.Unlock() + + exclude := strings.ReplaceAll(Conf.Editor.VirtualBlockRefExclude, "\\,", "__comma@sep__") + excludes := strings.Split(exclude, ",") + excludes = append(excludes, keyword...) + excludes = gulu.Str.RemoveDuplicatedElem(excludes) + Conf.Editor.VirtualBlockRefExclude = strings.Join(excludes, ",") + Conf.Save() + + ResetVirtualBlockRefCache() +} + func processVirtualRef(n *ast.Node, unlinks *[]*ast.Node, virtualBlockRefKeywords []string, refCount map[string]int, luteEngine *lute.Lute) bool { if !Conf.Editor.VirtualBlockRef { return false From 9052e4ff92f5d853e395bb0738b17fa1678a4284 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sun, 17 Dec 2023 22:45:09 +0800 Subject: [PATCH 3/3] :art: Add internal kernel API `/api/setting/addVirtualBlockRefInclude` and `addVirtualBlockRefExclude` https://github.com/siyuan-note/siyuan/issues/9909 --- kernel/api/setting.go | 3 +++ kernel/model/virutalref.go | 6 ------ 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/kernel/api/setting.go b/kernel/api/setting.go index b6bd898c3..9229dc6a7 100644 --- a/kernel/api/setting.go +++ b/kernel/api/setting.go @@ -47,6 +47,7 @@ func addVirtualBlockRefExclude(c *gin.Context) { } model.AddVirtualBlockRefExclude(keywords) + util.BroadcastByType("main", "setConf", 0, "", model.Conf) } func addVirtualBlockRefInclude(c *gin.Context) { @@ -67,6 +68,7 @@ func addVirtualBlockRefInclude(c *gin.Context) { } model.AddVirtualBlockRefInclude(keywords) + util.BroadcastByType("main", "setConf", 0, "", model.Conf) } func refreshVirtualBlockRef(c *gin.Context) { @@ -76,6 +78,7 @@ func refreshVirtualBlockRef(c *gin.Context) { defer c.JSON(http.StatusOK, ret) model.ResetVirtualBlockRefCache() + util.BroadcastByType("main", "setConf", 0, "", model.Conf) } func setBazaar(c *gin.Context) { diff --git a/kernel/model/virutalref.go b/kernel/model/virutalref.go index a1741e0c4..34c9c23c5 100644 --- a/kernel/model/virutalref.go +++ b/kernel/model/virutalref.go @@ -118,9 +118,6 @@ func AddVirtualBlockRefInclude(keyword []string) { return } - Conf.m.Lock() - defer Conf.m.Unlock() - include := strings.ReplaceAll(Conf.Editor.VirtualBlockRefInclude, "\\,", "__comma@sep__") includes := strings.Split(include, ",") includes = append(includes, keyword...) @@ -136,9 +133,6 @@ func AddVirtualBlockRefExclude(keyword []string) { return } - Conf.m.Lock() - defer Conf.m.Unlock() - exclude := strings.ReplaceAll(Conf.Editor.VirtualBlockRefExclude, "\\,", "__comma@sep__") excludes := strings.Split(exclude, ",") excludes = append(excludes, keyword...)