diff --git a/src/share/content_renderer.js b/src/share/content_renderer.ts similarity index 82% rename from src/share/content_renderer.js rename to src/share/content_renderer.ts index 20d8b0b7f..cb111008a 100644 --- a/src/share/content_renderer.js +++ b/src/share/content_renderer.ts @@ -1,10 +1,17 @@ -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'); +import { JSDOM } from "jsdom"; +import shaca = require('./shaca/shaca'); +import assetPath = require('../services/asset_path'); +import shareRoot = require('./share_root'); +import escapeHtml = require('escape-html'); +import SNote = require("./shaca/entities/snote"); -function getContent(note) { +interface Result { + header: string; + content: string | Buffer | undefined; + isEmpty: boolean; +} + +function getContent(note: SNote) { if (note.isProtected) { return { header: '', @@ -13,7 +20,7 @@ function getContent(note) { }; } - const result = { + const result: Result = { content: note.getContent(), header: '', isEmpty: false @@ -38,7 +45,7 @@ function getContent(note) { return result; } -function renderIndex(result) { +function renderIndex(result: Result) { result.content += ''; } -function renderText(result, note) { +function renderText(result: Result, note: SNote) { const document = new JSDOM(result.content || "").window.document; - result.isEmpty = document.body.textContent.trim().length === 0 + result.isEmpty = document.body.textContent?.trim().length === 0 && document.querySelectorAll("img").length === 0; if (!result.isEmpty) { @@ -89,7 +96,9 @@ function renderText(result, note) { if (linkedNote) { const isExternalLink = linkedNote.hasLabel("shareExternalLink"); const href = isExternalLink ? linkedNote.getLabelValue("shareExternalLink") : `./${linkedNote.shareId}`; - linkEl.setAttribute("href", href); + if (href) { + linkEl.setAttribute("href", href); + } if (isExternalLink) { linkEl.setAttribute("target", "_blank"); linkEl.setAttribute("rel", "noopener noreferrer"); @@ -122,8 +131,8 @@ document.addEventListener("DOMContentLoaded", function() { } } -function renderCode(result) { - if (!result.content?.trim()) { +function renderCode(result: Result) { + if (typeof result.content !== "string" || !result.content?.trim()) { result.isEmpty = true; } else { const document = new JSDOM().window.document; @@ -135,7 +144,11 @@ function renderCode(result) { } } -function renderMermaid(result, note) { +function renderMermaid(result: Result, note: SNote) { + if (typeof result.content !== "string") { + return; + } + result.content = `
@@ -145,11 +158,11 @@ function renderMermaid(result, note) { ` } -function renderImage(result, note) { +function renderImage(result: Result, note: SNote) { result.content = ``; } -function renderFile(note, result) { +function renderFile(note: SNote, result: Result) { if (note.mime === 'application/pdf') { result.content = `` } else { @@ -157,6 +170,6 @@ function renderFile(note, result) { } } -module.exports = { +export = { getContent }; diff --git a/src/share/routes.js b/src/share/routes.js index 8bbb106c4..719fa9368 100644 --- a/src/share/routes.js +++ b/src/share/routes.js @@ -6,7 +6,7 @@ const ejs = require("ejs"); const shaca = require('./shaca/shaca'); const shacaLoader = require('./shaca/shaca_loader'); const shareRoot = require('./share_root'); -const contentRenderer = require('./content_renderer.js'); +const contentRenderer = require('./content_renderer'); const assetPath = require('../services/asset_path'); const appPath = require('../services/app_path'); const searchService = require('../services/search/services/search'); diff --git a/src/share/shaca/entities/sattachment.ts b/src/share/shaca/entities/sattachment.ts index cb86f1106..c8d58cd17 100644 --- a/src/share/shaca/entities/sattachment.ts +++ b/src/share/shaca/entities/sattachment.ts @@ -10,7 +10,7 @@ class SAttachment extends AbstractShacaEntity { private attachmentId: string; private ownerId: string; title: string; - private role: string; + role: string; private mime: string; private blobId: string; /** used for caching of images */ diff --git a/src/share/shaca/entities/snote.ts b/src/share/shaca/entities/snote.ts index 50b03ec42..5df224af5 100644 --- a/src/share/shaca/entities/snote.ts +++ b/src/share/shaca/entities/snote.ts @@ -18,11 +18,11 @@ const isCredentials = (attr: SAttribute) => attr.type === 'label' && attr.name = class SNote extends AbstractShacaEntity { noteId: string; private title: string; - private type: string; - private mime: string; + type: string; + mime: string; private blobId: string; - private utcDateModified: string; - private isProtected: boolean; + utcDateModified: string; + isProtected: boolean; parentBranches: SBranch[]; parents: SNote[]; children: SNote[];