fixes for web clipper

This commit is contained in:
zadam 2020-04-02 22:55:11 +02:00
parent bab657e43d
commit 3bbb213f82
5 changed files with 27 additions and 9 deletions

View File

@ -77,7 +77,7 @@
"yazl": "^2.5.1"
},
"devDependencies": {
"electron": "9.0.0-beta.12",
"electron": "9.0.0-beta.13",
"electron-builder": "22.4.1",
"electron-packager": "14.2.1",
"electron-rebuild": "1.10.1",

View File

@ -559,7 +559,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
if (activeNotePath) {
let node = await this.expandToNote(activeNotePath);
if (node.data.noteId !== activeNoteId) {
if (node && node.data.noteId !== activeNoteId) {
// if the active note has been moved elsewhere then it won't be found by the path
// so we switch to the alternative of trying to find it by noteId
const notesById = this.getNodesByNoteId(activeNoteId);

View File

@ -114,7 +114,7 @@ async function addImagesToNote(images, note, content) {
console.log(`Replacing ${imageId} with ${url}`);
rewrittenContent = rewrittenContent.replace(imageId, url);
rewrittenContent = utils.replaceAll(rewrittenContent, imageId, url);
}
}

View File

@ -282,7 +282,7 @@ async function downloadImage(noteId, imageUrl) {
log.info(`Download of ${imageUrl} succeeded and was saved as image note ${note.noteId}`);
}
catch (e) {
log.error(`Downoad of ${imageUrl} for note ${noteId} failed with error: ${e.message} ${e.stack}`);
log.error(`Download of ${imageUrl} for note ${noteId} failed with error: ${e.message} ${e.stack}`);
}
}
@ -296,13 +296,17 @@ function replaceUrl(content, url, imageNote) {
}
async function downloadImages(noteId, content) {
const re = /<img.*?\ssrc=['"]([^'">]+)['"]/ig;
const re = /<img[^>]*?\ssrc=['"]([^'">]+)['"]/ig;
let match;
while (match = re.exec(content)) {
const url = match[1];
const origContent = content;
if (!url.startsWith('api/images/')) {
while (match = re.exec(origContent)) {
const url = match[1].toLowerCase();
if (!url.startsWith('api/images/')
// this is and exception for the web clipper's "imageId"
&& (url.length !== 20 || url.startsWith('http'))) {
if (url in downloadImagePromises) {
// download is already in progress
continue;
@ -386,12 +390,19 @@ async function saveLinks(note, content) {
const foundLinks = [];
if (note.type === 'text') {
console.time("LINKS");
content = findImageLinks(content, foundLinks);
content = findInternalLinks(content, foundLinks);
content = findExternalLinks(content, foundLinks);
content = findIncludeNoteLinks(content, foundLinks);
console.timeEnd("LINKS");
console.time("IMAGES");
content = await downloadImages(note.noteId, content);
console.timeEnd("IMAGES");
}
else if (note.type === 'relation-map') {
findRelationMapLinks(content, foundLinks);

View File

@ -166,6 +166,12 @@ function isStringNote(type, mime) {
|| STRING_MIME_TYPES.includes(mime);
}
function replaceAll(string, replaceWhat, replaceWith) {
const escapedWhat = replaceWhat.replace(/([\/,!\\^${}\[\]().*+?|<>\-&])/g, "\\$&");
return string.replace(new RegExp(escapedWhat, "g"), replaceWith);
}
module.exports = {
randomSecureToken,
randomString,
@ -191,5 +197,6 @@ module.exports = {
crash,
sanitizeFilenameForHeader,
getContentDisposition,
isStringNote
isStringNote,
replaceAll
};