Skip to content

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

:art: 支持伺服代码片段 `/snippets/` Fix https://github.com/siyuan-note/siyuan/issues/6356

Liang Ding2063203f23a9
diff --git a/kernel/api/router.go b/kernel/api/router.go
index 4b5ec5f658..6507ebd592 100644
--- a/kernel/api/router.go
+++ b/kernel/api/router.go
@@ -283,4 +283,5 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/notification/pushErrMsg", model.CheckAuth, pushErrMsg)
ginServer.Handle("POST", "/api/snippet/getSnippet", model.CheckAuth, getSnippet)
+ ginServer.Handle("GET", "/snippets/*filepath", serveSnippets)
}
diff --git a/kernel/api/snippet.go b/kernel/api/snippet.go
index 56dbc685d6..1b48539443 100644
--- a/kernel/api/snippet.go
+++ b/kernel/api/snippet.go
@@ -17,15 +17,40 @@
package api
import (
+ "mime"
"net/http"
+ "path/filepath"
+ "strings"
"github.com/88250/gulu"
"github.com/gin-gonic/gin"
+ "github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/conf"
"github.com/siyuan-note/siyuan/kernel/model"
"github.com/siyuan-note/siyuan/kernel/util"
)
+func serveSnippets(c *gin.Context) {
+ name := strings.TrimPrefix(c.Request.URL.Path, "/snippets/")
+ ext := filepath.Ext(name)
+ name = strings.TrimSuffix(name, ext)
+ confSnippets, err := model.LoadSnippets()
+ if nil != err {
+ logging.LogErrorf("load snippets failed: %s", name, err)
+ c.Status(404)
+ return
+ }
+
+ for _, s := range confSnippets {
+ if s.Name == name && ("" != ext && s.Type == ext[1:]) {
+ c.Header("Content-Type", mime.TypeByExtension(ext))
+ c.String(http.StatusOK, s.Content)
+ return
+ }
+ }
+ c.Status(404)
+}
+
func getSnippet(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)