An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
Enhance SYLink processing for improved plugin and block handling (#17843)
Yingyi / 颖逸129a9c2ac83a
diff --git a/app/src/boot/onGetConfig.ts b/app/src/boot/onGetConfig.tsindex f65ce06b85..f6e60bebb5 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 {processSYLink} from "../editor/openLink";+import {processSiYuanUri} from "../editor/openLink";import {getAllEditor} from "../layout/getAll";export const onGetConfig = (isStart: boolean, app: App) => {@@ -162,7 +162,7 @@ export const initWindow = async (app: App) => {});if (!isWindow()) {ipcRenderer.on(Constants.SIYUAN_OPEN_URL, (event, url) => {- processSYLink(app, url);+ processSiYuanUri(app, url);});}ipcRenderer.on(Constants.SIYUAN_OPEN_FILE, (event, data) => {diff --git a/app/src/editor/openLink.ts b/app/src/editor/openLink.tsindex ecf0d033b6..2a4b78034a 100644--- a/app/src/editor/openLink.ts+++ b/app/src/editor/openLink.ts@@ -1,4 +1,4 @@-import {getIdFromSYProtocol, isLocalPath, isSYProtocol, pathPosix} from "../util/pathName";+import {isLocalPath, isSiYuanUriProtocol, parseSiYuanUriBlockInfo, pathPosix} from "../util/pathName";/// #if !BROWSERimport {shell, ipcRenderer} from "electron";/// #endif@@ -8,63 +8,19 @@ import {Constants} from "../constants";import {openAsset, openBy, openFile, openFileById} from "./util";/// #endifimport {showMessage} from "../dialog/message";-import {openByMobile} from "../protyle/util/compatibility";-import {App} from "../index";+import {isInIOS, isInAndroid, isInHarmony} from "../protyle/util/compatibility";import {fetchPost} from "../util/fetch";import {checkFold} from "../util/noRelyPCFunction";import {openMobileFileById} from "../mobile/editor";-export const processSYLink = (app: App, url: string) => {- let urlObj: URL;- try {- urlObj = new URL(url);- if (urlObj.protocol !== "siyuan:") {- return false;- }- } catch (error) {- return false;- }- if (urlObj && urlObj.hostname === "plugins") {- const pluginNameType = urlObj.pathname.split("/")[1];- if (!pluginNameType) {- return false;- }- app.plugins.find(plugin => {- if (pluginNameType.startsWith(plugin.name)) {- // siyuan://plugins/plugin-name/foo?bar=baz- plugin.eventBus.emit("open-siyuan-url-plugin", {url});+import type {App} from "../index";- /// #if !MOBILE- // https://github.com/siyuan-note/siyuan/pull/9256- if (pluginNameType.split("/")[0] !== plugin.name) {- // siyuan://plugins/plugin-samplecustom_tab?title=自定义页签&icon=iconFace&data={"text": "This is the custom plugin tab I opened via protocol."}- let data = urlObj.searchParams.get("data");- try {- data = JSON.parse(data || "{}");- } catch (e) {- console.log("Error open plugin tab with protocol:", e);- }- openFile({- app,- custom: {- title: urlObj.searchParams.get("title"),- icon: urlObj.searchParams.get("icon"),- data,- id: pluginNameType- },- });- }- /// #endif- return true;- }- });- return true;- }- if (urlObj && isSYProtocol(url)) {- const id = getIdFromSYProtocol(url);- const focus = urlObj.searchParams.get("focus") === "1";- window.siyuan.editorIsFullscreen = urlObj.searchParams.get("fullscreen") === "1";- fetchPost("/api/block/checkBlockExist", {id}, existResponse => {+export const processSiYuanUriBlocks = (app: App, uriObj: URL): boolean => {+ const blockInfo = parseSiYuanUriBlockInfo(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@@ -84,7 +40,7 @@ export const processSYLink = (app: App, url: string) => {}app.plugins.forEach(plugin => {plugin.eventBus.emit("open-siyuan-url-block", {- url,+ url: uriObj.href,id,focus,exist: existResponse.data,@@ -96,6 +52,75 @@ export const processSYLink = (app: App, url: string) => {return false;};+export 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);let pdfParams;@@ -164,3 +189,36 @@ export const openLink = (protyle: IProtyle, aLink: string, event?: MouseEvent, c}/// #endif};++export const openByMobile = (uri: string) => {+ if (!uri) {+ return;+ }+ /// #if MOBILE+ if (processSiYuanUri(window.siyuan.ws.app, uri)) {+ return;+ }+ /// #endif+ if (isInIOS()) {+ if (uri.startsWith("assets/")) {+ // iOS 16.7 之前的版本,uri 需要 encodeURIComponent+ window.webkit.messageHandlers.openLink.postMessage(location.origin + "/assets/" + encodeURIComponent(uri.replace("assets/", "")));+ } else if (uri.startsWith("/")) {+ // 导出 zip 返回的是已经 encode 过的,因此不能再 encode+ window.webkit.messageHandlers.openLink.postMessage(location.origin + uri);+ } else {+ try {+ new URL(uri);+ window.webkit.messageHandlers.openLink.postMessage(uri);+ } catch (e) {+ window.webkit.messageHandlers.openLink.postMessage("https://" + uri);+ }+ }+ } else if (isInAndroid()) {+ window.JSAndroid.openExternal(uri);+ } else if (isInHarmony()) {+ window.JSHarmony.openExternal(uri);+ } else {+ window.open(uri);+ }+};diff --git a/app/src/menus/commonMenuItem.ts b/app/src/menus/commonMenuItem.tsindex 5eae62c02f..c4175b7f43 100644--- a/app/src/menus/commonMenuItem.ts+++ b/app/src/menus/commonMenuItem.ts@@ -11,10 +11,10 @@ import {isInHarmony,isInIOS,isInMobileApp,- openByMobile,saveExportFile,writeText} from "../protyle/util/compatibility";+import {openByMobile} from "../editor/openLink";import {fetchPost, fetchSyncPost} from "../util/fetch";import {hideMessage, showMessage} from "../dialog/message";import {Dialog} from "../dialog";diff --git a/app/src/plugin/platformUtils.ts b/app/src/plugin/platformUtils.tsindex f0d46ba5bf..6231528315 100644--- a/app/src/plugin/platformUtils.ts+++ b/app/src/plugin/platformUtils.ts@@ -3,7 +3,7 @@ import * as compatibility from "../protyle/util/compatibility";import {ipcRenderer} from "electron";import {Constants} from "../constants";/// #endif-export const openByMobile = compatibility.openByMobile;+export {openByMobile} from "../editor/openLink";export const readText = compatibility.readText;export const writeText = compatibility.writeText;export const copyPlainText = compatibility.copyPlainText;diff --git a/app/src/protyle/preview/index.ts b/app/src/protyle/preview/index.tsindex acef69d0c5..048f09de55 100644--- a/app/src/protyle/preview/index.ts+++ b/app/src/protyle/preview/index.ts@@ -1,5 +1,6 @@-import {isOnlyMeta, openByMobile, writeText} from "../util/compatibility";+import {isOnlyMeta, writeText} from "../util/compatibility";import {focusByRange} from "../util/selection";+import {openByMobile} from "../../editor/openLink";import {showMessage} from "../../dialog/message";import {isLocalPath, pathPosix} from "../../util/pathName";import {previewDocImage} from "./image";diff --git a/app/src/protyle/util/compatibility.ts b/app/src/protyle/util/compatibility.tsindex aa514ece6f..7e0f1982ad 100644--- a/app/src/protyle/util/compatibility.ts+++ b/app/src/protyle/util/compatibility.ts@@ -4,9 +4,6 @@ import {Constants} from "../../constants";/// #if !BROWSERimport {ipcRenderer} from "electron";/// #endif-/// #if MOBILE-import {processSYLink} from "../../editor/openLink";-/// #endifimport {getDefaultSubType, getDefaultType} from "../../search/getDefault";import {hideMessage, showMessage} from "../../dialog/message";@@ -66,39 +63,6 @@ export const getTextSiyuanFromTextHTML = (html: string) => {};};-export const openByMobile = (uri: string) => {- if (!uri) {- return;- }- /// #if MOBILE- if (processSYLink(window.siyuan.ws.app, uri)) {- return;- }- /// #endif- if (isInIOS()) {- if (uri.startsWith("assets/")) {- // iOS 16.7 之前的版本,uri 需要 encodeURIComponent- window.webkit.messageHandlers.openLink.postMessage(location.origin + "/assets/" + encodeURIComponent(uri.replace("assets/", "")));- } else if (uri.startsWith("/")) {- // 导出 zip 返回的是已经 encode 过的,因此不能再 encode- window.webkit.messageHandlers.openLink.postMessage(location.origin + uri);- } else {- try {- new URL(uri);- window.webkit.messageHandlers.openLink.postMessage(uri);- } catch (e) {- window.webkit.messageHandlers.openLink.postMessage("https://" + uri);- }- }- } else if (isInAndroid()) {- window.JSAndroid.openExternal(uri);- } else if (isInHarmony()) {- window.JSHarmony.openExternal(uri);- } else {- window.open(uri);- }-};-export const saveExportFile = async (uri: string, msgId?: string) => {if (!uri) {return;@@ -701,4 +665,3 @@ export const initNativeDialogOverride = () => {};};/// #endif-diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.tsindex 9b66ad6953..cbaf70c392 100644--- a/app/src/types/index.d.ts+++ b/app/src/types/index.d.ts@@ -1288,8 +1288,6 @@ interface IKernelPluginRpcError {data?: any;}--interface IKernelPluginRpc {/*** 通过 {@link Proxy} 实现的动态方法调用,插件开发者可以直接调用 `call.方法名(params)` 来调用内核插件暴露的方法,无需关心 JSON-RPC 的细节@@ -1316,3 +1314,27 @@ interface IKernelPluginRpc {*/unbind: (method: TJsonRpcMethod, handler: TJsonRpcHandler<void>) => void;}++/**+ * SiYuan URI 块信息接口,用于描述通过 SiYuan URI 协议传递的块信息+ */+interface ISiYuanUriBlockInfo {+ /**+ * 块 ID+ */+ id: string;++ /**+ * 是否聚焦该块+ *+ * @defaultValue false+ */+ focus: boolean;++ /**+ * 是否全屏显示该块+ *+ * @defaultValue false+ */+ fullscreen: boolean;+}diff --git a/app/src/util/pathName.ts b/app/src/util/pathName.tsindex fb3cfebdae..8ca5e09162 100644--- a/app/src/util/pathName.ts+++ b/app/src/util/pathName.ts@@ -2,7 +2,7 @@ import * as path from "path";import {fetchPost} from "./fetch";import {Dialog} from "../dialog";import {escapeHtml} from "./escape";-import {getSearch, isMobile} from "./functions";+import {isMobile} from "./functions";import {focusByRange} from "../protyle/util/selection";import {unicode2Emoji} from "../emoji";import {Constants} from "../constants";@@ -26,30 +26,81 @@ export const useShell = (cmd: "showItemInFolder" | "openPath", filePath: string)/// #endif};+/**+ * Check if the given URI is a valid SiYuan URI protocol (siyuan:// or web+siyuan://)+ * @param uri - the URI to check+ */+export const isSiYuanUriProtocol = (uri: URL | string | null | undefined): boolean => {+ try {+ if (uri == null) return false;++ const uriObj = uri instanceof URL ? uri : new URL(uri);+ if (uriObj.protocol === "siyuan:" || uriObj.protocol === "web+siyuan:") {+ return true;+ }+ return false;+ } catch (error) {+ return false;+ }+};++/**+ * Parse siyuan://blocks/20221031001313-rk7sd0e?focus=1&fullscreen=1+ * @param uri - the siyuan block uri to parse+ * @returns the block id and other info, or null if the uri is not a valid siyuan block uri+ */+export const parseSiYuanUriBlockInfo = (uri: URL | string | null | undefined): ISiYuanUriBlockInfo | null => {+ try {+ if (uri == null) return null;++ const uriObj = uri instanceof URL ? uri : new URL(uri);+ if (!isSiYuanUriProtocol(uriObj)) {+ return null;+ }+ if (uriObj.hostname === "blocks" && /^\/\d{14}-\w{7}/.test(uriObj.pathname)) {+ return {+ id: uriObj.pathname.substring(1, 1 + 22),+ focus: uriObj.searchParams.get("focus") === "1",+ fullscreen: uriObj.searchParams.get("fullscreen") === "1",+ };+ }+ return null;+ } catch (error) {+ return null;+ }+};+export const getIdZoomInByPath = () => {const searchParams = new URLSearchParams(window.location.search);- const PWAURL = searchParams.get("url");const data = {id: "",isZoomIn: false,};- if (/^web\+siyuan:\/\/blocks\/\d{14}-\w{7}/.test(PWAURL)) {- // PWA 捕获 web+siyuan://blocks/20221031001313-rk7sd0e?focus=1- data.id = PWAURL.substring(20, 20 + 22);- data.isZoomIn = getSearch("focus", PWAURL) === "1";- window.siyuan.editorIsFullscreen = getSearch("fullscreen", PWAURL) === "1";- } else if (window.JSAndroid) {- // PAD 通过思源协议打开- const SYURL = window.JSAndroid.getBlockURL();- data.id = getIdFromSYProtocol(SYURL);- data.isZoomIn = getSearch("focus", SYURL) === "1";- window.siyuan.editorIsFullscreen = getSearch("fullscreen", SYURL) === "1";- } else {- // 支持通过 URL 查询字符串参数 `id` 和 `focus` 跳转到 Web 端指定块 https://github.com/siyuan-note/siyuan/pull/7086- data.id = searchParams.get("id");- data.isZoomIn = searchParams.get("focus") === "1";- window.siyuan.editorIsFullscreen = searchParams.get("fullscreen") === "1";++ if (searchParams.has("url")) {+ const dataInfo = parseSiYuanUriBlockInfo(searchParams.get("url"));+ if (dataInfo != null) {+ data.id = dataInfo.id;+ data.isZoomIn = dataInfo.focus;+ window.siyuan.editorIsFullscreen = dataInfo.fullscreen;+ return data;+ }+ }++ if (window.JSAndroid) {+ const dataInfo = parseSiYuanUriBlockInfo(window.JSAndroid.getBlockURL());+ if (dataInfo != null) {+ data.id = dataInfo.id;+ data.isZoomIn = dataInfo.focus;+ window.siyuan.editorIsFullscreen = dataInfo.fullscreen;+ return data;+ }}++ // 支持通过 URL 查询字符串参数 `id` 和 `focus` 跳转到 Web 端指定块 https://github.com/siyuan-note/siyuan/pull/7086+ data.id = searchParams.get("id") ?? "";+ data.isZoomIn = searchParams.get("focus") === "1";+ window.siyuan.editorIsFullscreen = searchParams.get("fullscreen") === "1";return data;};