various tweaks to shared notes

This commit is contained in:
zadam
2021-12-27 20:48:14 +01:00
parent b8fe9a41db
commit 3128a7d62f
6 changed files with 132 additions and 71 deletions

View File

@@ -1,43 +1,18 @@
const {JSDOM} = require("jsdom");
const NO_CONTENT = '<p>This note has no content.</p>';
const shaca = require("./shaca/shaca");
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();
let header = '';
let isEmpty = false;
if (note.type === 'text') {
const document = new JSDOM(content || "").window.document;
const isEmpty = document.body.textContent.trim().length === 0
isEmpty = document.body.textContent.trim().length === 0
&& document.querySelectorAll("img").length === 0;
if (isEmpty) {
content = NO_CONTENT;
}
else {
if (!isEmpty) {
for (const linkEl of document.querySelectorAll("a")) {
const href = linkEl.getAttribute("href");
@@ -49,6 +24,7 @@ function getContent(note) {
if (linkedNote) {
linkEl.setAttribute("href", linkedNote.shareId);
linkEl.classList.add("type-" + linkedNote.type);
}
else {
linkEl.removeAttribute("href");
@@ -57,28 +33,39 @@ function getContent(note) {
}
content = document.body.innerHTML;
if (content.includes(`<span class="math-tex">`)) {
content += `<script src="../../libraries/katex/katex.min.js"></script>`;
content += `<link rel="stylesheet" href="../../libraries/katex/katex.min.css">`;
content += `<script src="../../libraries/katex/auto-render.min.js" onload="renderMathInElement(document.getElementById('content'));"></script>`;
content += `<script src="../../libraries/katex/mhchem.min.js"></script>`;
header += `
<script src="../../libraries/katex/katex.min.js"></script>
<link rel="stylesheet" href="../../libraries/katex/katex.min.css">
<script src="../../libraries/katex/auto-render.min.js" onload="renderMathInElement(document.getElementById('content'));"></script>
<script src="../../libraries/katex/mhchem.min.js"></script>`;
}
}
}
else if (note.type === 'code') {
if (!content?.trim()) {
content = NO_CONTENT;
isEmpty = true;
}
else {
content = `<textarea style="width:100px;" id="code">${content}</textarea>`
content += `<link rel="stylesheet" href="../../libraries/codemirror/codemirror.css">`
content += `<script src="../../libraries/codemirror/codemirror.js" onload="var editor = CodeMirror.fromTextArea(document.getElementById('code'), {lineNumbers: true, lineWrapping: true});"></script>`
const document = new JSDOM().window.document;
const preEl = document.createElement('pre');
preEl.appendChild(document.createTextNode(content));
content = preEl.outerHTML;
}
}
else if (note.type === 'mermaid') {
content = `<div class=\"mermaid\">${content}</div><script src=\"../../libraries/mermaid.min.js\"></script><hr><details><summary>Chart source</summary><pre>${content}</pre></details>`
}
content = `
<div class="mermaid">${content}</div>
<hr>
<details>
<summary>Chart source</summary>
<pre>${content}</pre>
</details>`
header += `<script src="../../libraries/mermaid.min.js"></script>`;
}
else if (note.type === 'image') {
content = `<img src="api/images/${note.noteId}/${note.title}?${note.utcDateModified}">`;
}
@@ -90,13 +77,18 @@ function getContent(note) {
content = `<button type="button" onclick="location.href='api/notes/${note.noteId}/download'">Download file</button>`;
}
}
else if (note.type === 'book') {
isEmpty = true;
}
else {
content = '<p>This note type cannot be displayed.</p>';
}
var child = getChildrenList(note);
content += child === '' ? '' : `<hr>${child}`;
return content;
return {
header,
content,
isEmpty
};
}
module.exports = {

View File

@@ -29,13 +29,15 @@ function register(router) {
const note = shaca.aliasToNote[shareId] || shaca.notes[shareId];
if (note) {
const content = contentRenderer.getContent(note);
const {header, content, isEmpty} = contentRenderer.getContent(note);
const subRoot = getSharedSubTreeRoot(note);
res.render("share/page", {
note,
header,
content,
isEmpty,
subRoot
});
}
@@ -78,7 +80,7 @@ function register(router) {
res.send(note.getContent());
});
router.get('/share/api/notes/:noteId/view', (req, res, next) => {
const {noteId} = req.params;
const note = shaca.getNote(noteId);