Notes/src/share/content_renderer.js

99 lines
3.0 KiB
JavaScript
Raw Normal View History

2021-12-22 09:10:38 +01:00
const {JSDOM} = require("jsdom");
2021-12-22 10:57:02 +01:00
const shaca = require("./shaca/shaca");
2021-12-22 09:10:38 +01:00
function getContent(note) {
let content = note.getContent();
2021-12-27 20:48:14 +01:00
let header = '';
let isEmpty = false;
2021-12-22 09:10:38 +01:00
if (note.type === 'text') {
const document = new JSDOM(content || "").window.document;
2021-12-27 20:48:14 +01:00
isEmpty = document.body.textContent.trim().length === 0
2021-12-22 09:10:38 +01:00
&& document.querySelectorAll("img").length === 0;
2021-12-27 20:48:14 +01:00
if (!isEmpty) {
2021-12-22 09:10:38 +01:00
for (const linkEl of document.querySelectorAll("a")) {
const href = linkEl.getAttribute("href");
if (href?.startsWith("#")) {
const notePathSegments = href.split("/");
2021-12-22 10:57:02 +01:00
const noteId = notePathSegments[notePathSegments.length - 1];
const linkedNote = shaca.getNote(noteId);
if (linkedNote) {
linkEl.setAttribute("href", linkedNote.shareId);
2021-12-27 20:48:14 +01:00
linkEl.classList.add("type-" + linkedNote.type);
2021-12-22 10:57:02 +01:00
}
else {
linkEl.removeAttribute("href");
}
2021-12-22 09:10:38 +01:00
}
}
content = document.body.innerHTML;
2021-12-27 20:48:14 +01:00
if (content.includes(`<span class="math-tex">`)) {
2021-12-27 20:48:14 +01:00
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>`;
}
2021-12-22 09:10:38 +01:00
}
}
else if (note.type === 'code') {
2021-12-22 09:10:38 +01:00
if (!content?.trim()) {
2021-12-27 20:48:14 +01:00
isEmpty = true;
2021-12-22 09:10:38 +01:00
}
else {
2021-12-27 20:48:14 +01:00
const document = new JSDOM().window.document;
const preEl = document.createElement('pre');
preEl.appendChild(document.createTextNode(content));
content = preEl.outerHTML;
2021-12-22 09:10:38 +01:00
}
}
else if (note.type === 'mermaid') {
2021-12-27 20:48:14 +01:00
content = `
<div class="mermaid">${content}</div>
<hr>
<details>
<summary>Chart source</summary>
<pre>${content}</pre>
</details>`
header += `<script src="../../libraries/mermaid.min.js"></script>`;
}
2021-12-22 09:10:38 +01:00
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') {
2021-12-24 22:46:55 +01:00
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>`;
}
2021-12-22 09:10:38 +01:00
}
2021-12-27 20:48:14 +01:00
else if (note.type === 'book') {
isEmpty = true;
}
2021-12-22 09:10:38 +01:00
else {
content = '<p>This note type cannot be displayed.</p>';
2021-12-22 09:10:38 +01:00
}
2021-12-24 22:46:55 +01:00
2021-12-27 20:48:14 +01:00
return {
header,
content,
isEmpty
};
2021-12-22 09:10:38 +01:00
}
module.exports = {
getContent
};