Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
chore(deps): drop unescape, inline the HTML entity decoder
Elian Doranb5e931818e02
diff --git a/apps/server/src/types.d.ts b/apps/server/src/types.d.tsindex 52c5588c9d..a320ad431a 100644--- a/apps/server/src/types.d.ts+++ b/apps/server/src/types.d.ts@@ -3,11 +3,6 @@* in its library or in `@types/*` packages.*/-declare module "unescape" {- function unescape(str: string, type?: string): string;- export default unescape;-}-declare module "normalize-strings" {function normalizeString(string: string): string;export default normalizeString;diff --git a/packages/trilium-core/package.json b/packages/trilium-core/package.jsonindex 366965aff2..c26fefb894 100644--- a/packages/trilium-core/package.json+++ b/packages/trilium-core/package.json@@ -19,8 +19,7 @@"sanitize-filename": "1.6.4","sanitize-html": "2.17.4","strip-bom": "5.0.0",- "sucrase": "3.35.1",- "unescape": "1.0.1"+ "sucrase": "3.35.1"},"devDependencies": {"@types/escape-html": "1.0.4",diff --git a/packages/trilium-core/src/services/utils/index.spec.ts b/packages/trilium-core/src/services/utils/index.spec.tsindex aedec02266..5c46b4b0ae 100644--- a/packages/trilium-core/src/services/utils/index.spec.ts+++ b/packages/trilium-core/src/services/utils/index.spec.ts@@ -142,8 +142,30 @@ describe("#escapeHtml", () => {});describe("#unescapeHtml", () => {- it("should re-export 'unescape' npm module as unescapeHtml", () => {- expect(utils.unescapeHtml).toBeTypeOf("function");+ it("decodes the five default HTML entities (named and numeric short forms)", () => {+ expect(utils.unescapeHtml("a & b")).toBe("a & b");+ expect(utils.unescapeHtml("&")).toBe("&");+ expect(utils.unescapeHtml("x < y > z")).toBe("x < y > z");+ expect(utils.unescapeHtml("< >")).toBe("< >");+ expect(utils.unescapeHtml(""hi" "")).toBe("\"hi\" \"");+ expect(utils.unescapeHtml("it's '")).toBe("it's '");+ });++ it("leaves entities outside the default set untouched", () => {+ // other numeric, hex, nbsp, unknown-named and wrong-case are NOT decoded+ expect(utils.unescapeHtml("© © & … &"))+ .toBe("© © & … &");+ });++ it("handles double-escaped entities and query-string URLs", () => {+ expect(utils.unescapeHtml("&amp;")).toBe("&");+ expect(utils.unescapeHtml("http://x/?a=1&b=2")).toBe("http://x/?a=1&b=2");+ });++ it("returns an empty string for empty or non-string input", () => {+ expect(utils.unescapeHtml("")).toBe("");+ expect(utils.unescapeHtml(null as unknown as string)).toBe("");+ expect(utils.unescapeHtml(undefined as unknown as string)).toBe("");});});diff --git a/packages/trilium-core/src/services/utils/index.ts b/packages/trilium-core/src/services/utils/index.tsindex 78a2ede2b2..1389f54908 100644--- a/packages/trilium-core/src/services/utils/index.ts+++ b/packages/trilium-core/src/services/utils/index.ts@@ -4,7 +4,6 @@ import { sanitizeFileName } from "../sanitizer";import { encodeBase64 } from "./binary";import { extensions as mimeToExt, types as extToMime } from "mime-types";import escape from "escape-html";-import unescape from "unescape";import { basename, extname } from "./path";import { NoteMeta } from "../../meta";@@ -217,7 +216,30 @@ export function toMap<T extends Record<string, any>>(list: T[], key: keyof T) {export const escapeHtml = escape;-export const unescapeHtml = unescape;+/**+ * Decodes the five HTML entities (and their numeric short forms) that the+ * former `unescape` npm dependency handled in its default mode: `&`, `<`, `>`,+ * `"` and `'`. Entities outside this set (other numeric/hex codes, named+ * entities such as ` `/`©`) are intentionally left untouched, and+ * non-string input yields an empty string — matching the previous behavior+ * exactly, since this backs the public `api.unescapeHtml` script API.+ */+const HTML_ENTITY_REPLACEMENTS: Record<string, string> = {+ """: "\"", """: "\"",+ "'": "'", "'": "'",+ "&": "&", "&": "&",+ ">": ">", ">": ">",+ "<": "<", "<": "<"+};++const HTML_ENTITY_RE = /&(?:quot|apos|amp|gt|lt|#34|#39|#38|#62|#60);/g;++export function unescapeHtml(str: string): string {+ if (!str || typeof str !== "string") {+ return "";+ }+ return str.replace(HTML_ENTITY_RE, (entity) => HTML_ENTITY_REPLACEMENTS[entity]);+}export function randomSecureToken(bytes = 32) {return encodeBase64(getCrypto().randomBytes(bytes));diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex be972243d8..88e3370793 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1706,9 +1706,6 @@ importers:sucrase:specifier: 3.35.1version: 3.35.1- unescape:- specifier: 1.0.1- version: 1.0.1devDependencies:'@types/escape-html':specifier: 1.0.4@@ -8482,10 +8479,6 @@ packages:resolution: {integrity: sha512-yblEwXAbGv1VQDmow7s38W77hzAgJAO50ztBLMcUyUBfxv1HC+LGwtiEN+Co6LtlqT/5uwVOxsD4TNIilWhwdQ==}engines: {node: '>=4'}- resolution: {integrity: sha512-zCnTtlxNoAiDc3gqY2aYAWFx7XWWiasuF2K8Me5WbN8otHKTUKBwjPtNpRs/rbUZm7KxWAaNj7P1a/p52GbVug==}- engines: {node: '>=0.10.0'}-resolution: {integrity: sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==}@@ -9399,10 +9392,6 @@ packages:engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}hasBin: true- resolution: {integrity: sha512-5BMULNob1vgFX6EjQw5izWDxrecWK9AM72rugNr0TFldMOi0fj6Jk+zeKIt0xGj4cEfQIJth4w3OKWOJ4f+AFw==}- engines: {node: '>=0.10.0'}-resolution: {integrity: sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==}engines: {node: '>=0.10.0'}@@ -12907,10 +12896,6 @@ packages:resolution: {integrity: sha512-xXnp4kTyor2Zq+J1FfPI6Eq3ew5h6Vl0F/8d9XU5zZQf1tX9s2Su1/3PiMmUANFULpmksxkClamIZcaUqryHsQ==}engines: {node: '>=20.18.1'}- resolution: {integrity: sha512-O0+af1Gs50lyH1nUu3ZyYS1cRh01Q/kUKatTOkSs7jukXE6/NebucDVxyiDsA9AQ4JC1V1jUH9EO8JX2nMDgGQ==}- engines: {node: '>=0.10.0'}-resolution: {integrity: sha512-jDrNnXWHd4oHiTZnx/ZG7gtUTVp+gCcTTKr8L0HjlwphROEW3+Him+IpvC+xcJEFegapiMZyZe02CyuOnRmbnQ==}engines: {node: '>=4'}@@ -23036,10 +23021,6 @@ snapshots:ext-list: 2.2.2sort-keys-length: 1.0.1- dependencies:- is-extendable: 0.1.1-@@ -24097,8 +24078,6 @@ snapshots:- [email protected]: {}-@@ -28112,10 +28091,6 @@ snapshots:- dependencies:- extend-shallow: 2.0.1-