const {JSDOM} = require("jsdom"); const shaca = require("./shaca/shaca"); function getContent(note) { let content = note.getContent(); let header = ''; let isEmpty = false; if (note.type === 'text') { const document = new JSDOM(content || "").window.document; isEmpty = document.body.textContent.trim().length === 0 && document.querySelectorAll("img").length === 0; if (!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; if (content.includes(``)) { header += ` `; } } } 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)); content = preEl.outerHTML; } } else if (note.type === 'mermaid') { content = `
${content}

Chart source
${content}
` header += ``; } else if (note.type === 'image') { content = ``; } else if (note.type === 'file') { if (note.mime === 'application/pdf') { content = `` } else { content = ``; } } else if (note.type === 'book') { isEmpty = true; } else { content = '

This note type cannot be displayed.

'; } return { header, content, isEmpty }; } module.exports = { getContent };