2021-10-16 11:07:41 +02:00
|
|
|
const URL = require('url-parse');
|
|
|
|
|
|
|
|
export function isCallbackUrl(s: string) {
|
2023-06-14 16:51:35 +02:00
|
|
|
return s.startsWith('joplin://x-callback-url/openNote?') ||
|
|
|
|
s.startsWith('joplin://x-callback-url/openFolder?') ||
|
|
|
|
s.startsWith('joplin://x-callback-url/openTag?');
|
2021-10-16 11:07:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getNoteCallbackUrl(noteId: string) {
|
|
|
|
return `joplin://x-callback-url/openNote?id=${encodeURIComponent(noteId)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getFolderCallbackUrl(folderId: string) {
|
|
|
|
return `joplin://x-callback-url/openFolder?id=${encodeURIComponent(folderId)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getTagCallbackUrl(tagId: string) {
|
|
|
|
return `joplin://x-callback-url/openTag?id=${encodeURIComponent(tagId)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const enum CallbackUrlCommand {
|
2023-07-06 20:17:41 +02:00
|
|
|
OpenNote = 'openNote',
|
|
|
|
OpenFolder = 'openFolder',
|
|
|
|
OpenTag = 'openTag',
|
2021-10-16 11:07:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface CallbackUrlInfo {
|
2023-07-06 20:17:41 +02:00
|
|
|
command: CallbackUrlCommand;
|
|
|
|
params: Record<string, string>;
|
2021-10-16 11:07:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function parseCallbackUrl(s: string): CallbackUrlInfo {
|
|
|
|
if (!isCallbackUrl(s)) throw new Error(`Invalid callback url ${s}`);
|
|
|
|
const url = new URL(s, true);
|
|
|
|
return {
|
|
|
|
command: url.pathname.substring(url.pathname.lastIndexOf('/') + 1) as CallbackUrlCommand,
|
|
|
|
params: url.query,
|
|
|
|
};
|
|
|
|
}
|