snippet.go 3.0 KB

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