From e365521d5e41dbad2f6ab2aa988799dd41150603 Mon Sep 17 00:00:00 2001 From: Tom <7283497+thfrei@users.noreply.github.com> Date: Mon, 11 Apr 2022 21:38:05 +0200 Subject: [PATCH] add support for sharing canvas-note --- src/share/routes.js | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/src/share/routes.js b/src/share/routes.js index 609028597..4aa90163a 100644 --- a/src/share/routes.js +++ b/src/share/routes.js @@ -1,3 +1,5 @@ +const excalidrawToSvg = require("excalidraw-to-svg"); + const shaca = require("./shaca/shaca"); const shacaLoader = require("./shaca/shaca_loader"); const shareRoot = require("./share_root"); @@ -112,15 +114,41 @@ function register(router) { if (!image) { return res.status(404).send(`Note ${noteId} not found`); } - else if (image.type !== 'image') { - return res.status(400).send("Requested note is not an image"); + else if (!["image", "canvas-note"].includes(image.type)) { + return res.status(400).send("Requested note is not a shareable image"); + } else if (image.type === "canvas-note") { + /** + * FIXME: deduplicate the code from api/image.js + */ + // render the svg in node.js using excalidraw and jsdom + const content = image.getContent(); + try { + const data = JSON.parse(content) + const excalidrawData = { + type: "excalidraw", + version: 2, + source: "trilium", + elements: data.elements, + appState: data.appState, + files: data.files, + } + excalidrawToSvg(excalidrawData) + .then(svg => { + const svgHtml = svg.outerHTML; + res.set('Content-Type', "image/svg+xml"); + res.set("Cache-Control", "no-cache, no-store, must-revalidate"); + res.send(svgHtml); + }); + } catch(err) { + res.status(500).send("there was an error parsing excalidraw to svg"); + } + } else { + // normal image + res.set('Content-Type', image.mime); + + res.send(image.getContent()); } - addNoIndexHeader(image, res); - - res.set('Content-Type', image.mime); - - res.send(image.getContent()); }); // used for PDF viewing