const {JSDOM} = require("jsdom");
const shaca = require("./shaca/shaca");
const assetPath = require("../services/asset_path");
const shareRoot = require('./share_root');
const escapeHtml = require('escape-html');
function getContent(note) {
if (note.isProtected) {
return {
header: '',
content: '
Protected note cannot be displayed
',
isEmpty: false
};
}
const result = {
content: note.getContent(),
header: '',
isEmpty: false
};
if (note.type === 'text') {
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 = 'This note type cannot be displayed.
';
}
return result;
}
function renderIndex(result) {
result.content += '';
const rootNote = shaca.getNote(shareRoot.SHARE_ROOT_NOTE_ID);
for (const childNote of rootNote.getChildNotes()) {
result.content += `- ${childNote.escapedTitle}
`;
}
result.content += '
';
}
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");
}
}
}
result.content = document.body.innerHTML;
if (result.content.includes(``)) {
result.header += `
`;
}
if (note.hasLabel("shareIndex")) {
renderIndex(result);
}
}
}
function renderCode(result) {
if (!result.content?.trim()) {
result.isEmpty = true;
} else {
const document = new JSDOM().window.document;
const preEl = document.createElement('pre');
preEl.appendChild(document.createTextNode(result.content));
result.content = preEl.outerHTML;
}
}
function renderMermaid(result) {
result.content = `
${escapeHtml(result.content)}
Chart source
${escapeHtml(result.content)}
`
result.header += ``;
}
function renderImage(result, note) {
result.content = `
`;
}
function renderFile(note, result) {
if (note.mime === 'application/pdf') {
result.content = ``
} else {
result.content = ``;
}
}
function renderCanvas(result, note) {
result.header += ``;
result.header += ``;
result.header += ``;
result.header += ``;
result.header += ``;
result.content = ``;
}
module.exports = {
getContent
};