1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-30 10:36:35 +02:00
joplin/packages/lib/callbackUrlUtils.ts

38 lines
1.0 KiB
TypeScript

const URL = require('url-parse');
export function isCallbackUrl(s: string) {
return s.startsWith('joplin://x-callback-url/');
}
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 {
OpenNote = 'openNote',
OpenFolder = 'openFolder',
OpenTag = 'openTag',
}
export interface CallbackUrlInfo {
command: CallbackUrlCommand;
params: Record<string, string>;
}
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,
};
}