Notes/src/routes/api/clipper.ts

237 lines
7.1 KiB
TypeScript
Raw Normal View History

2019-06-22 19:49:48 +02:00
"use strict";
2024-04-05 20:45:57 +03:00
import { Request } from "express";
import attributeService from "../../services/attributes.js";
import cloneService from "../../services/cloning.js";
import noteService from "../../services/notes.js";
import dateNoteService from "../../services/date_notes.js";
import dateUtils from "../../services/date_utils.js";
import imageService from "../../services/image.js";
import appInfo from "../../services/app_info.js";
import ws from "../../services/ws.js";
import log from "../../services/log.js";
import utils from "../../services/utils.js";
import path from "path";
import htmlSanitizer from "../../services/html_sanitizer.js";
import attributeFormatter from "../../services/attribute_formatter.js";
import jsdom from "jsdom";
import BNote from "../../becca/entities/bnote.js";
import ValidationError from "../../errors/validation_error.js";
const { JSDOM } = jsdom;
2019-06-22 19:49:48 +02:00
2024-04-05 20:45:57 +03:00
interface Image {
src: string;
dataUrl: string;
imageId: string;
}
function addClipping(req: Request) {
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
2025-01-09 18:07:02 +02:00
let { title, content, pageUrl, images } = req.body;
const clipType = "clippings";
2020-06-20 12:31:38 +02:00
const clipperInbox = getClipperInboxNote();
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
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,
2025-01-09 18:07:02 +02:00
content: "",
type: "text"
2021-01-14 21:52:44 +01:00
}).note;
2025-01-09 18:07:02 +02:00
clippingNote.setLabel("clipType", "clippings");
clippingNote.setLabel("pageUrl", pageUrl);
clippingNote.setLabel("iconClass", "bx bx-globe");
}
2020-09-16 20:32:20 +02:00
const rewrittenContent = processContent(images, clippingNote, content);
2020-06-20 12:31:38 +02:00
const existingContent = clippingNote.getContent();
2024-04-05 20:45:57 +03:00
if (typeof existingContent !== "string") {
throw new ValidationError("Invalid note content type.");
}
2020-04-05 15:35:01 +02:00
clippingNote.setContent(`${existingContent}${existingContent.trim() ? "<br>" : ""}${rewrittenContent}`);
2023-07-09 22:58:34 +02:00
2024-04-05 20:45:57 +03:00
// TODO: Is parentNoteId ever defined?
if ((clippingNote as any).parentNoteId !== clipperInbox.noteId) {
2023-08-09 23:00:42 +02:00
cloneService.cloneNoteToParentNote(clippingNote.noteId, clipperInbox.noteId);
}
2023-07-09 22:58:34 +02:00
return {
noteId: clippingNote.noteId
};
}
2024-04-05 20:45:57 +03:00
function findClippingNote(clipperInboxNote: BNote, pageUrl: string, clipType: string | null) {
2023-07-09 22:58:34 +02:00
if (!pageUrl) {
return null;
}
const notes = clipperInboxNote.searchNotesInSubtree(
2025-01-09 18:07:02 +02:00
attributeFormatter.formatAttrForSearch(
{
type: "label",
name: "pageUrl",
value: pageUrl
},
true
)
2023-07-09 22:58:34 +02:00
);
2025-01-09 18:07:02 +02:00
return clipType ? notes.find((note) => note.getOwnedLabelValue("clipType") === clipType) : notes[0];
2023-07-09 22:58:34 +02:00
}
function getClipperInboxNote() {
2025-01-09 18:07:02 +02:00
let clipperInbox = attributeService.getNoteWithLabel("clipperInbox");
2023-07-09 22:58:34 +02:00
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;
}
2024-04-05 20:45:57 +03:00
function createNote(req: Request) {
2025-01-09 18:07:02 +02:00
let { title, content, pageUrl, images, clipType, labels } = req.body;
2020-04-08 11:07:38 +02:00
if (!title || !title.trim()) {
title = `Clipped note from ${pageUrl}`;
}
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();
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) {
note = noteService.createNewNote({
parentNoteId: clipperInbox.noteId,
title,
2025-01-09 18:07:02 +02:00
content: "",
type: "text"
}).note;
2025-01-09 18:07:02 +02:00
note.setLabel("clipType", clipType);
2023-07-09 22:58:34 +02:00
if (pageUrl) {
pageUrl = htmlSanitizer.sanitizeUrl(pageUrl);
2025-01-09 18:07:02 +02:00
note.setLabel("pageUrl", pageUrl);
note.setLabel("iconClass", "bx bx-globe");
}
2019-07-06 16:48:06 +02: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);
}
}
2019-06-23 11:25:15 +02:00
const existingContent = note.getContent();
2024-04-05 20:45:57 +03:00
if (typeof existingContent !== "string") {
throw new ValidationError("Invalid note content tpye.");
}
2020-09-16 20:32:20 +02:00
const rewrittenContent = processContent(images, note, content);
const newContent = `${existingContent}${existingContent.trim() ? "<br/>" : ""}${rewrittenContent}`;
note.setContent(newContent);
noteService.asyncPostProcessContent(note, newContent); // to mark attachments as used
return {
noteId: note.noteId
};
}
2024-04-05 20:45:57 +03:00
function processContent(images: Image[], note: BNote, content: string) {
let rewrittenContent = htmlSanitizer.sanitize(content);
2019-06-23 11:25:15 +02:00
2019-07-06 16:48:06 +02:00
if (images) {
2025-01-09 18:07:02 +02:00
for (const { src, dataUrl, imageId } of images) {
2019-07-06 16:48:06 +02:00
const filename = path.basename(src);
2019-06-23 11:25:15 +02:00
if (!dataUrl || !dataUrl.startsWith("data:image")) {
2025-01-09 18:07:02 +02:00
const excerpt = dataUrl ? dataUrl.substr(0, Math.min(100, dataUrl.length)) : "null";
2021-04-12 23:29:02 +02: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
2025-01-09 18:07:02 +02:00
const buffer = Buffer.from(dataUrl.split(",")[1], "base64");
2019-06-23 11:25:15 +02:00
const attachment = imageService.saveImageToAttachment(note.noteId, buffer, filename, true);
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
// 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)) {
2023-07-09 22:58:34 +02:00
rewrittenContent = `<p>${rewrittenContent}</p>`;
}
// Create a JSDOM object from the existing HTML content
2023-07-09 22:58:34 +02:00
const dom = new JSDOM(rewrittenContent);
// Get the content inside the body tag and serialize it
rewrittenContent = dom.window.document.body.innerHTML;
return rewrittenContent;
2019-06-22 19:49:48 +02:00
}
2024-04-05 20:45:57 +03:00
function openNote(req: Request) {
if (utils.isElectron()) {
2019-08-26 20:21:43 +02:00
ws.sendMessageToAllClients({
2025-01-09 18:07:02 +02:00
type: "openNote",
noteId: req.params.noteId
});
return {
2025-01-09 18:07:02 +02:00
result: "ok"
};
2025-01-09 18:07:02 +02:00
} else {
return {
2025-01-09 18:07:02 +02:00
result: "open-in-browser"
};
}
2019-06-22 19:49:48 +02:00
}
2020-06-20 12:31:38 +02:00
function handshake() {
return {
appName: "trilium",
2019-07-07 22:27:06 +02:00
protocolVersion: appInfo.clipperProtocolVersion
2025-01-09 18:07:02 +02:00
};
2019-06-22 19:49:48 +02:00
}
2025-01-09 18:07:02 +02:00
function findNotesByUrl(req: Request) {
let pageUrl = req.params.noteUrl;
const clipperInbox = getClipperInboxNote();
2023-07-02 12:51:23 +02:00
let foundPage = findClippingNote(clipperInbox, pageUrl, null);
return {
noteId: foundPage ? foundPage.noteId : null
2025-01-09 18:07:02 +02:00
};
}
export default {
2019-06-22 19:49:48 +02:00
createNote,
addClipping,
2019-06-23 13:25:00 +02:00
openNote,
handshake,
findNotesByUrl
2020-05-13 23:06:13 +02:00
};