add possibility to define a share index, closes #3265

This commit is contained in:
zadam
2022-11-01 22:49:37 +01:00
parent eb68ab6776
commit 2467464433
8 changed files with 147 additions and 87 deletions

View File

@@ -1,6 +1,7 @@
const {JSDOM} = require("jsdom");
const shaca = require("./shaca/shaca");
const assetPath = require("../services/asset_path");
const shareRoot = require('./share_root');
function getContent(note) {
if (note.isProtected) {
@@ -11,40 +12,74 @@ function getContent(note) {
};
}
let content = note.getContent();
let header = '';
let isEmpty = false;
const result = {
content: note.getContent(),
header: '',
isEmpty: false
};
if (note.type === 'text') {
const document = new JSDOM(content || "").window.document;
renderText(result, note);
} else if (note.type === 'code') {
renderCode(result);
} else if (note.type === 'mermaid') {
renderMermaid(result);
} else if (note.type === 'image') {
renderImage(result, note);
} else if (note.type === 'file') {
renderFile(note, result);
} else if (note.type === 'book') {
result.isEmpty = true;
} else if (note.type === 'canvas') {
renderCanvas(result, note);
} else {
result.content = '<p>This note type cannot be displayed.</p>';
}
isEmpty = document.body.textContent.trim().length === 0
&& document.querySelectorAll("img").length === 0;
return result;
}
if (!isEmpty) {
for (const linkEl of document.querySelectorAll("a")) {
const href = linkEl.getAttribute("href");
function renderIndex(result) {
result.content += '<ul id="index">';
if (href?.startsWith("#")) {
const notePathSegments = href.split("/");
const rootNote = shaca.getNote(shareRoot.SHARE_ROOT_NOTE_ID);
const noteId = notePathSegments[notePathSegments.length - 1];
const linkedNote = shaca.getNote(noteId);
for (const childNote of rootNote.getChildNotes()) {
result.content += `<li><a class="${childNote.type}" href="./${childNote.shareId}">${childNote.escapedTitle}</a></li>`;
}
if (linkedNote) {
linkEl.setAttribute("href", linkedNote.shareId);
linkEl.classList.add("type-" + linkedNote.type);
}
else {
linkEl.removeAttribute("href");
}
result.content += '</ul>';
}
function renderText(result, note) {
const document = new JSDOM(result.content || "").window.document;
result.isEmpty = document.body.textContent.trim().length === 0
&& document.querySelectorAll("img").length === 0;
if (!result.isEmpty) {
for (const linkEl of document.querySelectorAll("a")) {
const href = linkEl.getAttribute("href");
if (href?.startsWith("#")) {
const notePathSegments = href.split("/");
const noteId = notePathSegments[notePathSegments.length - 1];
const linkedNote = shaca.getNote(noteId);
if (linkedNote) {
linkEl.setAttribute("href", linkedNote.shareId);
linkEl.classList.add("type-" + linkedNote.type);
} else {
linkEl.removeAttribute("href");
}
}
}
content = document.body.innerHTML;
result.content = document.body.innerHTML;
if (content.includes(`<span class="math-tex">`)) {
header += `
if (result.content.includes(`<span class="math-tex">`)) {
result.header += `
<script src="../../${assetPath}/libraries/katex/katex.min.js"></script>
<link rel="stylesheet" href="../../${assetPath}/libraries/katex/katex.min.css">
<script src="../../${assetPath}/libraries/katex/auto-render.min.js"></script>
@@ -54,54 +89,58 @@ document.addEventListener("DOMContentLoaded", function() {
renderMathInElement(document.getElementById('content'));
});
</script>`;
}
}
if (note.hasLabel("shareIndex")) {
renderIndex(result);
}
}
else if (note.type === 'code') {
if (!content?.trim()) {
isEmpty = true;
}
else {
const document = new JSDOM().window.document;
}
const preEl = document.createElement('pre');
preEl.appendChild(document.createTextNode(content));
function renderCode(result) {
if (!result.content?.trim()) {
result.isEmpty = true;
} else {
const document = new JSDOM().window.document;
content = preEl.outerHTML;
}
const preEl = document.createElement('pre');
preEl.appendChild(document.createTextNode(result.content));
result.content = preEl.outerHTML;
}
else if (note.type === 'mermaid') {
content = `
<div class="mermaid">${content}</div>
}
function renderMermaid(result) {
result.content = `
<div class="mermaid">${result.content}</div>
<hr>
<details>
<summary>Chart source</summary>
<pre>${content}</pre>
<pre>${result.content}</pre>
</details>`
header += `<script src="../../${assetPath}/libraries/mermaid.min.js"></script>`;
result.header += `<script src="../../${assetPath}/libraries/mermaid.min.js"></script>`;
}
function renderImage(result, note) {
result.content = `<img src="api/images/${note.noteId}/${note.title}?${note.utcDateModified}">`;
}
function renderFile(note, result) {
if (note.mime === 'application/pdf') {
result.content = `<iframe class="pdf-view" src="api/notes/${note.noteId}/view"></iframe>`
} else {
result.content = `<button type="button" onclick="location.href='api/notes/${note.noteId}/download'">Download file</button>`;
}
else if (note.type === 'image') {
content = `<img src="api/images/${note.noteId}/${note.title}?${note.utcDateModified}">`;
}
else if (note.type === 'file') {
if (note.mime === 'application/pdf') {
content = `<iframe class="pdf-view" src="api/notes/${note.noteId}/view"></iframe>`
}
else {
content = `<button type="button" onclick="location.href='api/notes/${note.noteId}/download'">Download file</button>`;
}
}
else if (note.type === 'book') {
isEmpty = true;
}
else if (note.type === 'canvas') {
header += `<script>
}
function renderCanvas(result, note) {
result.header += `<script>
window.EXCALIDRAW_ASSET_PATH = window.location.origin + "/node_modules/@excalidraw/excalidraw/dist/";
</script>`;
header += `<script src="../../${assetPath}/node_modules/react/umd/react.production.min.js"></script>`;
header += `<script src="../../${assetPath}/node_modules/react-dom/umd/react-dom.production.min.js"></script>`;
header += `<script src="../../${assetPath}/node_modules/@excalidraw/excalidraw/dist/excalidraw.production.min.js"></script>`;
header += `<style>
result.header += `<script src="../../${assetPath}/node_modules/react/umd/react.production.min.js"></script>`;
result.header += `<script src="../../${assetPath}/node_modules/react-dom/umd/react-dom.production.min.js"></script>`;
result.header += `<script src="../../${assetPath}/node_modules/@excalidraw/excalidraw/dist/excalidraw.production.min.js"></script>`;
result.header += `<style>
.excalidraw-wrapper {
height: 100%;
@@ -115,26 +154,16 @@ document.addEventListener("DOMContentLoaded", function() {
}
</style>`;
content = `<div>
result.content = `<div>
<script>
const {elements, appState, files} = JSON.parse(${JSON.stringify(content)});
const {elements, appState, files} = JSON.parse(${JSON.stringify(result.content)});
window.triliumExcalidraw = {elements, appState, files}
</script>
<div id="excalidraw-app"></div>
<hr>
<a href="api/images/${note.noteId}/${note.title}?utc=${note.utcDateModified}">Get Image Link</a>
<a href="api/images/${note.noteId}/${note.escapedTitle}?utc=${note.utcDateModified}">Get Image Link</a>
<script src="./canvas_share.js"></script>
</div>`;
}
else {
content = '<p>This note type cannot be displayed.</p>';
}
return {
header,
content,
isEmpty
};
}
module.exports = {