From 1180be75d1a1a73cb04967d760ea61452a27d773 Mon Sep 17 00:00:00 2001 From: zadam Date: Wed, 22 Dec 2021 09:10:38 +0100 Subject: [PATCH] added 404 error page --- src/share/content_renderer.js | 84 ++++++++++++++++++ src/share/routes.js | 88 +------------------ src/share/shaca/shaca_loader.js | 4 +- src/views/share/404.ejs | 11 +++ src/views/{share.ejs => share/page.ejs} | 2 +- .../tree_item.ejs} | 2 +- 6 files changed, 103 insertions(+), 88 deletions(-) create mode 100644 src/share/content_renderer.js create mode 100644 src/views/share/404.ejs rename src/views/{share.ejs => share/page.ejs} (97%) rename src/views/{share-tree-item.ejs => share/tree_item.ejs} (83%) diff --git a/src/share/content_renderer.js b/src/share/content_renderer.js new file mode 100644 index 000000000..8fe406d53 --- /dev/null +++ b/src/share/content_renderer.js @@ -0,0 +1,84 @@ +const {JSDOM} = require("jsdom"); +const NO_CONTENT = '

This note has no content.

'; + +function getChildrenList(note) { + if (note.hasChildren()) { + const document = new JSDOM().window.document; + + const ulEl = document.createElement("ul"); + + for (const childNote of note.getChildNotes()) { + const li = document.createElement("li"); + const link = document.createElement("a"); + link.appendChild(document.createTextNode(childNote.title)); + link.setAttribute("href", childNote.noteId); + + li.appendChild(link); + ulEl.appendChild(li); + } + + return '

Child notes:

' + ulEl.outerHTML; + } + else { + return ''; + } +} + +function getContent(note) { + let content = note.getContent(); + + if (note.type === 'text') { + const document = new JSDOM(content || "").window.document; + + const isEmpty = document.body.textContent.trim().length === 0 + && document.querySelectorAll("img").length === 0; + + if (isEmpty) { + content = NO_CONTENT + getChildrenList(note); + } + else { + for (const linkEl of document.querySelectorAll("a")) { + const href = linkEl.getAttribute("href"); + + if (href?.startsWith("#")) { + const notePathSegments = href.split("/"); + + linkEl.setAttribute("href", notePathSegments[notePathSegments.length - 1]); + } + } + + content = document.body.innerHTML; + } + } + else if (note.type === 'code') { + if (!content?.trim()) { + content = NO_CONTENT + getChildrenList(note); + } + else { + const document = new JSDOM().window.document; + + const preEl = document.createElement('pre'); + preEl.appendChild(document.createTextNode(content)); + + content = preEl.outerHTML; + } + } + else if (note.type === 'image') { + content = ``; + } + else if (note.type === 'file') { + content = ``; + } + else if (note.type === 'book') { + content = getChildrenList(note); + } + else { + content = '

This note type cannot be displayed.

' + getChildrenList(note); + } + + return content; +} + +module.exports = { + getContent +}; diff --git a/src/share/routes.js b/src/share/routes.js index 465c7d373..d91aed1cd 100644 --- a/src/share/routes.js +++ b/src/share/routes.js @@ -1,7 +1,7 @@ const shaca = require("./shaca/shaca"); const shacaLoader = require("./shaca/shaca_loader"); const shareRoot = require("./share_root"); -const {JSDOM} = require("jsdom"); +const contentRenderer = require("./content_renderer.js"); function getSubRoot(note) { if (note.noteId === shareRoot.SHARE_ROOT_NOTE_ID) { @@ -17,86 +17,6 @@ function getSubRoot(note) { return getSubRoot(parentNote); } -const NO_CONTENT = '

This note has no content.

'; - -function getChildrenList(note) { - if (note.hasChildren()) { - const document = new JSDOM().window.document; - - const ulEl = document.createElement("ul"); - - for (const childNote of note.getChildNotes()) { - const li = document.createElement("li"); - const link = document.createElement("a"); - link.appendChild(document.createTextNode(childNote.title)); - link.setAttribute("href", childNote.noteId); - - li.appendChild(link); - ulEl.appendChild(li); - } - - return '

Child notes:

' + ulEl.outerHTML; - } - else { - return ''; - } -} - -function getContent(note) { - let content = note.getContent(); - - if (note.type === 'text') { - const document = new JSDOM(content || "").window.document; - - const isEmpty = document.body.textContent.trim().length === 0 - && document.querySelectorAll("img").length === 0; - - if (isEmpty) { - content = NO_CONTENT + getChildrenList(note); - } - else { - for (const linkEl of document.querySelectorAll("a")) { - const href = linkEl.getAttribute("href"); - - if (href?.startsWith("#")) { - const notePathSegments = href.split("/"); - - linkEl.setAttribute("href", notePathSegments[notePathSegments.length - 1]); - } - } - - content = document.body.innerHTML; - } - } - else if (note.type === 'code') { - if (!content?.trim()) { - content = NO_CONTENT + getChildrenList(note); - } - else { - const document = new JSDOM().window.document; - - const preEl = document.createElement('pre'); - preEl.appendChild(document.createTextNode(content)); - - content = preEl.outerHTML; - } - } - else if (note.type === 'image') { - content = ``; - } - else if (note.type === 'file') { - content = ``; - } - else if (note.type === 'book') { - content = getChildrenList(note); - } - else { - content = '

This note type cannot be displayed.

' + getChildrenList(note); - } - - return content; -} - function register(router) { router.get('/share/:noteId', (req, res, next) => { const {noteId} = req.params; @@ -106,18 +26,18 @@ function register(router) { if (noteId in shaca.notes) { const note = shaca.notes[noteId]; - const content = getContent(note); + const content = contentRenderer.getContent(note); const subRoot = getSubRoot(note); - res.render("share", { + res.render("share/page", { note, content, subRoot }); } else { - res.send("FFF"); + res.status(404).render("share/404"); } }); diff --git a/src/share/shaca/shaca_loader.js b/src/share/shaca/shaca_loader.js index 502844ef0..c98c15b9c 100644 --- a/src/share/shaca/shaca_loader.js +++ b/src/share/shaca/shaca_loader.js @@ -48,8 +48,8 @@ function load() { WHERE isDeleted = 0 AND noteId IN (${noteIdStr}) AND ( - (type = 'label' AND name IN ('archived')) - OR (type = 'relation' AND name IN ('imageLink', 'template')) + (type = 'label' AND name IN ('archived', 'shareHiddenFromTree')) + OR (type = 'relation' AND name IN ('imageLink', 'template', 'shareCss')) )`, []); for (const row of attributes) { diff --git a/src/views/share/404.ejs b/src/views/share/404.ejs new file mode 100644 index 000000000..7389db892 --- /dev/null +++ b/src/views/share/404.ejs @@ -0,0 +1,11 @@ + + + + + + Not found + + +

Not found

+ + diff --git a/src/views/share.ejs b/src/views/share/page.ejs similarity index 97% rename from src/views/share.ejs rename to src/views/share/page.ejs index 9c90ecedb..bc9d48426 100644 --- a/src/views/share.ejs +++ b/src/views/share/page.ejs @@ -24,7 +24,7 @@ <% } %> diff --git a/src/views/share-tree-item.ejs b/src/views/share/tree_item.ejs similarity index 83% rename from src/views/share-tree-item.ejs rename to src/views/share/tree_item.ejs index a1285746d..8d7bc5a73 100644 --- a/src/views/share-tree-item.ejs +++ b/src/views/share/tree_item.ejs @@ -10,7 +10,7 @@