server-ts: Port share/shaca/content_renderer

This commit is contained in:
Elian Doran 2024-04-09 22:58:24 +03:00
parent 7c76d28f75
commit 88aba1c844
No known key found for this signature in database
4 changed files with 36 additions and 23 deletions

View File

@ -1,10 +1,17 @@
const { JSDOM } = require("jsdom"); import { JSDOM } from "jsdom";
const shaca = require('./shaca/shaca'); import shaca = require('./shaca/shaca');
const assetPath = require('../services/asset_path'); import assetPath = require('../services/asset_path');
const shareRoot = require('./share_root'); import shareRoot = require('./share_root');
const escapeHtml = require('escape-html'); 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) { if (note.isProtected) {
return { return {
header: '', header: '',
@ -13,7 +20,7 @@ function getContent(note) {
}; };
} }
const result = { const result: Result = {
content: note.getContent(), content: note.getContent(),
header: '', header: '',
isEmpty: false isEmpty: false
@ -38,7 +45,7 @@ function getContent(note) {
return result; return result;
} }
function renderIndex(result) { function renderIndex(result: Result) {
result.content += '<ul id="index">'; result.content += '<ul id="index">';
const rootNote = shaca.getNote(shareRoot.SHARE_ROOT_NOTE_ID); const rootNote = shaca.getNote(shareRoot.SHARE_ROOT_NOTE_ID);
@ -53,10 +60,10 @@ function renderIndex(result) {
result.content += '</ul>'; result.content += '</ul>';
} }
function renderText(result, note) { function renderText(result: Result, note: SNote) {
const document = new JSDOM(result.content || "").window.document; 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; && document.querySelectorAll("img").length === 0;
if (!result.isEmpty) { if (!result.isEmpty) {
@ -89,7 +96,9 @@ function renderText(result, note) {
if (linkedNote) { if (linkedNote) {
const isExternalLink = linkedNote.hasLabel("shareExternalLink"); const isExternalLink = linkedNote.hasLabel("shareExternalLink");
const href = isExternalLink ? linkedNote.getLabelValue("shareExternalLink") : `./${linkedNote.shareId}`; const href = isExternalLink ? linkedNote.getLabelValue("shareExternalLink") : `./${linkedNote.shareId}`;
if (href) {
linkEl.setAttribute("href", href); linkEl.setAttribute("href", href);
}
if (isExternalLink) { if (isExternalLink) {
linkEl.setAttribute("target", "_blank"); linkEl.setAttribute("target", "_blank");
linkEl.setAttribute("rel", "noopener noreferrer"); linkEl.setAttribute("rel", "noopener noreferrer");
@ -122,8 +131,8 @@ document.addEventListener("DOMContentLoaded", function() {
} }
} }
function renderCode(result) { function renderCode(result: Result) {
if (!result.content?.trim()) { if (typeof result.content !== "string" || !result.content?.trim()) {
result.isEmpty = true; result.isEmpty = true;
} else { } else {
const document = new JSDOM().window.document; 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 = ` result.content = `
<img src="api/images/${note.noteId}/${note.encodedTitle}?${note.utcDateModified}"> <img src="api/images/${note.noteId}/${note.encodedTitle}?${note.utcDateModified}">
<hr> <hr>
@ -145,11 +158,11 @@ function renderMermaid(result, note) {
</details>` </details>`
} }
function renderImage(result, note) { function renderImage(result: Result, note: SNote) {
result.content = `<img src="api/images/${note.noteId}/${note.encodedTitle}?${note.utcDateModified}">`; 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') { if (note.mime === 'application/pdf') {
result.content = `<iframe class="pdf-view" src="api/notes/${note.noteId}/view"></iframe>` result.content = `<iframe class="pdf-view" src="api/notes/${note.noteId}/view"></iframe>`
} else { } else {
@ -157,6 +170,6 @@ function renderFile(note, result) {
} }
} }
module.exports = { export = {
getContent getContent
}; };

View File

@ -6,7 +6,7 @@ const ejs = require("ejs");
const shaca = require('./shaca/shaca'); const shaca = require('./shaca/shaca');
const shacaLoader = require('./shaca/shaca_loader'); const shacaLoader = require('./shaca/shaca_loader');
const shareRoot = require('./share_root'); const shareRoot = require('./share_root');
const contentRenderer = require('./content_renderer.js'); const contentRenderer = require('./content_renderer');
const assetPath = require('../services/asset_path'); const assetPath = require('../services/asset_path');
const appPath = require('../services/app_path'); const appPath = require('../services/app_path');
const searchService = require('../services/search/services/search'); const searchService = require('../services/search/services/search');

View File

@ -10,7 +10,7 @@ class SAttachment extends AbstractShacaEntity {
private attachmentId: string; private attachmentId: string;
private ownerId: string; private ownerId: string;
title: string; title: string;
private role: string; role: string;
private mime: string; private mime: string;
private blobId: string; private blobId: string;
/** used for caching of images */ /** used for caching of images */

View File

@ -18,11 +18,11 @@ const isCredentials = (attr: SAttribute) => attr.type === 'label' && attr.name =
class SNote extends AbstractShacaEntity { class SNote extends AbstractShacaEntity {
noteId: string; noteId: string;
private title: string; private title: string;
private type: string; type: string;
private mime: string; mime: string;
private blobId: string; private blobId: string;
private utcDateModified: string; utcDateModified: string;
private isProtected: boolean; isProtected: boolean;
parentBranches: SBranch[]; parentBranches: SBranch[];
parents: SNote[]; parents: SNote[];
children: SNote[]; children: SNote[];