fix: Invalid URL at deeplink handler at startup

fixes #613
This commit is contained in:
lin onetwo 2025-02-12 00:36:32 +08:00
parent f1ac503a96
commit 105b87f981

View file

@ -18,30 +18,34 @@ export class DeepLinkService implements IDeepLinkService {
*/ */
private readonly deepLinkHandler: (requestUrl: string) => Promise<void> = async (requestUrl) => { private readonly deepLinkHandler: (requestUrl: string) => Promise<void> = async (requestUrl) => {
logger.info(`Receiving deep link`, { requestUrl, function: 'deepLinkHandler' }); logger.info(`Receiving deep link`, { requestUrl, function: 'deepLinkHandler' });
// hostname is workspace id or name try {
const { hostname, hash, pathname } = new URL(requestUrl); // hostname is workspace id or name
let workspace = await this.workspaceService.get(hostname); const { hostname, hash, pathname } = new URL(requestUrl);
if (workspace === undefined) { let workspace = await this.workspaceService.get(hostname);
logger.info(`Workspace not found, try get by name`, { hostname, function: 'deepLinkHandler' });
let workspaceName = hostname;
// Host name can't use Chinese or it becomes `xn--1-376ap73a`, so use `w` host, and get workspace name from path
if (hostname === 'w') {
workspaceName = decodeURIComponent(pathname.split('/')[1] ?? '');
logger.info(`Workspace name from w/`, { hostname, pathname, workspaceName, function: 'deepLinkHandler' });
}
workspace = await this.workspaceService.getByWikiName(workspaceName);
if (workspace === undefined) { if (workspace === undefined) {
return; logger.info(`Workspace not found, try get by name`, { hostname, function: 'deepLinkHandler' });
let workspaceName = hostname;
// Host name can't use Chinese or it becomes `xn--1-376ap73a`, so use `w` host, and get workspace name from path
if (hostname === 'w') {
workspaceName = decodeURIComponent(pathname.split('/')[1] ?? '');
logger.info(`Workspace name from w/`, { hostname, pathname, workspaceName, function: 'deepLinkHandler' });
}
workspace = await this.workspaceService.getByWikiName(workspaceName);
if (workspace === undefined) {
return;
}
} }
let tiddlerName = hash.substring(1); // remove '#:'
if (tiddlerName.includes(':')) {
tiddlerName = tiddlerName.split(':')[1];
}
// Support CJK
tiddlerName = decodeURIComponent(tiddlerName);
logger.info(`Open deep link`, { workspaceId: workspace.id, tiddlerName, function: 'deepLinkHandler' });
await this.workspaceService.openWorkspaceTiddler(workspace, tiddlerName);
} catch (error) {
logger.error(`Invalid URL`, { requestUrl, error, function: 'deepLinkHandler' });
} }
let tiddlerName = hash.substring(1); // remove '#:'
if (tiddlerName.includes(':')) {
tiddlerName = tiddlerName.split(':')[1];
}
// Support CJK
tiddlerName = decodeURIComponent(tiddlerName);
logger.info(`Open deep link`, { workspaceId: workspace.id, tiddlerName, function: 'deepLinkHandler' });
await this.workspaceService.openWorkspaceTiddler(workspace, tiddlerName);
}; };
public initializeDeepLink(protocol: string) { public initializeDeepLink(protocol: string) {