Skip to content

Build your personal knowledge base with Trilium Notes

chore(deps): drop html2plaintext for node-html-parser

Elian Doranc9f265b4f5ba
diff --git a/apps/server/package.json b/apps/server/package.json
index 3645e04f13..2a277143da 100644
--- a/apps/server/package.json
+++ b/apps/server/package.json
@@ -87,7 +87,6 @@
"file-uri-to-path": "2.0.0",
"helmet": "8.2.0",
"html": "1.0.0",
- "html2plaintext": "2.1.4",
"http-proxy-agent": "9.0.0",
"https-proxy-agent": "9.0.0",
"i18next-fs-backend": "2.6.6",
diff --git a/apps/server/src/types.d.ts b/apps/server/src/types.d.ts
index 8d6b3c993a..52c5588c9d 100644
--- a/apps/server/src/types.d.ts
+++ b/apps/server/src/types.d.ts
@@ -8,11 +8,6 @@ declare module "unescape" {
export default unescape;
}
-declare module "html2plaintext" {
- function html2plaintext(htmlText: string): string;
- export default html2plaintext;
-}
-
declare module "normalize-strings" {
function normalizeString(string: string): string;
export default normalizeString;
diff --git a/packages/trilium-core/src/services/notes.spec.ts b/packages/trilium-core/src/services/notes.spec.ts
index f405db4132..95ed523ed9 100644
--- a/packages/trilium-core/src/services/notes.spec.ts
+++ b/packages/trilium-core/src/services/notes.spec.ts
@@ -4,7 +4,7 @@ import becca from "../becca/becca.js";
import type BBranch from "../becca/entities/bbranch.js";
import type BNote from "../becca/entities/bnote.js";
import { getContext } from "./context.js";
-import noteService, { saveLinks } from "./notes.js";
+import noteService, { prepareTitle, saveLinks } from "./notes.js";
import optionService from "./options.js";
import { getSql } from "./sql/index.js";
@@ -331,3 +331,36 @@ describe("notes service (real DB)", () => {
});
});
});
+
+/**
+ * Characterization of the attachment-title derivation used by saveAttachments
+ * (the inner label of an inline base64 `<a>` link → a plain-text title). Locks
+ * the behavior so the underlying HTML-to-text implementation can be swapped
+ * without regressing real-world inputs (filenames, entities, inline markup).
+ */
+describe("prepareTitle", () => {
+ it("decodes HTML entities (named, decimal, hex)", () => {
+ expect(prepareTitle("a &amp; b.txt")).toBe("a & b.txt");
+ expect(prepareTitle("caf&eacute;.pdf")).toBe("café.pdf");
+ expect(prepareTitle("&copy; report.docx")).toBe("© report.docx");
+ expect(prepareTitle("price &lt; 100 &gt; 50.csv")).toBe("price < 100 > 50.csv");
+ expect(prepareTitle("emoji &#x1F600; file.png")).toBe("emoji 😀 file.png");
+ });
+
+ it("strips inline tags, keeping their text", () => {
+ expect(prepareTitle("<b>bold</b> name.jpg")).toBe("bold name.jpg");
+ expect(prepareTitle('<span class="x">nested <i>tags</i></span>.svg')).toBe("nested tags.svg");
+ });
+
+ it("collapses runs of whitespace and trims", () => {
+ expect(prepareTitle("spaced out name.dat")).toBe("spaced out name.dat");
+ expect(prepareTitle("tab\tand\nnewline.bin")).toBe("tab and newline.bin");
+ expect(prepareTitle(" leading & trailing ")).toBe("leading & trailing");
+ });
+
+ it("passes plain filenames through unchanged and handles empty input", () => {
+ expect(prepareTitle("document.pdf")).toBe("document.pdf");
+ expect(prepareTitle("My File (1).png")).toBe("My File (1).png");
+ expect(prepareTitle("")).toBe("");
+ });
+});
diff --git a/packages/trilium-core/src/services/notes.ts b/packages/trilium-core/src/services/notes.ts
index 7f1be4c281..c304f8839d 100644
--- a/packages/trilium-core/src/services/notes.ts
+++ b/packages/trilium-core/src/services/notes.ts
@@ -1,6 +1,6 @@
import { type AttachmentRow, type AttributeRow, type BranchRow, dayjs, type NoteRow, type NoteType } from "@triliumnext/commons";
-import html2plaintext from "html2plaintext";
import { t } from "i18next";
+import { parse as parseHtml } from "node-html-parser";
import url from "url";
import becca from "../becca/becca.js";
@@ -843,6 +843,17 @@ function downloadImages(noteId: string, content: string) {
return content;
}
+/**
+ * Derives a plain-text attachment title from the inner HTML of an inline
+ * attachment's link label: strips tags, decodes HTML entities, collapses
+ * whitespace and trims.
+ */
+export function prepareTitle(html: string): string {
+ // `.text` strips tags and decodes HTML entities (via `he`); we then collapse
+ // whitespace and trim, matching the former `html2plaintext` behavior.
+ return parseHtml(html).text.replace(/\s+/g, " ").trim();
+}
+
function saveAttachments(note: BNote, content: string) {
const inlineAttachmentRe = /<a[^>]*?\shref=['"]data:([^;'">]+);base64,([^'">]+)['"][^>]*>(.*?)<\/a>/gim;
let attachmentMatch;
@@ -853,7 +864,7 @@ function saveAttachments(note: BNote, content: string) {
const base64data = attachmentMatch[2];
const buffer = decodeBase64(base64data);
- const title = html2plaintext(attachmentMatch[3]);
+ const title = prepareTitle(attachmentMatch[3]);
const attachment = note.saveAttachment({
role: "file",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 9deb4f9179..be972243d8 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -758,9 +758,6 @@ importers:
html:
specifier: 1.0.0
version: 1.0.0
- html2plaintext:
- specifier: 2.1.4
- version: 2.1.4
http-proxy-agent:
specifier: 9.0.0
version: 9.0.0
@@ -7122,16 +7119,9 @@ packages:
resolution: {integrity: sha512-PsezH1rqdV9VvyNhxxOW32/d75r01NY7TQCmOqomRo15ZSOKbpTFVsfjghxo6JloQUCGnH4k1LGu0R4yCLlWQQ==}
- resolution: {integrity: sha512-eq0GdBvxVFbqWgmCm7M3XGs1I8oLy/nExUnh6oLqmBditPO9AqQJrkslDpMun/hZ0yyTs8L0m85OHp4ho6Qm9g==}
-
resolution: {integrity: sha512-9v9kG0LvzrlcungtnJtpGNxY+fzECQKhK4EGJX2vByejiMX84MFNQw4UxPJl3bFbTMw+Dfs37XaIkCwTZfLh4g==}
- resolution: {integrity: sha512-g0J0q/O6mW8z5zxQ3A8E8J1hUgp4SMOvEoW/x84OwyHKe/Zccz83PVT4y5Crcr530FV6NgmKI1qvGTKVl9XXVw==}
- engines: {node: '>= 6'}
-
resolution: {integrity: sha512-WDrybc/gKFpTYQutKIK6UvfcuxijIZfMfXaYm8NMsPQxSYvf+13fXUJ4rztGGbJcBQ/GF55gvrZ0Bc0bj/mqvg==}
engines: {node: '>=20.18.1'}
@@ -7563,9 +7553,6 @@ packages:
resolution: {integrity: sha512-8HFEBPKhOpJPEPu70wJJetjKta86Gw9+CCyCnB3sui2qQfOvRyqBy4IKLKKAwdMpWb2lHXWk9Wb4Z6AmaUT1Pg==}
engines: {node: '>=12'}
- resolution: {integrity: sha512-wPpOYtnsVontu2mODhA19JrqWxNsfdatRKd64kmpRbQgh1KtItko5sTnEpPdpSaJszTOhEMlF/RPz28qj4HqhQ==}
-
resolution: {integrity: sha512-TizTzUddG/xYLA3NXodFM0fSbNizXjOKhqiQQwvhlspadZokn1KDy0NZFS0wuEubIYAV5/c1/lAr0TaaFXEXzw==}
@@ -7972,19 +7959,12 @@ packages:
resolution: {integrity: sha512-nRCa7CK3VTrM2NmGkIy4cbK7IZlgBE/PYMn55rrXefr5xXDP0LdtfPnblFDoVdcAfslJ7or6iqAUnx0CCGIWQA==}
- resolution: {integrity: sha512-VHwB3KfrcOOkelEG2ZOfxqLZdfkil8PtJi4P8N2MMXucZq2yLp75ClViUlOVwyoHEDjYU433Aq+5zWP61+RGag==}
-
resolution: {integrity: sha512-wIkAryiqt/nV5EQKqQpo3SToSOV9J0DnbJqwK7Wv/Trc92zIAYZ4FlMu+JPFW1DfGFt81ZTCGgDEabffXeLyJg==}
resolution: {integrity: sha512-OLETBj6w0OsagBwdXnPdN0cnMfF9opN69co+7ZrbfPGrdpPVNBUj02spi6B1N7wChLQiPn4CSH/zJvXw56gmHw==}
- resolution: {integrity: sha512-GrwoxYN+uWlzO8uhUXRl0P+kHE4GtVPfYzVLcUxPL7KNdHKj66vvlhiweIHqYYXWlw+T8iLMp42Lm67ghw4WMQ==}
- engines: {node: '>= 4'}
-
resolution: {integrity: sha512-cgwlv/1iFQiFnU96XXgROh8xTeetsnJiDsTc7TYCLFd9+/WNkIqPTxiM/8pSd8VIrhXGTf1Ny1q1hquVqDJB5w==}
engines: {node: '>= 4'}
@@ -7995,9 +7975,6 @@ packages:
resolution: {integrity: sha512-2jBxDJY4RR06tQNy4w5FlFH7kfxsQZlufd0sbv+chfHCxeJwrFw2baUDsSwvBISD4K4RDbd0PTfy3uNXsR6siA==}
- resolution: {integrity: sha512-w96Cjofp72M5IIhpjgobBimYEfoPjx1Vx0BSX9P30WBdZW2WIKU0T1Bd0kz2eNZ9ikjKgHbEyKx8BB6H1L3h3A==}
-
resolution: {integrity: sha512-6kZKyUajlDuqlHKVX1w7gyslj9MPIXzIFiz/rGu35uC1wMi+kMhQwGhl4lt9unC9Vb9INnY9Z3/ZA3+FhASLaw==}
@@ -8165,9 +8142,6 @@ packages:
resolution: {integrity: sha512-mLCNbrQli11K1ySUmuNt4ZUB3OpGIDq4q2vTBTf5cL2lpsRjI9QKqSD0ndjW8FyvcW/Jj46gMe9syyHAsvMa/A==}
engines: {node: '>=10.13.0'}
- resolution: {integrity: sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A==}
-
resolution: {integrity: sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==}
engines: {node: '>=0.12'}
@@ -9152,9 +9126,6 @@ packages:
resolution: {integrity: sha512-bEqo66MRXsUGxWHV5IP0PUiAWwoEjba4VCzg0LjFJBpchPaTfyfCKTG6bc5F8ucKec3q5y6qOdGyYTSBEvhCrg==}
- resolution: {integrity: sha512-kigGyxhh85E5SgKd3ilyKy9VTUgMTsEn3ex4cZcXBX47cACAWHgmOPbSVd05msM6RpOi5o+2nWrK5OhAQQWEsg==}
-
resolution: {integrity: sha512-lw/7YsdKiP3kk5PnR1INY17iJuzdAtJewxr14ozKJWbbR97znovZ0mh+WEMZ8rjc3lgTK+ID/htTjuyGKB52Kw==}
hasBin: true
@@ -9171,9 +9142,6 @@ packages:
resolution: {integrity: sha512-VTZkM9GWRAtEpveh7MSF6SjjrpNVNNVJfFup7xTY3UpFtm67foy9HDVXneLtFVt4pMz5kZtgNcvCniNFb1hlEQ==}
- resolution: {integrity: sha512-gyyPk6rgonLFEDGoeRgQNaEUvdJ4ktTmmUh/h2t7s+M8oPpIPxgNACWa+6ESR57kXstwqPiCut0V8NRpcwgU7A==}
-
resolution: {integrity: sha512-dTxcvPXqPvXBQpq5dUr6mEMJX4oIEFv6bwom3FDwKRDsuIjjJGANqhBuoAn9c1RQJIdAKav33ED65E2ys+87QQ==}
@@ -11108,18 +11076,12 @@ packages:
resolution: {integrity: sha512-/2qh0lav6CmI15FzA3i/2Bzk2zCgQhGMkvhOhKNcBVQ1ldgpbfiNTVslmooUmWJcADi1f1kIeynbDRVzNlfR6Q==}
- resolution: {integrity: sha512-qPuWvbLgvDGilKc5BoicRovlT4MtYT6JfJyBOMDsKoiT+GiuP5qyrPCnR9HcPECIJJmZh5jRndyNThnhhb/vlA==}
-
resolution: {integrity: sha512-ruw5xyKs6lrpo9x9rCZqZZnIUntICjQAd0Wsmp396Ul9lN/h+ifgVV1x1gZHi8euej6wTfpqX8j+BFQxF0NS/g==}
resolution: {integrity: sha512-JyeQc9iwFLn5TbvvqACIF/VXG6abODeB3Fwmv/TGdLk2LfbWkaySGY72at4+Ty7EkPZj854u4CrICqNk2qIbow==}
- resolution: {integrity: sha512-Ofn/CTFzRGTTxwpNEs9PP93gXShHcTq255nzRYSKe8AkVpZY7e1fpmTfOyoIvjP5HG7Z2ZM7VS9PPhQGW2pOpw==}
-
resolution: {integrity: sha512-IInvU7fabl34qmi9gY8XOVxhYyMyuH2xUNpb2q8/Y+7552KlejkRvqvD19nMoUW/uQGGbqNpA6Tufu5FL5BZgw==}
@@ -11271,9 +11233,6 @@ packages:
resolution: {integrity: sha512-ZIfcLJC+7E7FBFnDxm9MPmt7D+DidyQ26lewieO75AdhA2ayMtsJSES0iWzqJQbcVRSrTufQoy0DR94xHue0oA==}
engines: {node: '>=10.4.0'}
- resolution: {integrity: sha512-m0/luIuyx9EmvbT6284YVeMk4OFeyOYILL0/dOvBaiMlv9pTKMiMvX7mLvjoIje/PesdMfp/8ABV9x4x8reLHQ==}
-
resolution: {integrity: sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA==}
engines: {node: '>=4'}
@@ -21350,14 +21309,6 @@ snapshots:
- dependencies:
- css-select: 4.3.0
- css-what: 6.2.2
- domelementtype: 2.3.0
- domhandler: 4.3.1
- domutils: 2.8.0
-
dependencies:
boolbase: 1.0.0
@@ -21367,16 +21318,6 @@ snapshots:
domhandler: 5.0.3
domutils: 3.2.2
- dependencies:
- cheerio-select: 1.6.0
- dom-serializer: 1.4.1
- domhandler: 4.3.1
- htmlparser2: 6.1.0
- parse5: 6.0.1
- parse5-htmlparser2-tree-adapter: 6.0.1
- tslib: 2.8.1
-
dependencies:
cheerio-select: 2.1.0
@@ -21906,14 +21847,6 @@ snapshots:
- dependencies:
- boolbase: 1.0.0
- css-what: 6.2.2
- domhandler: 4.3.1
- domutils: 2.8.0
- nth-check: 2.1.1
-
dependencies:
boolbase: 1.0.0
@@ -22310,12 +22243,6 @@ snapshots:
'@babel/runtime': 7.29.2
csstype: 3.2.3
- dependencies:
- domelementtype: 2.3.0
- domhandler: 4.3.1
- entities: 2.2.0
-
dependencies:
domelementtype: 2.3.0
@@ -22324,10 +22251,6 @@ snapshots:
- dependencies:
- domelementtype: 2.3.0
-
dependencies:
domelementtype: 2.3.0
@@ -22340,12 +22263,6 @@ snapshots:
optionalDependencies:
'@types/trusted-types': 2.0.7
- dependencies:
- dom-serializer: 1.4.1
- domelementtype: 2.3.0
- domhandler: 4.3.1
-
dependencies:
dom-serializer: 2.0.0
@@ -22593,8 +22510,6 @@ snapshots:
graceful-fs: 4.2.11
tapable: 2.3.3
-
@@ -23897,12 +23812,6 @@ snapshots:
- dependencies:
- cheerio: 1.0.0-rc.10
- he: 1.2.0
- plumb: 0.1.0
-
dependencies:
concat-stream: 1.6.2
@@ -23925,13 +23834,6 @@ snapshots:
domutils: 3.2.2
entities: 7.0.1
- dependencies:
- domelementtype: 2.3.0
- domhandler: 4.3.1
- domutils: 2.8.0
- entities: 2.2.0
-
@@ -26072,10 +25974,6 @@ snapshots:
- dependencies:
- parse5: 6.0.1
-
dependencies:
domhandler: 5.0.3
@@ -26085,8 +25983,6 @@ snapshots:
dependencies:
parse5: 7.3.0
-
dependencies:
entities: 6.0.1
@@ -26228,8 +26124,6 @@ snapshots:
base64-js: 1.5.1
xmlbuilder: 15.1.1
-