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(core): integrate escape/unescape & toMap
Elian Doran78262e55ec02
diff --git a/apps/server/package.json b/apps/server/package.jsonindex f16b11ec74..8c6765924d 100644--- a/apps/server/package.json+++ b/apps/server/package.json@@ -49,8 +49,7 @@"@types/compression": "1.8.1","@types/cookie-parser": "1.4.10","@types/debounce": "1.2.4",- "@types/ejs": "3.1.5",- "@types/escape-html": "1.0.4",+ "@types/ejs": "3.1.5","@types/express-http-proxy": "1.6.7","@types/express-session": "1.18.2","@types/fs-extra": "11.0.4",@@ -84,8 +83,7 @@"ejs": "3.1.10","electron": "39.2.7","electron-debug": "4.1.0",- "electron-window-state": "5.0.3",- "escape-html": "1.0.3",+ "electron-window-state": "5.0.3","express": "5.2.1","express-http-proxy": "2.1.2","express-openid-connect": "2.19.3",@@ -123,7 +121,6 @@"time2fa": "1.4.2","tmp": "0.2.5","turndown": "7.2.2",- "unescape": "1.0.1","vite": "7.3.0","ws": "8.18.3","xml2js": "0.6.2",diff --git a/apps/server/src/services/utils.ts b/apps/server/src/services/utils.tsindex ee1e18302b..861a8396fc 100644--- a/apps/server/src/services/utils.ts+++ b/apps/server/src/services/utils.ts@@ -3,12 +3,10 @@import { utils as coreUtils } from "@triliumnext/core";import chardet from "chardet";import crypto from "crypto";-import escape from "escape-html";import { t } from "i18next";import { release as osRelease } from "os";import path from "path";import stripBom from "strip-bom";-import unescape from "unescape";import log from "./log.js";import type NoteMeta from "./meta/note_meta.js";@@ -102,10 +100,6 @@ export function sanitizeSqlIdentifier(str: string) {return str.replace(/[^A-Za-z0-9_]/g, "");}-export const escapeHtml = escape;--export const unescapeHtml = unescape;-export function toObject<T, K extends string | number | symbol, V>(array: T[], fn: (item: T) => [K, V]): Record<K, V> {const obj: Record<K, V> = {} as Record<K, V>; // TODO: unsafe?@@ -227,16 +221,9 @@ export function normalize(str: string) {return coreUtils.normalize(str);}+/** @deprecated */export function toMap<T extends Record<string, any>>(list: T[], key: keyof T) {- const map = new Map<string, T>();- for (const el of list) {- const keyForMap = el[key];- if (!keyForMap) continue;- // TriliumNextTODO: do we need to handle the case when the same key is used?- // currently this will overwrite the existing entry in the map- map.set(keyForMap, el);- }- return map;+ return coreUtils.toMap(list, key);}// try to turn 'true' and 'false' strings from process.env variables into boolean values or undefined@@ -470,6 +457,9 @@ function slugify(text: string) {.replace(/(^-|-$)+/g, ""); // trim dashes}+export const escapeHtml = coreUtils.escapeHtml;+export const unescapeHtml = coreUtils.unescapeHtml;+export default {compareVersions,constantTimeCompare,diff --git a/packages/trilium-core/package.json b/packages/trilium-core/package.jsonindex 147aaa0ef2..fd2f90a18c 100644--- a/packages/trilium-core/package.json+++ b/packages/trilium-core/package.json@@ -11,10 +11,13 @@"sanitize-html": "2.17.0","@braintree/sanitize-url": "7.1.1","sanitize-filename": "1.6.3",- "mime-types": "3.0.2"+ "mime-types": "3.0.2",+ "unescape": "1.0.1",+ "escape-html": "1.0.3"},"devDependencies": {"@types/sanitize-html": "2.16.0",- "@types/mime-types": "3.0.1"+ "@types/mime-types": "3.0.1",+ "@types/escape-html": "1.0.4"}}diff --git a/packages/trilium-core/src/services/utils/index.ts b/packages/trilium-core/src/services/utils/index.tsindex 6cd7c7f06e..f33ccb0391 100644--- a/packages/trilium-core/src/services/utils/index.ts+++ b/packages/trilium-core/src/services/utils/index.ts@@ -2,6 +2,8 @@ import { getCrypto } from "../encryption/crypto";import { sanitizeFileName } from "../sanitizer";import { encodeBase64 } from "./binary";import mimeTypes from "mime-types";+import escape from "escape-html";+import unescape from "unescape";// render and book are string note in the sense that they are expected to contain empty stringconst STRING_NOTE_TYPES = new Set(["text", "code", "relationMap", "search", "render", "book", "mermaid", "canvas", "webView"]);@@ -99,3 +101,19 @@ export function formatDownloadTitle(fileName: string, type: string | null, mime:return `${fileNameBase}${getExtension()}`;}++export function toMap<T extends Record<string, any>>(list: T[], key: keyof T) {+ const map = new Map<string, T>();+ for (const el of list) {+ const keyForMap = el[key];+ if (!keyForMap) continue;+ // TriliumNextTODO: do we need to handle the case when the same key is used?+ // currently this will overwrite the existing entry in the map+ map.set(keyForMap, el);+ }+ return map;+}++export const escapeHtml = escape;++export const unescapeHtml = unescape;