widget.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. "strings"
  21. "github.com/siyuan-note/logging"
  22. "github.com/siyuan-note/siyuan/kernel/bazaar"
  23. "github.com/siyuan-note/siyuan/kernel/util"
  24. )
  25. func SearchWidget(keyword string) (ret []*Block) {
  26. ret = []*Block{}
  27. widgetsDir := filepath.Join(util.DataDir, "widgets")
  28. entries, err := os.ReadDir(widgetsDir)
  29. if nil != err {
  30. logging.LogErrorf("read dir [%s] failed: %s", widgetsDir, err)
  31. return
  32. }
  33. k := strings.ToLower(keyword)
  34. var widgets []*bazaar.Widget
  35. for _, entry := range entries {
  36. if !util.IsDirRegularOrSymlink(entry) {
  37. continue
  38. }
  39. if strings.HasPrefix(entry.Name(), ".") {
  40. continue
  41. }
  42. widget, _ := bazaar.WidgetJSON(entry.Name())
  43. if nil == widget {
  44. continue
  45. }
  46. widgets = append(widgets, widget)
  47. }
  48. widgets = filterWidgets(widgets, k)
  49. for _, widget := range widgets {
  50. b := &Block{
  51. Name: bazaar.GetPreferredName(widget.Package),
  52. Content: widget.Name,
  53. }
  54. ret = append(ret, b)
  55. }
  56. return
  57. }