1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-12-02 22:49:09 +02:00

Desktop, CLI: Fixed import of notes that contain links with hashes

This commit is contained in:
Laurent Cozic
2019-09-12 22:48:10 +01:00
parent 0379523eaf
commit 88561a6c3c
3 changed files with 68 additions and 33 deletions

View File

@@ -40,27 +40,53 @@ urlUtils.prependBaseUrl = function(url, baseUrl) {
}
};
const resourceRegex = /^(joplin:\/\/|:\/)([0-9a-zA-Z]{32})(|#[^\s]*)(|\s".*?")$/;
urlUtils.isResourceUrl = function(url) {
return !!url.match(/^(joplin:\/\/|:\/)[0-9a-zA-Z]{32}(|#.*)$/);
return !!url.match(resourceRegex);
};
urlUtils.parseResourceUrl = function(url) {
if (!urlUtils.isResourceUrl(url)) return null;
const filename = url.split('/').pop();
const splitted = filename.split('#');
const match = url.match(resourceRegex);
const output = {
itemId: '',
hash: '',
};
if (splitted.length) output.itemId = splitted[0];
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 (splitted.length >= 2) output.hash = urlDecode(splitted[1]);
if (hash) hash = urlDecode(hash.substr(1)); // Remove the first #
return {
itemId: itemId,
hash: hash,
};
};
urlUtils.extractResourceUrls = function(text) {
const markdownLinksRE = /\]\((.*?)\)/g;
const output = [];
let result = null;
while ((result = markdownLinksRE.exec(text)) !== null) {
const resourceUrlInfo = urlUtils.parseResourceUrl(result[1]);
if (resourceUrlInfo) output.push(resourceUrlInfo);
}
const htmlRegexes = [
/<img[\s\S]*?src=["']:\/([a-zA-Z0-9]{32})["'][\s\S]*?>/gi,
/<a[\s\S]*?href=["']:\/([a-zA-Z0-9]{32})["'][\s\S]*?>/gi,
];
for (const htmlRegex of htmlRegexes) {
while (true) {
const m = htmlRegex.exec(text);
if (!m) break;
output.push({ itemId: m[1], hash: '' });
}
}
return output;
};