Skip to content

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

:art: Open bazaar resouce readme by `siyuan://bazaar/{type}/{name}/readme` URI (#17938)

Yingyi / 颖逸16760a559738
diff --git a/app/src/boot/onGetConfig.ts b/app/src/boot/onGetConfig.ts
index c6314c92ff..c53baa6f39 100644
--- a/app/src/boot/onGetConfig.ts
+++ b/app/src/boot/onGetConfig.ts
@@ -27,7 +27,7 @@ import {sendGlobalShortcut} from "./globalEvent/keydown";
import {closeWindow} from "../window/closeWin";
import {correctHotkey} from "./globalEvent/commonHotkey";
import {recordBeforeResizeTop} from "../protyle/util/resize";
-import {processSiYuanUri} from "../editor/openLink";
+import {processSiYuanUri} from "../util/uri";
import {getAllEditor} from "../layout/getAll";
export const onGetConfig = (isStart: boolean, app: App) => {
diff --git a/app/src/config/bazaar.ts b/app/src/config/bazaar.ts
index 8ba3077b98..51d83929c4 100644
--- a/app/src/config/bazaar.ts
+++ b/app/src/config/bazaar.ts
@@ -53,6 +53,14 @@ export const mountBazaarTab = (root: HTMLElement, keywords?: string, app?: App)
}
};
+/**
+ * 渲染集市 README
+ */
+export const renderReadme = (bazaarType: TBazaarType, from: "downloaded" | "updated" | "bazaar", data: IBazaarItem) => {
+ if (bazaar.element == null) return;
+ bazaar._renderReadme(bazaarType, from, data);
+};
+
const bazaar = {
element: undefined as Element,
genHTML() {
diff --git a/app/src/config/index.ts b/app/src/config/index.ts
index 9514f7ec7d..5b86879396 100644
--- a/app/src/config/index.ts
+++ b/app/src/config/index.ts
@@ -6,11 +6,13 @@ import {bindSettingSaveDelegation} from "./setting/save";
import {Dialog} from "../dialog";
import {Constants} from "../constants";
import {focusByRange} from "../protyle/util/selection";
+import {renderReadme} from "./bazaar";
+import {fetchSyncPost} from "../util/fetch";
/// #endif
-import type {TSettingTab} from "./setting/tabs";
import {getSettingTabDefs} from "./setting/tabs";
import {clearAccessTabElement} from "./tabs/accessRuntime";
import {clearSyncTabElement} from "./tabs/syncRuntime";
+import type {TSettingTab} from "./setting/tabs";
import type {App} from "../index";
/// #if !MOBILE
@@ -80,3 +82,37 @@ export const openSetting = (app: App, tab?: TSettingTab) => {
return openSettingDialog(app, tab);
/// #endif
};
+
+export const openBazaarReadme = async (app: App, bazaarType: TBazaarType, itemName: string) => {
+ /// #if !MOBILE
+ let getResourcesUrl: string;
+ switch (bazaarType) {
+ case "templates":
+ getResourcesUrl = "/api/bazaar/getBazaarTemplate";
+ break;
+ case "icons":
+ getResourcesUrl = "/api/bazaar/getBazaarIcon";
+ break;
+ case "widgets":
+ getResourcesUrl = "/api/bazaar/getBazaarWidget";
+ break;
+ case "themes":
+ getResourcesUrl = "/api/bazaar/getBazaarTheme";
+ break;
+ case "plugins":
+ getResourcesUrl = "/api/bazaar/getBazaarPlugin";
+ break;
+ default:
+ return;
+ }
+
+ const response = await fetchSyncPost(getResourcesUrl, {frontend: "all", keyword: itemName});
+ if (response.code !== 0) return;
+
+ const resource = (response.data.packages as IBazaarItem[]).find((item: IBazaarItem) => item.name === itemName);
+ if (!resource) return;
+
+ openSettingDialog(app, "bazaar");
+ renderReadme(bazaarType, "bazaar", resource);
+ /// #endif
+};
diff --git a/app/src/editor/openLink.ts b/app/src/editor/openLink.ts
index 5e57db3dd5..9b0a22bd42 100644
--- a/app/src/editor/openLink.ts
+++ b/app/src/editor/openLink.ts
@@ -1,125 +1,16 @@
-import {isLocalPath, isSiYuanUriProtocol, parseSiYuanUriInfo, pathPosix} from "../util/pathName";
+import {isLocalPath, pathPosix} from "../util/pathName";
/// #if !BROWSER
-import {shell, ipcRenderer} from "electron";
+import {shell} from "electron";
/// #endif
import {getSearch} from "../util/functions";
import {Constants} from "../constants";
-/// #if !MOBILE
-import {openAsset, openBy, openFile, openFileById} from "./util";
+/// #if MOBILE
+import {processSiYuanUri} from "../util/uri";
+/// #else
+import {openAsset, openBy} from "./util";
/// #endif
import {showMessage} from "../dialog/message";
import {isInIOS, isInAndroid, isInHarmony} from "../protyle/util/compatibility";
-import {fetchPost} from "../util/fetch";
-import {checkFold} from "../util/noRelyPCFunction";
-import {openMobileFileById} from "../mobile/editor";
-
-import type {App} from "../index";
-
-const processSiYuanUriBlocks = (app: App, uriObj: URL): boolean => {
- const blockInfo = parseSiYuanUriInfo(uriObj);
- if (blockInfo != null) {
- const {id, focus} = blockInfo;
- window.siyuan.editorIsFullscreen = blockInfo.fullscreen;
- fetchPost("/api/block/checkBlockExist", { id }, existResponse => {
- if (existResponse.data) {
- checkFold(id, (zoomIn) => {
- /// #if !MOBILE
- openFileById({
- app,
- id,
- action: (zoomIn || focus) ? [Constants.CB_GET_FOCUS, Constants.CB_GET_HL, Constants.CB_GET_ALL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL],
- zoomIn: zoomIn || focus
- });
- /// #else
- openMobileFileById(app, id, (zoomIn || focus) ? [Constants.CB_GET_FOCUS, Constants.CB_GET_HL, Constants.CB_GET_ALL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]);
- /// #endif
- });
- /// #if !BROWSER
- ipcRenderer.send(Constants.SIYUAN_CMD, "show");
- /// #endif
- }
- app.plugins.forEach(plugin => {
- plugin.eventBus.emit("open-siyuan-url-block", {
- url: uriObj.href,
- id,
- focus,
- exist: existResponse.data,
- });
- });
- });
- return true;
- }
- return false;
-};
-
-const processSiYuanUriPlugins = (app: App, uriObj: URL): boolean => {
- const pluginNameOrTabType: string | null = (() => {
- const name = uriObj.pathname.split("/")[1];
- if (!name) {
- return null;
- }
- try {
- return decodeURIComponent(name);
- } catch (error) {
- return null;
- }
- })();
-
- if (!pluginNameOrTabType) {
- return false;
- }
-
- const plugin = app.plugins.find(plugin => pluginNameOrTabType === plugin.name);
- if (plugin) {
- // siyuan://plugins/plugin-name/foo?bar=baz
- plugin.eventBus.emit("open-siyuan-url-plugin", { url: uriObj.href });
- } else {
- // siyuan://plugins/plugin-samplecustom_tab?title=自定义页签&icon=iconFace&data={"text": "This is the custom plugin tab I opened via protocol."}
- /// #if !MOBILE
- // https://github.com/siyuan-note/siyuan/pull/9256
- const data = (() => {
- try {
- return JSON.parse(uriObj.searchParams.get("data") || "{}");
- } catch (e) {
- console.log("Error open plugin tab with protocol:", e);
- return undefined;
- }
- })();
- // id 不存在时无副作用
- openFile({
- app,
- custom: {
- title: uriObj.searchParams.get("title") ?? pluginNameOrTabType,
- icon: uriObj.searchParams.get("icon") ?? "iconPlugin",
- data,
- id: pluginNameOrTabType
- },
- });
- /// #endif
- }
- return true;
-};
-
-export const processSiYuanUri = (app: App, uri: string) => {
- let uriObj: URL;
- try {
- uriObj = new URL(uri);
- if (!isSiYuanUriProtocol(uriObj)) {
- return false;
- }
- } catch (error) {
- return false;
- }
- switch (uriObj.hostname) {
- case "blocks":
- return processSiYuanUriBlocks(app, uriObj);
- case "plugins":
- return processSiYuanUriPlugins(app, uriObj);
- default:
- break;
- }
- return false;
-};
export const openLink = (protyle: IProtyle, aLink: string, event?: MouseEvent, ctrlIsPressed = false) => {
let linkAddress = Lute.UnEscapeHTMLStr(aLink);
diff --git a/app/src/layout/dock/agent/AgentMessageRenderer.ts b/app/src/layout/dock/agent/AgentMessageRenderer.ts
index 4cabea1669..1e3b1c2b72 100644
--- a/app/src/layout/dock/agent/AgentMessageRenderer.ts
+++ b/app/src/layout/dock/agent/AgentMessageRenderer.ts
@@ -1,4 +1,5 @@
import {escapeHtml} from "../../../util/escape";
+import {processSiYuanUri} from "../../../util/uri";
import {highlightRender} from "../../../protyle/render/highlightRender";
import {mathRender} from "../../../protyle/render/mathRender";
import {mermaidRender} from "../../../protyle/render/mermaidRender";
@@ -10,8 +11,8 @@ import {abcRender} from "../../../protyle/render/abcRender";
import {plantumlRender} from "../../../protyle/render/plantumlRender";
import {htmlRender} from "../../../protyle/render/htmlRender";
import {showMessage} from "../../../dialog/message";
+
import type {App} from "../../../index";
-import {processSiYuanUri} from "../../../editor/openLink";
export const renderTodoList = (result: string): string => {
const L = window.siyuan.languages;
diff --git a/app/src/util/pathName.ts b/app/src/util/pathName.ts
index 7f583766e4..19331c3ec8 100644
--- a/app/src/util/pathName.ts
+++ b/app/src/util/pathName.ts
@@ -73,9 +73,8 @@ export const parseSiYuanUriInfo = (uri: URL | string | null | undefined): ISiYua
export const parseUriInfo = (): ISiYuanUriBlockInfo => {
const searchParams = new URLSearchParams(window.location.search);
- const PWAURL = searchParams.get("url");
- if (/^web\+siyuan:\/\/blocks\/\d{14}-\w{7}/.test(PWAURL)) {
- const dataInfo = parseSiYuanUriInfo(PWAURL);
+ if (searchParams.has("url")) {
+ const dataInfo = parseSiYuanUriInfo(searchParams.get("url"));
if (dataInfo != null) {
window.siyuan.editorIsFullscreen = dataInfo.fullscreen;
return dataInfo;
diff --git a/app/src/util/uri.ts b/app/src/util/uri.ts
new file mode 100644
index 0000000000..a917349dd6
--- /dev/null
+++ b/app/src/util/uri.ts
@@ -0,0 +1,147 @@
+import {isSiYuanUriProtocol, parseSiYuanUriInfo} from "./pathName";
+/// #if !BROWSER
+import {ipcRenderer} from "electron";
+/// #endif
+import {Constants} from "../constants";
+/// #if !MOBILE
+import {openFile, openFileById} from "../editor/util";
+/// #endif
+import {fetchPost} from "./fetch";
+import {checkFold} from "./noRelyPCFunction";
+import {openMobileFileById} from "../mobile/editor";
+
+import type {App} from "../index";
+
+const processSiYuanUriBlocks = (app: App, uriObj: URL): boolean => {
+ const blockInfo = parseSiYuanUriInfo(uriObj);
+ if (blockInfo != null) {
+ const {id, focus} = blockInfo;
+ window.siyuan.editorIsFullscreen = blockInfo.fullscreen;
+ fetchPost("/api/block/checkBlockExist", { id }, existResponse => {
+ if (existResponse.data) {
+ checkFold(id, (zoomIn) => {
+ /// #if !MOBILE
+ openFileById({
+ app,
+ id,
+ action: (zoomIn || focus) ? [Constants.CB_GET_FOCUS, Constants.CB_GET_HL, Constants.CB_GET_ALL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL],
+ zoomIn: zoomIn || focus
+ });
+ /// #else
+ openMobileFileById(app, id, (zoomIn || focus) ? [Constants.CB_GET_FOCUS, Constants.CB_GET_HL, Constants.CB_GET_ALL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]);
+ /// #endif
+ });
+ /// #if !BROWSER
+ ipcRenderer.send(Constants.SIYUAN_CMD, "show");
+ /// #endif
+ }
+ app.plugins.forEach(plugin => {
+ plugin.eventBus.emit("open-siyuan-url-block", {
+ url: uriObj.href,
+ id,
+ focus,
+ exist: existResponse.data,
+ });
+ });
+ });
+ return true;
+ }
+ return false;
+};
+
+const processSiYuanUriPlugins = (app: App, uriObj: URL): boolean => {
+ const pluginNameOrTabType: string | null = (() => {
+ const name = uriObj.pathname.split("/")[1];
+ if (!name) {
+ return null;
+ }
+ try {
+ return decodeURIComponent(name);
+ } catch (error) {
+ return null;
+ }
+ })();
+
+ if (!pluginNameOrTabType) {
+ return false;
+ }
+
+ const plugin = app.plugins.find(plugin => pluginNameOrTabType === plugin.name);
+ if (plugin) {
+ // siyuan://plugins/plugin-name/foo?bar=baz
+ plugin.eventBus.emit("open-siyuan-url-plugin", { url: uriObj.href });
+ } else {
+ // siyuan://plugins/plugin-samplecustom_tab?title=自定义页签&icon=iconFace&data={"text": "This is the custom plugin tab I opened via protocol."}
+ /// #if !MOBILE
+ // https://github.com/siyuan-note/siyuan/pull/9256
+ const data = (() => {
+ try {
+ return JSON.parse(uriObj.searchParams.get("data") || "{}");
+ } catch (e) {
+ console.log("Error open plugin tab with protocol:", e);
+ return undefined;
+ }
+ })();
+ // id 不存在时无副作用
+ openFile({
+ app,
+ custom: {
+ title: uriObj.searchParams.get("title") ?? pluginNameOrTabType,
+ icon: uriObj.searchParams.get("icon") ?? "iconPlugin",
+ data,
+ id: pluginNameOrTabType
+ },
+ });
+ /// #endif
+ }
+ return true;
+};
+
+const processSiYuanUriBazaar = (app: App, uriObj: URL): boolean => {
+ /// #if !MOBILE
+ const [, _type, _name, target] = uriObj.pathname.split("/");
+ if (!_type || !_name) return false;
+ const resourceType = _type as TBazaarType;
+ let resourceName: string;
+ try {
+ resourceName = decodeURIComponent(_name);
+ } catch {
+ return false;
+ }
+ switch (target) {
+ case "readme":
+ // siyuan://bazaar/plugins/plugin-sample/readme
+ (async () => {
+ const {openBazaarReadme} = await import("../config");
+ openBazaarReadme(app, resourceType, resourceName);
+ })();
+ return true;
+ default:
+ break;
+ }
+ /// #endif
+ return false;
+};
+
+export const processSiYuanUri = (app: App, uri: string) => {
+ let uriObj: URL;
+ try {
+ uriObj = new URL(uri);
+ if (!isSiYuanUriProtocol(uriObj)) {
+ return false;
+ }
+ } catch (error) {
+ return false;
+ }
+ switch (uriObj.hostname) {
+ case "blocks":
+ return processSiYuanUriBlocks(app, uriObj);
+ case "plugins":
+ return processSiYuanUriPlugins(app, uriObj);
+ case "bazaar":
+ return processSiYuanUriBazaar(app, uriObj);
+ default:
+ break;
+ }
+ return false;
+};