| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-22 19:34:48 +01:00
										 |  |  | const attributeService = require('../../services/attributes.js'); | 
					
						
							|  |  |  | const cloneService = require('../../services/cloning.js'); | 
					
						
							|  |  |  | const noteService = require('../../services/notes.js'); | 
					
						
							|  |  |  | const dateNoteService = require('../../services/date_notes.js'); | 
					
						
							| 
									
										
										
										
											2024-02-16 21:38:09 +02:00
										 |  |  | const dateUtils = require('../../services/date_utils'); | 
					
						
							| 
									
										
										
										
											2023-11-22 19:34:48 +01:00
										 |  |  | const imageService = require('../../services/image.js'); | 
					
						
							|  |  |  | const appInfo = require('../../services/app_info.js'); | 
					
						
							| 
									
										
										
										
											2024-02-17 11:54:55 +02:00
										 |  |  | const ws = require('../../services/ws'); | 
					
						
							| 
									
										
										
										
											2024-02-16 21:17:33 +02:00
										 |  |  | const log = require('../../services/log'); | 
					
						
							| 
									
										
										
										
											2024-02-16 21:38:09 +02:00
										 |  |  | const utils = require('../../services/utils'); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2023-11-22 19:34:48 +01:00
										 |  |  | const htmlSanitizer = require('../../services/html_sanitizer.js'); | 
					
						
							|  |  |  | const {formatAttrForSearch} = require('../../services/attribute_formatter.js'); | 
					
						
							| 
									
										
										
										
											2023-07-02 12:52:16 +02:00
										 |  |  | const jsdom = require("jsdom"); | 
					
						
							|  |  |  | const { JSDOM } = jsdom; | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function addClipping(req) { | 
					
						
							| 
									
										
										
										
											2023-08-09 23:00:42 +02:00
										 |  |  |     // if a note under the clipperInbox has the same 'pageUrl' attribute,
 | 
					
						
							| 
									
										
										
										
											2023-07-09 22:58:34 +02:00
										 |  |  |     // add the content to that note and clone it under today's inbox
 | 
					
						
							|  |  |  |     // otherwise just create a new note under today's inbox
 | 
					
						
							| 
									
										
										
										
											2022-07-06 23:09:16 +02:00
										 |  |  |     let {title, content, pageUrl, images} = req.body; | 
					
						
							| 
									
										
										
										
											2023-07-02 12:51:23 +02:00
										 |  |  |     const clipType = 'clippings'; | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const clipperInbox = getClipperInboxNote(); | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-03 20:44:31 +02:00
										 |  |  |     pageUrl = htmlSanitizer.sanitizeUrl(pageUrl); | 
					
						
							| 
									
										
										
										
											2023-07-02 12:51:23 +02:00
										 |  |  |     let clippingNote = findClippingNote(clipperInbox, pageUrl, clipType); | 
					
						
							| 
									
										
										
										
											2023-07-09 22:58:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     if (!clippingNote) { | 
					
						
							| 
									
										
										
										
											2021-01-14 21:52:44 +01:00
										 |  |  |         clippingNote = noteService.createNewNote({ | 
					
						
							| 
									
										
										
										
											2023-08-09 23:00:42 +02:00
										 |  |  |             parentNoteId: clipperInbox.noteId, | 
					
						
							| 
									
										
										
										
											2019-11-16 11:09:52 +01:00
										 |  |  |             title: title, | 
					
						
							| 
									
										
										
										
											2019-12-08 09:41:31 +01:00
										 |  |  |             content: '', | 
					
						
							|  |  |  |             type: 'text' | 
					
						
							| 
									
										
										
										
											2021-01-14 21:52:44 +01:00
										 |  |  |         }).note; | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         clippingNote.setLabel('clipType', 'clippings'); | 
					
						
							|  |  |  |         clippingNote.setLabel('pageUrl', pageUrl); | 
					
						
							| 
									
										
										
										
											2021-10-20 02:57:05 -04:00
										 |  |  |         clippingNote.setLabel('iconClass', 'bx bx-globe'); | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-16 20:32:20 +02:00
										 |  |  |     const rewrittenContent = processContent(images, clippingNote, content); | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const existingContent = clippingNote.getContent(); | 
					
						
							| 
									
										
										
										
											2020-04-05 15:35:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 12:52:16 +02:00
										 |  |  |     clippingNote.setContent(`${existingContent}${existingContent.trim() ? "<br>" : ""}${rewrittenContent}`); | 
					
						
							| 
									
										
										
										
											2023-07-09 22:58:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-09 23:00:42 +02:00
										 |  |  |     if (clippingNote.parentNoteId !== clipperInbox.noteId) { | 
					
						
							|  |  |  |         cloneService.cloneNoteToParentNote(clippingNote.noteId, clipperInbox.noteId); | 
					
						
							| 
									
										
										
										
											2023-06-21 13:09:49 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-07-09 22:58:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     return { | 
					
						
							|  |  |  |         noteId: clippingNote.noteId | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-09 22:58:34 +02:00
										 |  |  | function findClippingNote(clipperInboxNote, pageUrl, clipType) { | 
					
						
							|  |  |  |     if (!pageUrl) { | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const notes = clipperInboxNote.searchNotesInSubtree( | 
					
						
							|  |  |  |         formatAttrForSearch({ | 
					
						
							|  |  |  |             type: 'label', | 
					
						
							|  |  |  |             name: "pageUrl", | 
					
						
							|  |  |  |             value: pageUrl | 
					
						
							|  |  |  |         }, true) | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return clipType | 
					
						
							|  |  |  |         ? notes.find(note => note.getOwnedLabelValue('clipType') === clipType) | 
					
						
							|  |  |  |         : notes[0]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getClipperInboxNote() { | 
					
						
							|  |  |  |     let clipperInbox = attributeService.getNoteWithLabel('clipperInbox'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!clipperInbox) { | 
					
						
							| 
									
										
										
										
											2023-08-09 23:00:42 +02:00
										 |  |  |         clipperInbox = dateNoteService.getDayNote(dateUtils.localNowDate()); | 
					
						
							| 
									
										
										
										
											2023-07-09 22:58:34 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return clipperInbox; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function createNote(req) { | 
					
						
							| 
									
										
										
										
											2023-03-06 21:21:09 +08:00
										 |  |  |     let {title, content, pageUrl, images, clipType, labels} = req.body; | 
					
						
							| 
									
										
										
										
											2020-04-08 11:07:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-11 23:05:28 +02:00
										 |  |  |     if (!title || !title.trim()) { | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |         title = `Clipped note from ${pageUrl}`; | 
					
						
							| 
									
										
										
										
											2020-07-11 23:05:28 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-01-14 21:52:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 12:51:23 +02:00
										 |  |  |     clipType = htmlSanitizer.sanitize(clipType); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const clipperInbox = getClipperInboxNote(); | 
					
						
							| 
									
										
										
										
											2023-06-21 16:10:06 +02:00
										 |  |  |     pageUrl = htmlSanitizer.sanitizeUrl(pageUrl); | 
					
						
							| 
									
										
										
										
											2023-07-02 12:51:23 +02:00
										 |  |  |     let note = findClippingNote(clipperInbox, pageUrl, clipType); | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-09 22:58:34 +02:00
										 |  |  |     if (!note) { | 
					
						
							| 
									
										
										
										
											2023-06-21 16:10:06 +02:00
										 |  |  |         note = noteService.createNewNote({ | 
					
						
							| 
									
										
										
										
											2023-09-05 23:24:01 +02:00
										 |  |  |             parentNoteId: clipperInbox.noteId, | 
					
						
							| 
									
										
										
										
											2023-06-21 16:10:06 +02:00
										 |  |  |             title, | 
					
						
							| 
									
										
										
										
											2023-07-02 12:51:23 +02:00
										 |  |  |             content: '', | 
					
						
							| 
									
										
										
										
											2023-06-21 16:10:06 +02:00
										 |  |  |             type: 'text' | 
					
						
							|  |  |  |         }).note; | 
					
						
							| 
									
										
										
										
											2022-07-06 23:09:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 16:10:06 +02:00
										 |  |  |         note.setLabel('clipType', clipType); | 
					
						
							| 
									
										
										
										
											2023-07-09 22:58:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 16:10:06 +02:00
										 |  |  |         if (pageUrl) { | 
					
						
							|  |  |  |             pageUrl = htmlSanitizer.sanitizeUrl(pageUrl); | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 16:10:06 +02:00
										 |  |  |             note.setLabel('pageUrl', pageUrl); | 
					
						
							|  |  |  |             note.setLabel('iconClass', 'bx bx-globe'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-06-21 16:10:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-06 21:21:09 +08:00
										 |  |  |     if (labels) { | 
					
						
							|  |  |  |         for (const labelName in labels) { | 
					
						
							| 
									
										
										
										
											2023-03-06 21:28:09 +08:00
										 |  |  |             const labelValue = htmlSanitizer.sanitize(labels[labelName]); | 
					
						
							|  |  |  |             note.setLabel(labelName, labelValue); | 
					
						
							| 
									
										
										
										
											2023-03-06 21:21:09 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 16:10:06 +02:00
										 |  |  |     const existingContent = note.getContent(); | 
					
						
							| 
									
										
										
										
											2020-09-16 20:32:20 +02:00
										 |  |  |     const rewrittenContent = processContent(images, note, content); | 
					
						
							| 
									
										
										
										
											2023-08-08 23:42:47 +02:00
										 |  |  |     const newContent = `${existingContent}${existingContent.trim() ? "<br/>" : ""}${rewrittenContent}`; | 
					
						
							|  |  |  |     note.setContent(newContent); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     noteService.asyncPostProcessContent(note, newContent); // to mark attachments as used
 | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         noteId: note.noteId | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-16 20:32:20 +02:00
										 |  |  | function processContent(images, note, content) { | 
					
						
							| 
									
										
										
										
											2021-10-03 11:56:08 -07:00
										 |  |  |     let rewrittenContent = htmlSanitizer.sanitize(content); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |     if (images) { | 
					
						
							|  |  |  |         for (const {src, dataUrl, imageId} of images) { | 
					
						
							|  |  |  |             const filename = path.basename(src); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-12 20:39:42 +01:00
										 |  |  |             if (!dataUrl || !dataUrl.startsWith("data:image")) { | 
					
						
							| 
									
										
										
										
											2021-04-12 23:29:02 +02:00
										 |  |  |                 const excerpt = dataUrl | 
					
						
							|  |  |  |                     ? dataUrl.substr(0, Math.min(100, dataUrl.length)) | 
					
						
							|  |  |  |                     : "null"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |                 log.info(`Image could not be recognized as data URL: ${excerpt}`); | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |             const buffer = Buffer.from(dataUrl.split(",")[1], 'base64'); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-08 23:07:59 +02:00
										 |  |  |             const attachment = imageService.saveImageToAttachment(note.noteId, buffer, filename, true); | 
					
						
							| 
									
										
										
										
											2023-11-27 10:10:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-27 10:15:29 +01:00
										 |  |  |             const encodedTitle = encodeURIComponent(attachment.title); | 
					
						
							|  |  |  |             const url = `api/attachments/${attachment.attachmentId}/image/${encodedTitle}`; | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-19 23:36:21 +02:00
										 |  |  |             log.info(`Replacing '${imageId}' with '${url}' in note '${note.noteId}'`); | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 22:55:11 +02:00
										 |  |  |             rewrittenContent = utils.replaceAll(rewrittenContent, imageId, url); | 
					
						
							| 
									
										
										
										
											2019-07-06 16:48:06 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-23 11:25:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-14 20:50:50 +01:00
										 |  |  |     // fallback if parsing/downloading images fails for some reason on the extension side (
 | 
					
						
							|  |  |  |     rewrittenContent = noteService.downloadImages(note.noteId, rewrittenContent); | 
					
						
							| 
									
										
										
										
											2023-07-02 12:52:16 +02:00
										 |  |  |     // Check if rewrittenContent contains at least one HTML tag
 | 
					
						
							|  |  |  |     if (!/<.+?>/.test(rewrittenContent)) { | 
					
						
							| 
									
										
										
										
											2023-07-09 22:58:34 +02:00
										 |  |  |         rewrittenContent = `<p>${rewrittenContent}</p>`; | 
					
						
							| 
									
										
										
										
											2023-07-02 12:52:16 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     // Create a JSDOM object from the existing HTML content
 | 
					
						
							| 
									
										
										
										
											2023-07-09 22:58:34 +02:00
										 |  |  |     const dom = new JSDOM(rewrittenContent); | 
					
						
							| 
									
										
										
										
											2023-07-02 12:52:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Get the content inside the body tag and serialize it
 | 
					
						
							|  |  |  |     rewrittenContent = dom.window.document.body.innerHTML; | 
					
						
							| 
									
										
										
										
											2022-02-14 20:50:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     return rewrittenContent; | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function openNote(req) { | 
					
						
							| 
									
										
										
										
											2019-07-07 13:12:40 +02:00
										 |  |  |     if (utils.isElectron()) { | 
					
						
							| 
									
										
										
										
											2019-08-26 20:21:43 +02:00
										 |  |  |         ws.sendMessageToAllClients({ | 
					
						
							| 
									
										
										
										
											2021-04-24 11:39:44 +02:00
										 |  |  |             type: 'openNote', | 
					
						
							| 
									
										
										
										
											2019-07-07 13:12:40 +02:00
										 |  |  |             noteId: req.params.noteId | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             result: 'ok' | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             result: 'open-in-browser' | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function handshake() { | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     return { | 
					
						
							|  |  |  |         appName: "trilium", | 
					
						
							| 
									
										
										
										
											2019-07-07 22:27:06 +02:00
										 |  |  |         protocolVersion: appInfo.clipperProtocolVersion | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 13:09:49 +02:00
										 |  |  | function findNotesByUrl(req){ | 
					
						
							|  |  |  |     let pageUrl = req.params.noteUrl; | 
					
						
							|  |  |  |     const clipperInbox = getClipperInboxNote(); | 
					
						
							| 
									
										
										
										
											2023-07-02 12:51:23 +02:00
										 |  |  |     let foundPage = findClippingNote(clipperInbox, pageUrl, null); | 
					
						
							| 
									
										
										
										
											2023-06-21 13:09:49 +02:00
										 |  |  |     return { | 
					
						
							|  |  |  |         noteId: foundPage ? foundPage.noteId : null | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-22 19:49:48 +02:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |     createNote, | 
					
						
							| 
									
										
										
										
											2019-07-06 23:54:48 +02:00
										 |  |  |     addClipping, | 
					
						
							| 
									
										
										
										
											2019-06-23 13:25:00 +02:00
										 |  |  |     openNote, | 
					
						
							| 
									
										
										
										
											2023-06-21 13:09:49 +02:00
										 |  |  |     handshake, | 
					
						
							|  |  |  |     findNotesByUrl | 
					
						
							| 
									
										
										
										
											2020-05-13 23:06:13 +02:00
										 |  |  | }; |