2023-06-24 20:39:55 +08:00
|
|
|
|
// SiYuan - Refactor your thinking
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// Copyright (c) 2020-present, b3log.org
|
|
|
|
|
|
//
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
//
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
package bazaar
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-03-08 11:09:46 +08:00
|
|
|
|
"html"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
"os"
|
2026-03-08 11:09:46 +08:00
|
|
|
|
"path"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
"strings"
|
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/88250/gulu"
|
2022-09-29 21:52:01 +08:00
|
|
|
|
"github.com/siyuan-note/filelock"
|
2022-07-17 12:22:32 +08:00
|
|
|
|
"github.com/siyuan-note/logging"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/util"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-02-03 09:59:36 +08:00
|
|
|
|
// LocaleStrings 表示按语种 key 的字符串表,key 为语种如 "default"、"en_US"、"zh_CN" 等
|
|
|
|
|
|
type LocaleStrings map[string]string
|
2023-05-05 22:00:51 +08:00
|
|
|
|
|
2023-05-04 19:06:54 +08:00
|
|
|
|
type Funding struct {
|
|
|
|
|
|
OpenCollective string `json:"openCollective"`
|
|
|
|
|
|
Patreon string `json:"patreon"`
|
|
|
|
|
|
GitHub string `json:"github"`
|
|
|
|
|
|
Custom []string `json:"custom"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-08 11:09:46 +08:00
|
|
|
|
// Package 描述了集市包元数据和传递给前端的其他信息。
|
2026-03-08 15:49:18 +08:00
|
|
|
|
// - 集市包新增元数据字段需要同步修改 bazaar 的工作流,参考 https://github.com/siyuan-note/bazaar/commit/aa36d0003139c52d8e767c6e18a635be006323e2
|
2022-09-01 21:24:18 +08:00
|
|
|
|
type Package struct {
|
2026-02-03 09:59:36 +08:00
|
|
|
|
Author string `json:"author"`
|
|
|
|
|
|
URL string `json:"url"`
|
|
|
|
|
|
Version string `json:"version"`
|
|
|
|
|
|
MinAppVersion string `json:"minAppVersion"`
|
|
|
|
|
|
DisabledInPublish bool `json:"disabledInPublish"`
|
|
|
|
|
|
Backends []string `json:"backends"`
|
|
|
|
|
|
Frontends []string `json:"frontends"`
|
|
|
|
|
|
DisplayName LocaleStrings `json:"displayName"`
|
|
|
|
|
|
Description LocaleStrings `json:"description"`
|
|
|
|
|
|
Readme LocaleStrings `json:"readme"`
|
|
|
|
|
|
Funding *Funding `json:"funding"`
|
|
|
|
|
|
Keywords []string `json:"keywords"`
|
2023-05-05 22:00:51 +08:00
|
|
|
|
|
|
|
|
|
|
PreferredFunding string `json:"preferredFunding"`
|
2023-05-07 17:10:23 +08:00
|
|
|
|
PreferredName string `json:"preferredName"`
|
2023-05-05 22:00:51 +08:00
|
|
|
|
PreferredDesc string `json:"preferredDesc"`
|
2023-05-09 09:12:29 +08:00
|
|
|
|
PreferredReadme string `json:"preferredReadme"`
|
2022-09-01 21:24:18 +08:00
|
|
|
|
|
2026-03-08 11:09:46 +08:00
|
|
|
|
Name string `json:"name"` // 包名,不一定是仓库名
|
|
|
|
|
|
RepoURL string `json:"repoURL"` // 形式为 https://github.com/owner/repo
|
|
|
|
|
|
RepoHash string `json:"repoHash"`
|
|
|
|
|
|
PreviewURL string `json:"previewURL"`
|
|
|
|
|
|
IconURL string `json:"iconURL"`
|
2022-09-01 21:24:18 +08:00
|
|
|
|
|
2025-12-30 17:27:23 +08:00
|
|
|
|
Installed bool `json:"installed"`
|
|
|
|
|
|
Outdated bool `json:"outdated"`
|
|
|
|
|
|
Current bool `json:"current"`
|
|
|
|
|
|
Updated string `json:"updated"`
|
|
|
|
|
|
Stars int `json:"stars"`
|
|
|
|
|
|
OpenIssues int `json:"openIssues"`
|
|
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
|
|
HSize string `json:"hSize"`
|
|
|
|
|
|
InstallSize int64 `json:"installSize"`
|
|
|
|
|
|
HInstallSize string `json:"hInstallSize"`
|
|
|
|
|
|
HInstallDate string `json:"hInstallDate"`
|
|
|
|
|
|
HUpdated string `json:"hUpdated"`
|
|
|
|
|
|
Downloads int `json:"downloads"`
|
|
|
|
|
|
DisallowInstall bool `json:"disallowInstall"`
|
|
|
|
|
|
DisallowUpdate bool `json:"disallowUpdate"`
|
|
|
|
|
|
UpdateRequiredMinAppVer string `json:"updateRequiredMinAppVer"`
|
2023-05-29 20:16:23 +08:00
|
|
|
|
|
2026-03-08 11:09:46 +08:00
|
|
|
|
// 专用字段,nil 时不序列化
|
|
|
|
|
|
Incompatible *bool `json:"incompatible,omitempty"` // Plugin:是否不兼容
|
|
|
|
|
|
Enabled *bool `json:"enabled,omitempty"` // Plugin:是否启用
|
|
|
|
|
|
Modes *[]string `json:"modes,omitempty"` // Theme:支持的模式列表
|
2022-09-01 21:24:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-05 23:07:06 +08:00
|
|
|
|
type StageRepo struct {
|
2026-03-08 11:09:46 +08:00
|
|
|
|
URL string `json:"url"` // owner/repo@hash 形式
|
2024-04-28 22:48:05 +08:00
|
|
|
|
Updated string `json:"updated"`
|
|
|
|
|
|
Stars int `json:"stars"`
|
|
|
|
|
|
OpenIssues int `json:"openIssues"`
|
|
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
|
|
InstallSize int64 `json:"installSize"`
|
2023-05-05 22:00:51 +08:00
|
|
|
|
|
2026-02-03 10:07:16 +08:00
|
|
|
|
// Package 与 stage/*.json 内嵌的完整 package 一致,可直接用于构建列表
|
|
|
|
|
|
Package *Package `json:"package"`
|
2023-05-05 23:07:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type StageIndex struct {
|
|
|
|
|
|
Repos []*StageRepo `json:"repos"`
|
2023-05-07 17:10:23 +08:00
|
|
|
|
|
2026-03-08 15:49:18 +08:00
|
|
|
|
reposByURL map[string]*StageRepo // 不序列化,首次按 URL 查找时懒构建,随整份索引一起过期
|
2026-03-08 11:09:46 +08:00
|
|
|
|
reposOnce sync.Once
|
2023-05-05 22:00:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-08 11:09:46 +08:00
|
|
|
|
// ParsePackageJSON 解析集市包 JSON 文件
|
|
|
|
|
|
func ParsePackageJSON(filePath string) (ret *Package, err error) {
|
|
|
|
|
|
if !filelock.IsExist(filePath) {
|
2023-04-25 18:52:19 +08:00
|
|
|
|
err = os.ErrNotExist
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
data, err := filelock.ReadFile(filePath)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2026-03-08 11:09:46 +08:00
|
|
|
|
logging.LogErrorf("read [%s] failed: %s", filePath, err)
|
2023-04-25 18:52:19 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = gulu.JSON.UnmarshalJSON(data, &ret); err != nil {
|
2026-03-08 11:09:46 +08:00
|
|
|
|
logging.LogErrorf("parse [%s] failed: %s", filePath, err)
|
2023-04-25 18:52:19 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2023-05-05 23:07:06 +08:00
|
|
|
|
|
2026-03-08 11:09:46 +08:00
|
|
|
|
// 仅对本地集市包做 HTML 转义,在线 stage 由 bazaar 工作流处理
|
|
|
|
|
|
sanitizePackageDisplayStrings(ret)
|
2023-05-05 23:07:06 +08:00
|
|
|
|
ret.URL = strings.TrimSuffix(ret.URL, "/")
|
2023-04-25 18:52:19 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-08 11:09:46 +08:00
|
|
|
|
// sanitizePackageDisplayStrings 对集市包直接显示的信息做 HTML 转义,避免 XSS。
|
|
|
|
|
|
func sanitizePackageDisplayStrings(pkg *Package) {
|
|
|
|
|
|
if pkg == nil {
|
2022-09-01 21:33:24 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
for k, v := range pkg.DisplayName {
|
|
|
|
|
|
pkg.DisplayName[k] = html.EscapeString(v)
|
2022-09-01 21:33:24 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
for k, v := range pkg.Description {
|
|
|
|
|
|
pkg.Description[k] = html.EscapeString(v)
|
2022-09-01 21:33:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-08 11:09:46 +08:00
|
|
|
|
// GetPreferredLocaleString 从 LocaleStrings 中按当前语种取值,无则回退 default、en_US,再回退 fallback。
|
|
|
|
|
|
func GetPreferredLocaleString(m LocaleStrings, fallback string) string {
|
|
|
|
|
|
if len(m) == 0 {
|
|
|
|
|
|
return fallback
|
2022-09-01 21:24:18 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
if v := strings.TrimSpace(m[util.Lang]); "" != v {
|
|
|
|
|
|
return v
|
2022-09-01 21:24:18 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
if v := strings.TrimSpace(m["default"]); "" != v {
|
|
|
|
|
|
return v
|
2022-09-01 21:24:18 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
if v := strings.TrimSpace(m["en_US"]); "" != v {
|
|
|
|
|
|
return v
|
|
|
|
|
|
}
|
|
|
|
|
|
return fallback
|
2022-09-01 21:24:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-08 11:09:46 +08:00
|
|
|
|
// getPreferredFunding 获取包的首选赞助链接
|
|
|
|
|
|
func getPreferredFunding(funding *Funding) string {
|
|
|
|
|
|
if nil == funding {
|
|
|
|
|
|
return ""
|
2022-09-01 19:37:33 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
if v := normalizeFundingURL(funding.OpenCollective, "https://opencollective.com/"); "" != v {
|
|
|
|
|
|
return v
|
2022-09-01 19:37:33 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
if v := normalizeFundingURL(funding.Patreon, "https://www.patreon.com/"); "" != v {
|
|
|
|
|
|
return v
|
2022-09-01 19:37:33 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
if v := normalizeFundingURL(funding.GitHub, "https://github.com/sponsors/"); "" != v {
|
|
|
|
|
|
return v
|
2022-09-01 19:37:33 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
if 0 < len(funding.Custom) {
|
|
|
|
|
|
return funding.Custom[0]
|
2022-09-01 19:37:33 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
return ""
|
2022-09-01 19:37:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-08 11:09:46 +08:00
|
|
|
|
func normalizeFundingURL(s, base string) string {
|
|
|
|
|
|
if "" == s {
|
|
|
|
|
|
return ""
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
if strings.HasPrefix(s, "https://") || strings.HasPrefix(s, "http://") {
|
|
|
|
|
|
return s
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
return base + s
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-08 11:09:46 +08:00
|
|
|
|
// FilterPackages 按关键词过滤集市包列表
|
|
|
|
|
|
func FilterPackages(packages []*Package, keyword string) []*Package {
|
|
|
|
|
|
keywords := getSearchKeywords(keyword)
|
|
|
|
|
|
if 0 == len(keywords) {
|
|
|
|
|
|
return packages
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
ret := []*Package{}
|
|
|
|
|
|
for _, pkg := range packages {
|
|
|
|
|
|
if packageContainsKeywords(pkg, keywords) {
|
|
|
|
|
|
ret = append(ret, pkg)
|
|
|
|
|
|
}
|
2024-04-14 23:52:09 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
return ret
|
2024-04-14 23:52:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-08 11:09:46 +08:00
|
|
|
|
func getSearchKeywords(query string) (ret []string) {
|
|
|
|
|
|
query = strings.TrimSpace(query)
|
|
|
|
|
|
if "" == query {
|
2024-04-14 23:52:09 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
keywords := strings.Split(query, " ")
|
|
|
|
|
|
for _, k := range keywords {
|
|
|
|
|
|
if "" != k {
|
|
|
|
|
|
ret = append(ret, strings.ToLower(k))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-04-14 23:52:09 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-08 11:09:46 +08:00
|
|
|
|
func packageContainsKeywords(pkg *Package, keywords []string) bool {
|
|
|
|
|
|
if 0 == len(keywords) {
|
|
|
|
|
|
return true
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
if nil == pkg {
|
|
|
|
|
|
return false
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
for _, kw := range keywords {
|
|
|
|
|
|
if !packageContainsKeyword(pkg, kw) {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
return true
|
|
|
|
|
|
}
|
2023-05-05 15:38:44 +08:00
|
|
|
|
|
2026-03-08 11:09:46 +08:00
|
|
|
|
func packageContainsKeyword(pkg *Package, kw string) bool {
|
|
|
|
|
|
if strings.Contains(strings.ToLower(pkg.Name), kw) || // https://github.com/siyuan-note/siyuan/issues/10515
|
|
|
|
|
|
strings.Contains(strings.ToLower(pkg.Author), kw) { // https://github.com/siyuan-note/siyuan/issues/11673
|
|
|
|
|
|
return true
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
for _, s := range pkg.DisplayName {
|
|
|
|
|
|
if strings.Contains(strings.ToLower(s), kw) {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
for _, s := range pkg.Description {
|
|
|
|
|
|
if strings.Contains(strings.ToLower(s), kw) {
|
|
|
|
|
|
return true
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
for _, s := range pkg.Keywords {
|
|
|
|
|
|
if strings.Contains(strings.ToLower(s), kw) {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
2023-05-24 10:38:30 +08:00
|
|
|
|
}
|
2026-03-08 11:09:46 +08:00
|
|
|
|
if strings.Contains(strings.ToLower(path.Base(pkg.RepoURL)), kw) { // 仓库名,不一定是包名
|
2023-05-29 20:16:23 +08:00
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
return false
|
2023-05-24 10:38:30 +08:00
|
|
|
|
}
|