mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
added 404 error page
This commit is contained in:
parent
cfa49c7b1b
commit
1180be75d1
84
src/share/content_renderer.js
Normal file
84
src/share/content_renderer.js
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
const {JSDOM} = require("jsdom");
|
||||||
|
const NO_CONTENT = '<p>This note has no content.</p>';
|
||||||
|
|
||||||
|
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 '<p>Child notes:</p>' + 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 = `<img src="api/images/${note.noteId}/${note.title}?${note.utcDateModified}">`;
|
||||||
|
}
|
||||||
|
else if (note.type === 'file') {
|
||||||
|
content = `<button type="button" onclick="location.href='api/notes/${note.noteId}/download'">Download file</button>`;
|
||||||
|
}
|
||||||
|
else if (note.type === 'book') {
|
||||||
|
content = getChildrenList(note);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
content = '<p>This note type cannot be displayed.</p>' + getChildrenList(note);
|
||||||
|
}
|
||||||
|
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getContent
|
||||||
|
};
|
@ -1,7 +1,7 @@
|
|||||||
const shaca = require("./shaca/shaca");
|
const shaca = require("./shaca/shaca");
|
||||||
const shacaLoader = require("./shaca/shaca_loader");
|
const shacaLoader = require("./shaca/shaca_loader");
|
||||||
const shareRoot = require("./share_root");
|
const shareRoot = require("./share_root");
|
||||||
const {JSDOM} = require("jsdom");
|
const contentRenderer = require("./content_renderer.js");
|
||||||
|
|
||||||
function getSubRoot(note) {
|
function getSubRoot(note) {
|
||||||
if (note.noteId === shareRoot.SHARE_ROOT_NOTE_ID) {
|
if (note.noteId === shareRoot.SHARE_ROOT_NOTE_ID) {
|
||||||
@ -17,86 +17,6 @@ function getSubRoot(note) {
|
|||||||
return getSubRoot(parentNote);
|
return getSubRoot(parentNote);
|
||||||
}
|
}
|
||||||
|
|
||||||
const NO_CONTENT = '<p>This note has no content.</p>';
|
|
||||||
|
|
||||||
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 '<p>Child notes:</p>' + 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 = `<img src="api/images/${note.noteId}/${note.title}?${note.utcDateModified}">`;
|
|
||||||
}
|
|
||||||
else if (note.type === 'file') {
|
|
||||||
content = `<button type="button" onclick="location.href='api/notes/${note.noteId}/download'">Download file</button>`;
|
|
||||||
}
|
|
||||||
else if (note.type === 'book') {
|
|
||||||
content = getChildrenList(note);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
content = '<p>This note type cannot be displayed.</p>' + getChildrenList(note);
|
|
||||||
}
|
|
||||||
|
|
||||||
return content;
|
|
||||||
}
|
|
||||||
|
|
||||||
function register(router) {
|
function register(router) {
|
||||||
router.get('/share/:noteId', (req, res, next) => {
|
router.get('/share/:noteId', (req, res, next) => {
|
||||||
const {noteId} = req.params;
|
const {noteId} = req.params;
|
||||||
@ -106,18 +26,18 @@ function register(router) {
|
|||||||
if (noteId in shaca.notes) {
|
if (noteId in shaca.notes) {
|
||||||
const note = shaca.notes[noteId];
|
const note = shaca.notes[noteId];
|
||||||
|
|
||||||
const content = getContent(note);
|
const content = contentRenderer.getContent(note);
|
||||||
|
|
||||||
const subRoot = getSubRoot(note);
|
const subRoot = getSubRoot(note);
|
||||||
|
|
||||||
res.render("share", {
|
res.render("share/page", {
|
||||||
note,
|
note,
|
||||||
content,
|
content,
|
||||||
subRoot
|
subRoot
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
res.send("FFF");
|
res.status(404).render("share/404");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -48,8 +48,8 @@ function load() {
|
|||||||
WHERE isDeleted = 0
|
WHERE isDeleted = 0
|
||||||
AND noteId IN (${noteIdStr})
|
AND noteId IN (${noteIdStr})
|
||||||
AND (
|
AND (
|
||||||
(type = 'label' AND name IN ('archived'))
|
(type = 'label' AND name IN ('archived', 'shareHiddenFromTree'))
|
||||||
OR (type = 'relation' AND name IN ('imageLink', 'template'))
|
OR (type = 'relation' AND name IN ('imageLink', 'template', 'shareCss'))
|
||||||
)`, []);
|
)`, []);
|
||||||
|
|
||||||
for (const row of attributes) {
|
for (const row of attributes) {
|
||||||
|
11
src/views/share/404.ejs
Normal file
11
src/views/share/404.ejs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<link rel="shortcut icon" href="../favicon.ico">
|
||||||
|
<title>Not found</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Not found</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -24,7 +24,7 @@
|
|||||||
<button id="menuButton"></button>
|
<button id="menuButton"></button>
|
||||||
|
|
||||||
<div id="menu">
|
<div id="menu">
|
||||||
<%- include('share-tree-item', {note: subRoot, activeNote: note}) %>
|
<%- include('tree_item', {note: subRoot, activeNote: note}) %>
|
||||||
</div>
|
</div>
|
||||||
<% } %>
|
<% } %>
|
||||||
</div>
|
</div>
|
@ -10,7 +10,7 @@
|
|||||||
<ul>
|
<ul>
|
||||||
<% note.getChildNotes().forEach(function (childNote) { %>
|
<% note.getChildNotes().forEach(function (childNote) { %>
|
||||||
<li>
|
<li>
|
||||||
<%- include('share-tree-item', {note: childNote}) %>
|
<%- include('tree_item', {note: childNote}) %>
|
||||||
</li>
|
</li>
|
||||||
<% }) %>
|
<% }) %>
|
||||||
</ul>
|
</ul>
|
Loading…
x
Reference in New Issue
Block a user