snippet.go 2.7 KB

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