diff --git a/src/routes/api/clipper.js b/src/routes/api/clipper.js
index d792ff38a..bcab106ba 100644
--- a/src/routes/api/clipper.js
+++ b/src/routes/api/clipper.js
@@ -1,6 +1,7 @@
"use strict";
const attributeService = require("../../services/attributes");
+const cloneService = require("../../services/cloning");
const noteService = require('../../services/notes');
const dateNoteService = require('../../services/date_notes');
const dateUtils = require('../../services/date_utils');
@@ -13,19 +14,29 @@ const path = require('path');
const BAttribute = require('../../becca/entities/battribute');
const htmlSanitizer = require('../../services/html_sanitizer');
const {formatAttrForSearch} = require("../../services/attribute_formatter");
+const jsdom = require("jsdom");
+const { JSDOM } = jsdom;
-function findClippingNote(clipperInboxNote, pageUrl) {
- const notes = clipperInboxNote.searchNotesInSubtree(
- formatAttrForSearch({
- type: 'label',
- name: "pageUrl",
- value: pageUrl
- }, true)
- );
+function findClippingNote(clipperInboxNote, pageUrl, clipType) {
+ //Avoid searching for empty of browser pages like about:blank
+ if (pageUrl.trim().length > 1 && pageUrl.trim().indexOf('about:') != 0 ){
+ const notes = clipperInboxNote.searchNotesInSubtree(
+ formatAttrForSearch({
+ type: 'label',
+ name: "pageUrl",
+ value: pageUrl
+ }, true)
+ );
- for (const note of notes) {
- if (note.getOwnedLabelValue('clipType') === 'clippings') {
- return note;
+ for (const note of notes) {
+ if (clipType){
+ if (note.getOwnedLabelValue('clipType') === clipType) {
+ return note;
+ }
+ }
+ else{
+ return note;
+ }
}
}
@@ -36,23 +47,29 @@ function getClipperInboxNote() {
let clipperInbox = attributeService.getNoteWithLabel('clipperInbox');
if (!clipperInbox) {
- clipperInbox = dateNoteService.getDayNote(dateUtils.localNowDate());
+ clipperInbox = dateNoteService.getRootCalendarNote();
}
return clipperInbox;
}
function addClipping(req) {
+ //if a note under the clipperInbox as the same 'pageUrl' attribute, add the content to that note
+ //and clone it under today's inbox
+ //otherwise just create a new note under today's inbox
let {title, content, pageUrl, images} = req.body;
+ const clipType = 'clippings';
+ //this is only for reference
const clipperInbox = getClipperInboxNote();
+ const dailyNote = dateNoteService.getDayNote(dateUtils.localNowDate());
pageUrl = htmlSanitizer.sanitizeUrl(pageUrl);
- let clippingNote = findClippingNote(clipperInbox, pageUrl);
-
+ let clippingNote = findClippingNote(clipperInbox, pageUrl, clipType);
+
if (!clippingNote) {
clippingNote = noteService.createNewNote({
- parentNoteId: clipperInbox.noteId,
+ parentNoteId: dailyNote.noteId,
title: title,
content: '',
type: 'text'
@@ -63,12 +80,16 @@ function addClipping(req) {
clippingNote.setLabel('iconClass', 'bx bx-globe');
}
+
const rewrittenContent = processContent(images, clippingNote, content);
const existingContent = clippingNote.getContent();
- clippingNote.setContent(`${existingContent}${existingContent.trim() ? "
" : ""}${rewrittenContent}`);
-
+ clippingNote.setContent(`${existingContent}${existingContent.trim() ? "
" : ""}${rewrittenContent}`);
+
+ if (clippingNote.parentNoteId != dailyNote.noteId){
+ cloneService.cloneNoteToParentNote(clippingNote.noteId, dailyNote.noteId);
+ }
return {
noteId: clippingNote.noteId
};
@@ -81,25 +102,34 @@ function createNote(req) {
title = `Clipped note from ${pageUrl}`;
}
- const clipperInbox = getClipperInboxNote();
-
- const {note} = noteService.createNewNote({
- parentNoteId: clipperInbox.noteId,
- title,
- content,
- type: 'text'
- });
clipType = htmlSanitizer.sanitize(clipType);
- note.setLabel('clipType', clipType);
+ const clipperInbox = getClipperInboxNote();
+ const dailyNote = dateNoteService.getDayNote(dateUtils.localNowDate());
+ pageUrl = htmlSanitizer.sanitizeUrl(pageUrl);
+ let note = findClippingNote(clipperInbox, pageUrl, clipType);
- if (pageUrl) {
- pageUrl = htmlSanitizer.sanitizeUrl(pageUrl);
+ if (!note){
+ note = noteService.createNewNote({
+ parentNoteId: dailyNote.noteId,
+ title,
+ content: '',
+ type: 'text'
+ }).note;
+
+ note.setLabel('clipType', clipType);
+ if (pageUrl) {
+ pageUrl = htmlSanitizer.sanitizeUrl(pageUrl);
+
+ note.setLabel('pageUrl', pageUrl);
+ note.setLabel('iconClass', 'bx bx-globe');
+ }
- note.setLabel('pageUrl', pageUrl);
- note.setLabel('iconClass', 'bx bx-globe');
}
+
+
+
if (labels) {
for (const labelName in labels) {
@@ -108,9 +138,11 @@ function createNote(req) {
}
}
+ const existingContent = note.getContent();
const rewrittenContent = processContent(images, note, content);
+ note.setContent(`${existingContent}${existingContent.trim() ? "
" : ""}${rewrittenContent}`);
- note.setContent(rewrittenContent);
+ // note.setContent(rewrittenContent);
return {
noteId: note.noteId
@@ -158,6 +190,15 @@ function processContent(images, note, content) {
// fallback if parsing/downloading images fails for some reason on the extension side (
rewrittenContent = noteService.downloadImages(note.noteId, rewrittenContent);
+ // Check if rewrittenContent contains at least one HTML tag
+ if (!/<.+?>/.test(rewrittenContent)) {
+ rewrittenContent = '
'+rewrittenContent + '
'; + } + // Create a JSDOM object from the existing HTML content + let dom = new JSDOM(rewrittenContent); + + // Get the content inside the body tag and serialize it + rewrittenContent = dom.window.document.body.innerHTML; return rewrittenContent; } @@ -187,9 +228,20 @@ function handshake() { } } +function findNotesByUrl(req){ + let pageUrl = req.params.noteUrl; + const clipperInbox = getClipperInboxNote(); + let foundPage = findClippingNote(clipperInbox, pageUrl, null); + return { + noteId: foundPage ? foundPage.noteId : null + } + +} + module.exports = { createNote, addClipping, openNote, - handshake + handshake, + findNotesByUrl }; diff --git a/src/routes/routes.js b/src/routes/routes.js index 2989208b5..764b2746c 100644 --- a/src/routes/routes.js +++ b/src/routes/routes.js @@ -286,6 +286,7 @@ function register(app) { route(POST, '/api/clipper/clippings', clipperMiddleware, clipperRoute.addClipping, apiResultHandler); route(POST, '/api/clipper/notes', clipperMiddleware, clipperRoute.createNote, apiResultHandler); route(POST, '/api/clipper/open/:noteId', clipperMiddleware, clipperRoute.openNote, apiResultHandler); + route(GET, '/api/clipper/notes-by-url/:noteUrl', clipperMiddleware, clipperRoute.findNotesByUrl, apiResultHandler); apiRoute(GET, '/api/similar-notes/:noteId', similarNotesRoute.getSimilarNotes);