"use strict";
const attributeService = require("../../services/attributes");
const noteService = require('../../services/notes');
const dateNoteService = require('../../services/date_notes');
const dateUtils = require('../../services/date_utils');
const imageService = require('../../services/image');
const appInfo = require('../../services/app_info');
const ws = require('../../services/ws.js');
const log = require('../../services/log');
const utils = require('../../services/utils');
const path = require('path');
const Attribute = require('../../entities/attribute');
const htmlSanitizer = require('../../services/html_sanitizer');
function findClippingNote(todayNote, pageUrl) {
const notes = todayNote.getDescendantNotesWithLabel('pageUrl', pageUrl);
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.getDateNote(dateUtils.localNowDate());
}
return clipperInbox;
}
function addClipping(req) {
const {title, content, pageUrl, images} = req.body;
const clipperInbox = getClipperInboxNote();
let clippingNote = findClippingNote(clipperInbox, pageUrl);
if (!clippingNote) {
clippingNote = noteService.createNewNote({
parentNoteId: clipperInbox.noteId,
title: title,
content: '',
type: 'text'
}).note;
clippingNote.setLabel('clipType', 'clippings');
clippingNote.setLabel('pageUrl', pageUrl);
}
const rewrittenContent = processContent(images, clippingNote, content);
const existingContent = clippingNote.getContent();
clippingNote.setContent(existingContent + (existingContent.trim() ? "
" : "") + rewrittenContent);
return {
noteId: clippingNote.noteId
};
}
function createNote(req) {
let {title, content, pageUrl, images, clipType} = req.body;
if (!title || !title.trim()) {
title = "Clipped note from " + pageUrl;
}
const clipperInbox = getClipperInboxNote();
const {note} = noteService.createNewNote({
parentNoteId: clipperInbox.noteId,
title,
content,
type: 'text'
});
note.setLabel('clipType', clipType);
if (pageUrl) {
note.setLabel('pageUrl', pageUrl);
}
const rewrittenContent = processContent(images, note, content);
note.setContent(rewrittenContent);
return {
noteId: note.noteId
};
}
function processContent(images, note, content) {
let rewrittenContent = htmlSanitizer.sanitize(content)
// H1 is not supported so convert it to H2
.replace(/