snippet.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 api
  17. import (
  18. "net/http"
  19. "strings"
  20. "github.com/88250/gulu"
  21. "github.com/88250/lute/ast"
  22. "github.com/gin-gonic/gin"
  23. "github.com/siyuan-note/siyuan/kernel/conf"
  24. "github.com/siyuan-note/siyuan/kernel/model"
  25. "github.com/siyuan-note/siyuan/kernel/util"
  26. )
  27. func getSnippet(c *gin.Context) {
  28. ret := gulu.Ret.NewResult()
  29. defer c.JSON(http.StatusOK, ret)
  30. arg, ok := util.JsonArg(c, ret)
  31. if !ok {
  32. return
  33. }
  34. typ := arg["type"].(string) // js/css/all
  35. enabledArg := int(arg["enabled"].(float64)) // 0:禁用,1:启用,2:全部
  36. enabled := true
  37. if 0 == enabledArg {
  38. enabled = false
  39. }
  40. var keyword string
  41. if nil != arg["keyword"] {
  42. keyword = arg["keyword"].(string)
  43. }
  44. confSnippets, err := model.LoadSnippets()
  45. if nil != err {
  46. ret.Code = -1
  47. ret.Msg = "load snippets failed: " + err.Error()
  48. return
  49. }
  50. var snippets []*conf.Snippet
  51. for _, s := range confSnippets {
  52. if ("all" == typ || s.Type == typ) && (2 == enabledArg || s.Enabled == enabled) {
  53. snippets = append(snippets, s)
  54. }
  55. }
  56. if "" != keyword {
  57. var snippetsFiltered []*conf.Snippet
  58. for _, s := range snippets {
  59. if strings.Contains(strings.ToLower(s.Name), strings.ToLower(keyword)) || strings.Contains(strings.ToLower(s.Content), strings.ToLower(keyword)) {
  60. snippetsFiltered = append(snippetsFiltered, s)
  61. }
  62. }
  63. snippets = snippetsFiltered
  64. }
  65. if 1 > len(snippets) {
  66. snippets = []*conf.Snippet{}
  67. }
  68. ret.Data = map[string]interface{}{
  69. "snippets": snippets,
  70. }
  71. }
  72. func setSnippet(c *gin.Context) {
  73. ret := gulu.Ret.NewResult()
  74. defer c.JSON(http.StatusOK, ret)
  75. arg, ok := util.JsonArg(c, ret)
  76. if !ok {
  77. return
  78. }
  79. snippetsArg := arg["snippets"].([]interface{})
  80. var snippets []*conf.Snippet
  81. for _, s := range snippetsArg {
  82. m := s.(map[string]interface{})
  83. snippet := &conf.Snippet{
  84. ID: m["id"].(string),
  85. Name: m["name"].(string),
  86. Type: m["type"].(string),
  87. Content: m["content"].(string),
  88. Enabled: m["enabled"].(bool),
  89. }
  90. if "" == snippet.ID {
  91. snippet.ID = ast.NewNodeID()
  92. }
  93. snippets = append(snippets, snippet)
  94. }
  95. err := model.SetSnippet(snippets)
  96. if nil != err {
  97. ret.Code = -1
  98. ret.Msg = "set snippet failed: " + err.Error()
  99. return
  100. }
  101. }
  102. func removeSnippet(c *gin.Context) {
  103. ret := gulu.Ret.NewResult()
  104. defer c.JSON(http.StatusOK, ret)
  105. arg, ok := util.JsonArg(c, ret)
  106. if !ok {
  107. return
  108. }
  109. id := arg["id"].(string)
  110. snippet, err := model.RemoveSnippet(id)
  111. if nil != err {
  112. ret.Code = -1
  113. ret.Msg = "remove snippet failed: " + err.Error()
  114. return
  115. }
  116. ret.Data = snippet
  117. }