A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
1// SiYuan - Refactor your thinking
2// Copyright (c) 2020-present, b3log.org
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU Affero General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU Affero General Public License for more details.
13//
14// You should have received a copy of the GNU Affero General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17package api
18
19import (
20 "net/http"
21
22 "github.com/88250/gulu"
23 "github.com/gin-gonic/gin"
24 "github.com/siyuan-note/siyuan/kernel/model"
25 "github.com/siyuan-note/siyuan/kernel/util"
26)
27
28func renderSprig(c *gin.Context) {
29 ret := gulu.Ret.NewResult()
30 defer c.JSON(http.StatusOK, ret)
31
32 arg, ok := util.JsonArg(c, ret)
33 if !ok {
34 return
35 }
36
37 template := arg["template"].(string)
38 content, err := model.RenderGoTemplate(template)
39 if err != nil {
40 ret.Code = -1
41 ret.Msg = util.EscapeHTML(err.Error())
42 return
43 }
44 ret.Data = content
45}
46
47func docSaveAsTemplate(c *gin.Context) {
48 ret := gulu.Ret.NewResult()
49 defer c.JSON(http.StatusOK, ret)
50
51 arg, ok := util.JsonArg(c, ret)
52 if !ok {
53 return
54 }
55
56 id := arg["id"].(string)
57 name := arg["name"].(string)
58 overwrite := arg["overwrite"].(bool)
59 code, err := model.DocSaveAsTemplate(id, name, overwrite)
60 if err != nil {
61 ret.Code = -1
62 ret.Msg = util.EscapeHTML(err.Error())
63 return
64 }
65 ret.Code = code
66}
67
68func renderTemplate(c *gin.Context) {
69 ret := gulu.Ret.NewResult()
70 defer c.JSON(http.StatusOK, ret)
71
72 arg, ok := util.JsonArg(c, ret)
73 if !ok {
74 return
75 }
76
77 p := arg["path"].(string)
78 id := arg["id"].(string)
79 if util.InvalidIDPattern(id, ret) {
80 return
81 }
82
83 if !util.IsAbsPathInWorkspace(p) {
84 ret.Code = -1
85 ret.Msg = "Path [" + p + "] is not in workspace"
86 return
87 }
88
89 preview := false
90 if previewArg := arg["preview"]; nil != previewArg {
91 preview = previewArg.(bool)
92 }
93
94 _, content, err := model.RenderTemplate(p, id, preview)
95 if err != nil {
96 ret.Code = -1
97 ret.Msg = util.EscapeHTML(err.Error())
98 return
99 }
100
101 ret.Data = map[string]interface{}{
102 "path": p,
103 "content": content,
104 }
105}