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 model
18
19import (
20 "os"
21 "path/filepath"
22 "sync"
23
24 "github.com/88250/gulu"
25 "github.com/88250/lute/ast"
26 "github.com/siyuan-note/filelock"
27 "github.com/siyuan-note/logging"
28 "github.com/siyuan-note/siyuan/kernel/conf"
29 "github.com/siyuan-note/siyuan/kernel/util"
30)
31
32var snippetsLock = sync.Mutex{}
33
34func RemoveSnippet(id string) (ret *conf.Snippet, err error) {
35 snippetsLock.Lock()
36 defer snippetsLock.Unlock()
37
38 snippets, err := loadSnippets()
39 if err != nil {
40 return
41 }
42
43 for i, s := range snippets {
44 if s.ID == id {
45 ret = s
46 snippets = append(snippets[:i], snippets[i+1:]...)
47 break
48 }
49 }
50 err = writeSnippetsConf(snippets)
51 return
52}
53
54func SetSnippet(snippets []*conf.Snippet) (err error) {
55 snippetsLock.Lock()
56 defer snippetsLock.Unlock()
57
58 err = writeSnippetsConf(snippets)
59 return
60}
61
62func LoadSnippets() (ret []*conf.Snippet, err error) {
63 snippetsLock.Lock()
64 defer snippetsLock.Unlock()
65 return loadSnippets()
66}
67
68func loadSnippets() (ret []*conf.Snippet, err error) {
69 ret = []*conf.Snippet{}
70 confPath := filepath.Join(util.SnippetsPath, "conf.json")
71 if !filelock.IsExist(confPath) {
72 return
73 }
74
75 data, err := filelock.ReadFile(confPath)
76 if err != nil {
77 logging.LogErrorf("load js snippets failed: %s", err)
78 return
79 }
80
81 if err = gulu.JSON.UnmarshalJSON(data, &ret); err != nil {
82 logging.LogErrorf("unmarshal js snippets failed: %s", err)
83 return
84 }
85
86 needRewrite := false
87 for _, snippet := range ret {
88 if "" == snippet.ID {
89 snippet.ID = ast.NewNodeID()
90 needRewrite = true
91 }
92 }
93 if needRewrite {
94 writeSnippetsConf(ret)
95 }
96 return
97}
98
99func writeSnippetsConf(snippets []*conf.Snippet) (err error) {
100 data, err := gulu.JSON.MarshalIndentJSON(snippets, "", " ")
101 if err != nil {
102 logging.LogErrorf("marshal snippets failed: %s", err)
103 return
104 }
105
106 if err = os.MkdirAll(util.SnippetsPath, 0755); err != nil {
107 return
108 }
109
110 confPath := filepath.Join(util.SnippetsPath, "conf.json")
111 oldData, _ := filelock.ReadFile(confPath)
112 if string(oldData) == string(data) {
113 return
114 }
115 err = filelock.WriteFile(confPath, data)
116 return
117}