feat(server): replace jsdom

This commit is contained in:
Elian Doran
2025-09-28 19:43:04 +03:00
parent c0ea441c59
commit 7e79d907be
9 changed files with 54 additions and 61 deletions

View File

@@ -1,4 +1,4 @@
import { JSDOM } from "jsdom";
import { parse, HTMLElement } from "node-html-parser";
import shaca from "./shaca/shaca.js";
import assetPath from "../services/asset_path.js";
import shareRoot from "./share_root.js";
@@ -65,8 +65,8 @@ function renderIndex(result: Result) {
result.content += "</ul>";
}
export function renderText(result: Result, note: SNote) {
const document = new JSDOM(result.content || "").window.document;
function renderText(result: Result, note: SNote) {
const document = parse(result.content?.toString() || "");
// Process include notes.
for (const includeNoteEl of document.querySelectorAll("section.include-note")) {
@@ -79,11 +79,13 @@ export function renderText(result: Result, note: SNote) {
const includedResult = getContent(note);
if (typeof includedResult.content !== "string") continue;
const includedDocument = new JSDOM(includedResult.content).window.document;
includeNoteEl.replaceWith(...includedDocument.body.childNodes);
const includedDocument = parse(includedResult.content).querySelector("body");
if (includedDocument) {
includeNoteEl.replaceWith(includedDocument);
}
}
result.isEmpty = document.body.textContent?.trim().length === 0 && document.querySelectorAll("img").length === 0;
result.isEmpty = document.querySelector("body")?.textContent?.trim().length === 0 && document.querySelectorAll("img").length === 0;
if (!result.isEmpty) {
for (const linkEl of document.querySelectorAll("a")) {
@@ -99,7 +101,7 @@ export function renderText(result: Result, note: SNote) {
}
}
result.content = document.body.innerHTML;
result.content = document.querySelector("body")?.innerHTML ?? "";
if (result.content.includes(`<span class="math-tex">`)) {
result.header += `
@@ -120,7 +122,7 @@ document.addEventListener("DOMContentLoaded", function() {
}
}
function handleAttachmentLink(linkEl: HTMLAnchorElement, href: string) {
function handleAttachmentLink(linkEl: HTMLElement, href: string) {
const linkRegExp = /attachmentId=([a-zA-Z0-9_]+)/g;
let attachmentMatch;
if ((attachmentMatch = linkRegExp.exec(href))) {
@@ -131,7 +133,8 @@ function handleAttachmentLink(linkEl: HTMLAnchorElement, href: string) {
linkEl.setAttribute("href", `api/attachments/${attachmentId}/download`);
linkEl.classList.add(`attachment-link`);
linkEl.classList.add(`role-${attachment.role}`);
linkEl.innerText = attachment.title;
linkEl.childNodes.length = 0;
linkEl.append(attachment.title);
} else {
linkEl.removeAttribute("href");
}
@@ -164,11 +167,8 @@ export function renderCode(result: Result) {
if (typeof result.content !== "string" || !result.content?.trim()) {
result.isEmpty = true;
} else {
const document = new JSDOM().window.document;
const preEl = document.createElement("pre");
preEl.appendChild(document.createTextNode(result.content));
const preEl = new HTMLElement("pre", {});
preEl.append(result.content);
result.content = preEl.outerHTML;
}
}