Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Improve marketplace package funding display https://github.com/siyuan-note/siyuan/issues/18535

Daniela7c29987c8b1
diff --git a/app/src/config/bazaar.ts b/app/src/config/bazaar.ts
index 928eb8eedf..6f22dac072 100644
--- a/app/src/config/bazaar.ts
+++ b/app/src/config/bazaar.ts
@@ -21,6 +21,7 @@ import {switchSettingPanelSubTab} from "./setting/mount";
import {isThemeFrontendSupported} from "../util/themeCompatibility";
import {
getBazaarCompatibilityFieldVisibility,
+ getBazaarFundingItems,
getBazaarThemeModeLabels,
} from "../util/bazaarPackage";
@@ -824,11 +825,15 @@ type="checkbox">
<span class="fn__space--small"></span>
${formatCount(resourceData.downloads)}
</div>` : "";
+ const fundingItems = getBazaarFundingItems(resourceData.funding);
+ if (fundingItems.length === 0 && resourceData.preferredFunding) {
+ fundingItems.push(resourceData.preferredFunding);
+ }
const packageSection = `<section class="item__meta-section">
<div class="item__meta-title">${window.siyuan.languages.bazaarPackageInfo}</div>
${bazaar._genReadmeMetaRow(window.siyuan.languages.bazaarPackageName, displayData.name)}
${displayData.author ? bazaar._genReadmeMetaRow(window.siyuan.languages.author, `<a href="${escapeAttr(urls.join("/"))}" target="_blank" title="Creator">${escapeHtml(displayData.author)}</a>`, true) : ""}
- ${resourceData.preferredFunding ? bazaar._genReadmeMetaRow(window.siyuan.languages.bazaarFunding, bazaar._genReadmeFundingHTML(resourceData.preferredFunding), true) : ""}
+ ${fundingItems.length ? bazaar._genReadmeMetaRow(window.siyuan.languages.bazaarFunding, fundingItems.map((item) => bazaar._genReadmeFundingHTML(item)).join("<br>"), true) : ""}
</section>`;
readmeElement.innerHTML = ` <div class="item__side" data-from="${from}" data-name="${escapeAttr(displayData.name)}" data-package-type="${bazaarType}" data-repourl="${escapeAttr(resourceData.repoURL)}" data-progress-id="${escapeAttr(available?.repoURL || resourceData.repoURL)}">
<div class="item__header fn__pointer" data-type="goBack">
diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts
index 0e39706360..f3462be791 100644
--- a/app/src/types/index.d.ts
+++ b/app/src/types/index.d.ts
@@ -990,6 +990,13 @@ interface IMenu {
warning?: boolean
}
+interface IBazaarFunding {
+ openCollective?: string;
+ patreon?: string;
+ github?: string;
+ custom?: string[];
+}
+
interface IBazaarItem {
preferredName: string;
minAppVersion: string;
@@ -1022,6 +1029,7 @@ interface IBazaarItem {
updateTime: number;
hInstallDate: string;
hUpdated: string;
+ funding?: IBazaarFunding;
preferredFunding: string;
disallowUpdate: boolean;
updateRequiredMinAppVer?: string;
diff --git a/app/src/util/bazaarPackage.test.ts b/app/src/util/bazaarPackage.test.ts
index 43d4eee19f..66ec8f9437 100644
--- a/app/src/util/bazaarPackage.test.ts
+++ b/app/src/util/bazaarPackage.test.ts
@@ -2,6 +2,7 @@ import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {
getBazaarCompatibilityFieldVisibility,
+ getBazaarFundingItems,
getBazaarThemeModeLabels,
isValidBazaarPackageName,
} from "./bazaarPackage";
@@ -45,6 +46,29 @@ describe("getBazaarThemeModeLabels", () => {
});
});
+describe("getBazaarFundingItems", () => {
+ it("normalizes platform values and preserves custom item order", () => {
+ assert.deepEqual(getBazaarFundingItems({
+ openCollective: "collective",
+ patreon: "https://example.com/patreon",
+ github: "sponsor",
+ custom: ["custom text", "https://example.com/custom", "custom text"],
+ }), [
+ "https://opencollective.com/collective",
+ "https://example.com/patreon",
+ "https://github.com/sponsors/sponsor",
+ "custom text",
+ "https://example.com/custom",
+ "custom text",
+ ]);
+ });
+
+ it("removes empty values without discarding later custom items", () => {
+ assert.deepEqual(getBazaarFundingItems({custom: ["", "https://example.com"]}), ["https://example.com"]);
+ assert.deepEqual(getBazaarFundingItems(undefined), []);
+ });
+});
+
describe("isValidBazaarPackageName", () => {
it("accepts valid package names", () => {
assert.equal(isValidBazaarPackageName("plugin-sample"), true);
diff --git a/app/src/util/bazaarPackage.ts b/app/src/util/bazaarPackage.ts
index e2f94ae08a..357ed44100 100644
--- a/app/src/util/bazaarPackage.ts
+++ b/app/src/util/bazaarPackage.ts
@@ -29,6 +29,28 @@ export const getBazaarThemeModeLabels = (
return mode;
})));
+const normalizeBazaarFundingURL = (value: string | undefined, base: string) => {
+ if (!value) {
+ return "";
+ }
+ if (value.startsWith("https://") || value.startsWith("http://")) {
+ return value;
+ }
+ return `${base}${value}`;
+};
+
+export const getBazaarFundingItems = (funding: IBazaarFunding | null | undefined) => {
+ if (!funding) {
+ return [];
+ }
+ return [
+ normalizeBazaarFundingURL(funding.openCollective, "https://opencollective.com/"),
+ normalizeBazaarFundingURL(funding.patreon, "https://www.patreon.com/"),
+ normalizeBazaarFundingURL(funding.github, "https://github.com/sponsors/"),
+ ...(funding.custom || []),
+ ].filter(Boolean);
+};
+
export const isValidBazaarPackageName = (name: string) => {
if (!/^[\x20-\x7E]{1,255}$/.test(name) || /^[. ]/.test(name) || /[. ]$/.test(name)) {
return false;
diff --git a/kernel/bazaar/package.go b/kernel/bazaar/package.go
index 0ce475b6e5..e905a82855 100644
--- a/kernel/bazaar/package.go
+++ b/kernel/bazaar/package.go
@@ -197,16 +197,41 @@ func getPreferredFunding(funding *Funding) string {
if v := normalizeFundingURL(funding.GitHub, "https://github.com/sponsors/"); "" != v {
return v
}
- if 0 < len(funding.Custom) {
- v := funding.Custom[0]
- if strings.HasPrefix(v, "https://") || strings.HasPrefix(v, "http://") || strings.HasPrefix(v, "mailto:") {
+ for _, v := range funding.Custom {
+ if !unsafeFundingURI(v) && "" != strings.TrimSpace(v) {
return v
}
- return ""
}
return ""
}
+// unsafeFundingURI 判断自定义赞助信息是否包含危险或不受支持的 URI 协议。
+func unsafeFundingURI(s string) bool {
+ s = strings.TrimSpace(strings.ToLower(s))
+ if "" == s || strings.HasPrefix(s, "https://") || strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "mailto:") {
+ return false
+ }
+
+ i := strings.IndexByte(s, ':')
+ if i <= 0 {
+ return false
+ }
+ scheme := s[:i]
+ for _, r := range scheme {
+ if (r < 'a' || r > 'z') && (r < '0' || r > '9') && r != '+' && r != '-' && r != '.' {
+ return false
+ }
+ }
+ if scheme[0] < 'a' || scheme[0] > 'z' {
+ return false
+ }
+ switch scheme {
+ case "javascript", "data", "file", "vbscript", "blob":
+ return true
+ }
+ return strings.HasPrefix(s[i:], "://")
+}
+
func normalizeFundingURL(s, base string) string {
if "" == s {
return ""
diff --git a/kernel/bazaar/package_test.go b/kernel/bazaar/package_test.go
new file mode 100644
index 0000000000..565f8fb7f8
--- /dev/null
+++ b/kernel/bazaar/package_test.go
@@ -0,0 +1,72 @@
+// SiYuan - Refactor your thinking
+// 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 "testing"
+
+func TestGetPreferredFunding(t *testing.T) {
+ tests := []struct {
+ name string
+ funding *Funding
+ want string
+ }{
+ {name: "missing funding"},
+ {
+ name: "platform priority",
+ funding: &Funding{
+ OpenCollective: "collective",
+ Patreon: "patron",
+ GitHub: "sponsor",
+ Custom: []string{"https://example.com"},
+ },
+ want: "https://opencollective.com/collective",
+ },
+ {
+ name: "complete platform URL",
+ funding: &Funding{GitHub: "https://example.com/sponsor"},
+ want: "https://example.com/sponsor",
+ },
+ {
+ name: "custom text",
+ funding: &Funding{Custom: []string{"支付宝:example"}},
+ want: "支付宝:example",
+ },
+ {
+ name: "custom mail address",
+ funding: &Funding{Custom: []string{"mailto:[email protected]"}},
+ want: "mailto:[email protected]",
+ },
+ {
+ name: "skip invalid custom entries",
+ funding: &Funding{Custom: []string{"", "javascript:alert(1)", "ftp://example.com", "https://example.com"}},
+ want: "https://example.com",
+ },
+ {
+ name: "plain text with colon",
+ funding: &Funding{Custom: []string{"Note: scan the QR code"}},
+ want: "Note: scan the QR code",
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ if got := getPreferredFunding(test.funding); got != test.want {
+ t.Fatalf("expected %q, got %q", test.want, got)
+ }
+ })
+ }
+}