-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
urlUtils.js
32 lines (23 loc) · 826 Bytes
/
urlUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const urlUtils = {};
const resourceRegex = /^(joplin:\/\/|:\/)([0-9a-zA-Z]{32})(|#[^\s]*)(|\s".*?")$/;
urlUtils.urlDecode = function(string) {
return decodeURIComponent((`${string}`).replace(/\+/g, '%20'));
};
urlUtils.isResourceUrl = function(url) {
return !!url.match(resourceRegex);
};
urlUtils.parseResourceUrl = function(url) {
if (!urlUtils.isResourceUrl(url)) return null;
const match = url.match(resourceRegex);
const itemId = match[2];
let hash = match[3].trim();
// In general we want the hash to be decoded so that non-alphabetical languages
// appear as-is without being encoded with %.
// Fixes https://github.com/laurent22/joplin/issues/1870
if (hash) hash = urlUtils.urlDecode(hash.substr(1)); // Remove the first #
return {
itemId: itemId,
hash: hash,
};
};
module.exports = urlUtils;