Skip to content

Build your personal knowledge base with Trilium Notes

server-ts: Port share/shaca/content_renderer

Elian Doran88aba1c8448a
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 20d8b0b7f6..cb111008a0 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");
+
+interface Result {
+ header: string;
+ content: string | Buffer | undefined;
+ isEmpty: boolean;
+}
-function getContent(note) {
+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 += '<ul id="index">';
const rootNote = shaca.getNote(shareRoot.SHARE_ROOT_NOTE_ID);
@@ -53,10 +60,10 @@ function renderIndex(result) {
result.content += '</ul>';
}
-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 = `
<img src="api/images/${note.noteId}/${note.encodedTitle}?${note.utcDateModified}">
<hr>
@@ -145,11 +158,11 @@ function renderMermaid(result, note) {
</details>`
}
-function renderImage(result, note) {
+function renderImage(result: Result, note: SNote) {
result.content = `<img src="api/images/${note.noteId}/${note.encodedTitle}?${note.utcDateModified}">`;
}
-function renderFile(note, result) {
+function renderFile(note: SNote, result: Result) {
if (note.mime === 'application/pdf') {
result.content = `<iframe class="pdf-view" src="api/notes/${note.noteId}/view"></iframe>`
} 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 8bbb106c42..719fa93685 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 cb86f11069..c8d58cd17a 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 50b03ec421..5df224af5b 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[];