Skip to content

Build your personal knowledge base with Trilium Notes

chore(core): integrate sanitize_attribute_name

Elian Doran61f6f9429531
diff --git a/apps/server/src/routes/api/sender.ts b/apps/server/src/routes/api/sender.ts
index efdf6817a5..a4c8348464 100644
--- a/apps/server/src/routes/api/sender.ts
+++ b/apps/server/src/routes/api/sender.ts
@@ -1,9 +1,9 @@
+import { utils } from "@triliumnext/core";
import type { Request } from "express";
import imageType from "image-type";
import imageService from "../../services/image.js";
import noteService from "../../services/notes.js";
-import sanitizeAttributeName from "../../services/sanitize_attribute_name.js";
import specialNotesService from "../../services/special_notes.js";
async function uploadImage(req: Request) {
@@ -43,14 +43,14 @@ async function uploadImage(req: Request) {
const labels = JSON.parse(labelsStr);
for (const { name, value } of labels) {
- note.setLabel(sanitizeAttributeName(name), value);
+ note.setLabel(utils.sanitizeAttributeName(name), value);
}
}
note.setLabel("sentFromSender");
return {
- noteId: noteId
+ noteId
};
}
@@ -72,7 +72,7 @@ async function saveNote(req: Request) {
if (req.body.labels) {
for (const { name, value } of req.body.labels) {
- note.setLabel(sanitizeAttributeName(name), value);
+ note.setLabel(utils.sanitizeAttributeName(name), value);
}
}
diff --git a/apps/server/src/services/consistency_checks.ts b/apps/server/src/services/consistency_checks.ts
index 23727c30fb..a7fe6d3a6e 100644
--- a/apps/server/src/services/consistency_checks.ts
+++ b/apps/server/src/services/consistency_checks.ts
@@ -11,7 +11,6 @@ import cls from "./cls.js";
import entityChangesService from "./entity_changes.js";
import log from "./log.js";
import optionsService from "./options.js";
-import sanitizeAttributeName from "./sanitize_attribute_name.js";
import sql from "./sql.js";
import sqlInit from "./sql_init.js";
import syncMutexService from "./sync_mutex.js";
@@ -804,7 +803,7 @@ class ConsistencyChecks {
const attrNames = sql.getColumn<string>(/*sql*/`SELECT DISTINCT name FROM attributes`);
for (const origName of attrNames) {
- const fixedName = sanitizeAttributeName(origName);
+ const fixedName = utils.sanitizeAttributeName(origName);
if (fixedName !== origName) {
if (this.autoFix) {
diff --git a/apps/server/src/services/import/enex.ts b/apps/server/src/services/import/enex.ts
index 5f9166fce3..19c6170b5e 100644
--- a/apps/server/src/services/import/enex.ts
+++ b/apps/server/src/services/import/enex.ts
@@ -1,20 +1,21 @@
+import type { AttributeType } from "@triliumnext/commons";
import { dayjs } from "@triliumnext/commons";
+import { utils } from "@triliumnext/core";
import sax from "sax";
import stream from "stream";
import { Throttle } from "stream-throttle";
-import log from "../log.js";
-import { md5, escapeHtml, fromBase64 } from "../utils.js";
+
+import type BNote from "../../becca/entities/bnote.js";
import date_utils from "../date_utils.js";
-import sql from "../sql.js";
-import noteService from "../notes.js";
+import htmlSanitizer from "../html_sanitizer.js";
import imageService from "../image.js";
+import log from "../log.js";
+import noteService from "../notes.js";
import protectedSessionService from "../protected_session.js";
-import htmlSanitizer from "../html_sanitizer.js";
-import sanitizeAttributeName from "../sanitize_attribute_name.js";
+import sql from "../sql.js";
import type TaskContext from "../task_context.js";
-import type BNote from "../../becca/entities/bnote.js";
+import { escapeHtml, fromBase64,md5 } from "../utils.js";
import type { File } from "./common.js";
-import type { AttributeType } from "@triliumnext/commons";
/**
* date format is e.g. 20181121T193703Z or 2013-04-14T16:19:00.000Z (Mac evernote, see #3496)
@@ -25,7 +26,7 @@ function parseDate(text: string) {
text = text.replace(/[-:]/g, "");
// insert - and : to convert it to trilium format
- text = text.substr(0, 4) + "-" + text.substr(4, 2) + "-" + text.substr(6, 2) + " " + text.substr(9, 2) + ":" + text.substr(11, 2) + ":" + text.substr(13, 2) + ".000Z";
+ text = `${text.substr(0, 4) }-${ text.substr(4, 2) }-${ text.substr(6, 2) } ${ text.substr(9, 2) }:${ text.substr(11, 2) }:${ text.substr(13, 2) }.000Z`;
return text;
}
@@ -155,7 +156,7 @@ function importEnex(taskContext: TaskContext<"importNotes">, file: File, parentN
labelName = "pageUrl";
}
- labelName = sanitizeAttributeName(labelName || "");
+ labelName = utils.sanitizeAttributeName(labelName || "");
if (note.attributes) {
note.attributes.push({
@@ -201,7 +202,7 @@ function importEnex(taskContext: TaskContext<"importNotes">, file: File, parentN
} else if (currentTag === "tag" && note.attributes) {
note.attributes.push({
type: "label",
- name: sanitizeAttributeName(text),
+ name: utils.sanitizeAttributeName(text),
value: ""
});
}
diff --git a/apps/server/src/services/sanitize_attribute_name.ts b/apps/server/src/services/sanitize_attribute_name.ts
deleted file mode 100644
index 3e597e94a8..0000000000
--- a/apps/server/src/services/sanitize_attribute_name.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-export default function sanitizeAttributeName(origName: string) {
- const fixedName = origName === "" ? "unnamed" : origName.replace(/[^\p{L}\p{N}_:]/gu, "_");
- // any not allowed character should be replaced with underscore
-
- return fixedName;
-}
diff --git a/packages/trilium-core/src/becca/entities/battribute.ts b/packages/trilium-core/src/becca/entities/battribute.ts
index 5e4028fe65..2807995802 100644
--- a/packages/trilium-core/src/becca/entities/battribute.ts
+++ b/packages/trilium-core/src/becca/entities/battribute.ts
@@ -4,8 +4,8 @@ import BNote from "./bnote.js";
import AbstractBeccaEntity from "./abstract_becca_entity.js";
import dateUtils from "../../services/utils/date";
import promotedAttributeDefinitionParser from "../../services/promoted_attribute_definition_parser.js";
-import sanitizeAttributeName from "../../services/sanitize_attribute_name.js";
import type { AttributeRow, AttributeType } from "@triliumnext/commons";
+import { sanitizeAttributeName } from "src/services/utils/index.js";
interface SavingOpts {
skipValidation?: boolean;
diff --git a/apps/server/src/services/sanitize_attribute_name.spec.ts b/packages/trilium-core/src/services/utils/index.spec.ts
similarity index 93%
rename from apps/server/src/services/sanitize_attribute_name.spec.ts
rename to packages/trilium-core/src/services/utils/index.spec.ts
index 3755da5a78..368a81c035 100644
--- a/apps/server/src/services/sanitize_attribute_name.spec.ts
+++ b/packages/trilium-core/src/services/utils/index.spec.ts
@@ -1,5 +1,5 @@
import { expect, describe, it } from "vitest";
-import sanitizeAttributeName from "./sanitize_attribute_name.js";
+import { sanitizeAttributeName } from "./index";
// fn value, expected value
const testCases: [fnValue: string, expectedValue: string][] = [
diff --git a/packages/trilium-core/src/services/utils/index.ts b/packages/trilium-core/src/services/utils/index.ts
index 06f730dcbf..fc39276f1a 100644
--- a/packages/trilium-core/src/services/utils/index.ts
+++ b/packages/trilium-core/src/services/utils/index.ts
@@ -58,3 +58,10 @@ export function removeDiacritic(str: string) {
export function normalize(str: string) {
return removeDiacritic(str).toLowerCase();
}
+
+export function sanitizeAttributeName(origName: string) {
+ const fixedName = origName === "" ? "unnamed" : origName.replace(/[^\p{L}\p{N}_:]/gu, "_");
+ // any not allowed character should be replaced with underscore
+
+ return fixedName;
+}