This commit is contained in:
Daniel 2024-04-07 10:03:31 +08:00
parent 62db617f91
commit 1a487c0251
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
7 changed files with 31 additions and 40 deletions

View file

@ -22,54 +22,45 @@ import (
"strings"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/search"
"github.com/siyuan-note/siyuan/kernel/bazaar"
"github.com/siyuan-note/siyuan/kernel/util"
)
func SearchWidget(keyword string) (ret []*Block) {
ret = []*Block{}
widgets := filepath.Join(util.DataDir, "widgets")
entries, err := os.ReadDir(widgets)
widgetsDir := filepath.Join(util.DataDir, "widgets")
entries, err := os.ReadDir(widgetsDir)
if nil != err {
logging.LogErrorf("read dir [%s] failed: %s", widgets, err)
logging.LogErrorf("read dir [%s] failed: %s", widgetsDir, err)
return
}
k := strings.ToLower(keyword)
var widgets []*bazaar.Widget
for _, entry := range entries {
if !util.IsDirRegularOrSymlink(entry) {
continue
}
isWidgetDir := false
subEntries, readErr := os.ReadDir(filepath.Join(widgets, entry.Name()))
if nil != readErr {
logging.LogWarnf("read dir [%s] failed: %s", filepath.Join(widgets, entry.Name()), readErr)
continue
}
for _, subEntry := range subEntries {
if !subEntry.IsDir() && "widget.json" == subEntry.Name() {
isWidgetDir = true
break
}
}
if !isWidgetDir {
if strings.HasPrefix(entry.Name(), ".") {
continue
}
name := strings.ToLower(entry.Name())
if strings.HasPrefix(name, ".") {
widget, _ := bazaar.WidgetJSON(entry.Name())
if nil == widget {
continue
}
if strings.Contains(name, k) {
name = entry.Name()
if "" != keyword {
_, name = search.MarkText(entry.Name(), keyword, 32, Conf.Search.CaseSensitive)
}
b := &Block{Content: name}
ret = append(ret, b)
}
widgets = append(widgets, widget)
}
widgets = filterWidgets(widgets, k)
for _, widget := range widgets {
b := &Block{
Name: bazaar.GetPreferredName(widget.Package),
Content: widget.Name,
}
ret = append(ret, b)
}
return
}