Skip to content

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

:art: 支持通过界面设置代码片段 https://github.com/siyuan-note/siyuan/issues/6357

Liang Dingdd30521f722b
diff --git a/kernel/api/snippet.go b/kernel/api/snippet.go
index 6a67b3ddb3..5f186f1b2b 100644
--- a/kernel/api/snippet.go
+++ b/kernel/api/snippet.go
@@ -23,6 +23,7 @@ import (
"strings"
"github.com/88250/gulu"
+ "github.com/88250/lute/ast"
"github.com/gin-gonic/gin"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/conf"
@@ -98,22 +99,29 @@ func setSnippet(c *gin.Context) {
return
}
- id := gulu.Rand.String(12)
- idArg := arg["id"]
- if nil != idArg {
- id = idArg.(string)
+ snippetsArg := arg["snippets"].([]interface{})
+ var snippets []*conf.Snippet
+ for _, s := range snippetsArg {
+ m := s.(map[string]interface{})
+ snippet := &conf.Snippet{
+ ID: m["id"].(string),
+ Name: m["name"].(string),
+ Type: m["type"].(string),
+ Content: m["content"].(string),
+ Enabled: m["enabled"].(bool),
+ }
+ if "" == snippet.ID {
+ snippet.ID = ast.NewNodeID()
+ }
+ snippets = append(snippets, snippet)
}
- name := arg["name"].(string)
- typ := arg["type"].(string)
- content := arg["content"].(string)
- enabled := arg["enabled"].(bool)
- snippet, err := model.SetSnippet(id, name, typ, content, enabled)
+
+ err := model.SetSnippet(snippets)
if nil != err {
ret.Code = -1
ret.Msg = "set snippet failed: " + err.Error()
return
}
- ret.Data = snippet
}
func removeSnippet(c *gin.Context) {
diff --git a/kernel/model/snippet.go b/kernel/model/snippet.go
index e822cd7ccd..416d261a9f 100644
--- a/kernel/model/snippet.go
+++ b/kernel/model/snippet.go
@@ -50,32 +50,10 @@ func RemoveSnippet(id string) (ret *conf.Snippet, err error) {
return
}
-func SetSnippet(id, name, typ, content string, enabled bool) (ret *conf.Snippet, err error) {
+func SetSnippet(snippets []*conf.Snippet) (err error) {
snippetsLock.Lock()
defer snippetsLock.Unlock()
- snippets, err := loadSnippets()
- if nil != err {
- return
- }
-
- isUpdate := false
- for _, s := range snippets {
- if s.ID == id {
- s.Name = name
- s.Type = typ
- s.Content = content
- s.Enabled = enabled
- ret = s
- isUpdate = true
- break
- }
- }
-
- if !isUpdate {
- ret = &conf.Snippet{ID: id, Name: name, Type: typ, Content: content, Enabled: enabled}
- snippets = append(snippets, ret)
- }
err = writeSnippetsConf(snippets)
return
}