widget.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/search"
  23. "github.com/siyuan-note/siyuan/kernel/util"
  24. )
  25. func SearchWidget(keyword string) (ret []*Block) {
  26. ret = []*Block{}
  27. widgets := filepath.Join(util.DataDir, "widgets")
  28. entries, err := os.ReadDir(widgets)
  29. if nil != err {
  30. logging.LogErrorf("read dir [%s] failed: %s", widgets, err)
  31. return
  32. }
  33. k := strings.ToLower(keyword)
  34. for _, entry := range entries {
  35. if !util.IsDirRegularOrSymlink(entry) {
  36. continue
  37. }
  38. isWidgetDir := false
  39. subEntries, readErr := os.ReadDir(filepath.Join(widgets, entry.Name()))
  40. if nil != readErr {
  41. logging.LogWarnf("read dir [%s] failed: %s", filepath.Join(widgets, entry.Name()), readErr)
  42. continue
  43. }
  44. for _, subEntry := range subEntries {
  45. if !subEntry.IsDir() && "widget.json" == subEntry.Name() {
  46. isWidgetDir = true
  47. break
  48. }
  49. }
  50. if !isWidgetDir {
  51. continue
  52. }
  53. name := strings.ToLower(entry.Name())
  54. if strings.HasPrefix(name, ".") {
  55. continue
  56. }
  57. if strings.Contains(name, k) {
  58. name = entry.Name()
  59. if "" != keyword {
  60. _, name = search.MarkText(entry.Name(), keyword, 32, Conf.Search.CaseSensitive)
  61. }
  62. b := &Block{Content: name}
  63. ret = append(ret, b)
  64. }
  65. }
  66. return
  67. }