|
|
|
|
@@ -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,46 +14,25 @@ const path = require('path');
|
|
|
|
|
const BAttribute = require('../../becca/entities/battribute');
|
|
|
|
|
const htmlSanitizer = require('../../services/html_sanitizer');
|
|
|
|
|
const {formatAttrForSearch} = require("../../services/attribute_formatter");
|
|
|
|
|
|
|
|
|
|
function findClippingNote(clipperInboxNote, pageUrl) {
|
|
|
|
|
const notes = clipperInboxNote.searchNotesInSubtree(
|
|
|
|
|
formatAttrForSearch({
|
|
|
|
|
type: 'label',
|
|
|
|
|
name: "pageUrl",
|
|
|
|
|
value: pageUrl
|
|
|
|
|
}, true)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
for (const note of notes) {
|
|
|
|
|
if (note.getOwnedLabelValue('clipType') === 'clippings') {
|
|
|
|
|
return note;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getClipperInboxNote() {
|
|
|
|
|
let clipperInbox = attributeService.getNoteWithLabel('clipperInbox');
|
|
|
|
|
|
|
|
|
|
if (!clipperInbox) {
|
|
|
|
|
clipperInbox = dateNoteService.getDayNote(dateUtils.localNowDate());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return clipperInbox;
|
|
|
|
|
}
|
|
|
|
|
const jsdom = require("jsdom");
|
|
|
|
|
const { JSDOM } = jsdom;
|
|
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
|
|
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'
|
|
|
|
|
@@ -67,13 +47,45 @@ function addClipping(req) {
|
|
|
|
|
|
|
|
|
|
const existingContent = clippingNote.getContent();
|
|
|
|
|
|
|
|
|
|
clippingNote.setContent(`${existingContent}${existingContent.trim() ? "<br/>" : ""}${rewrittenContent}`);
|
|
|
|
|
clippingNote.setContent(`${existingContent}${existingContent.trim() ? "<br>" : ""}${rewrittenContent}`);
|
|
|
|
|
|
|
|
|
|
if (clippingNote.parentNoteId !== dailyNote.noteId) {
|
|
|
|
|
cloneService.cloneNoteToParentNote(clippingNote.noteId, dailyNote.noteId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
noteId: clippingNote.noteId
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
clipperInbox = dateNoteService.getRootCalendarNote();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return clipperInbox;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createNote(req) {
|
|
|
|
|
let {title, content, pageUrl, images, clipType, labels} = req.body;
|
|
|
|
|
|
|
|
|
|
@@ -81,26 +93,31 @@ 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('pageUrl', pageUrl);
|
|
|
|
|
note.setLabel('iconClass', 'bx bx-globe');
|
|
|
|
|
note.setLabel('clipType', clipType);
|
|
|
|
|
|
|
|
|
|
if (pageUrl) {
|
|
|
|
|
pageUrl = htmlSanitizer.sanitizeUrl(pageUrl);
|
|
|
|
|
|
|
|
|
|
note.setLabel('pageUrl', pageUrl);
|
|
|
|
|
note.setLabel('iconClass', 'bx bx-globe');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (labels) {
|
|
|
|
|
for (const labelName in labels) {
|
|
|
|
|
const labelValue = htmlSanitizer.sanitize(labels[labelName]);
|
|
|
|
|
@@ -108,9 +125,9 @@ function createNote(req) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const existingContent = note.getContent();
|
|
|
|
|
const rewrittenContent = processContent(images, note, content);
|
|
|
|
|
|
|
|
|
|
note.setContent(rewrittenContent);
|
|
|
|
|
note.setContent(`${existingContent}${existingContent.trim() ? "<br/>" : ""}${rewrittenContent}`);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
noteId: note.noteId
|
|
|
|
|
@@ -158,6 +175,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 = `<p>${rewrittenContent}</p>`;
|
|
|
|
|
}
|
|
|
|
|
// Create a JSDOM object from the existing HTML content
|
|
|
|
|
const dom = new JSDOM(rewrittenContent);
|
|
|
|
|
|
|
|
|
|
// Get the content inside the body tag and serialize it
|
|
|
|
|
rewrittenContent = dom.window.document.body.innerHTML;
|
|
|
|
|
|
|
|
|
|
return rewrittenContent;
|
|
|
|
|
}
|
|
|
|
|
@@ -187,9 +213,19 @@ 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
|
|
|
|
|
};
|
|
|
|
|
|