From c5518b64b76cd7d8f2595d39217a17492cab3c64 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 9 Feb 2026 19:24:06 +0200 Subject: [PATCH] chore(core): integrate attribute_formatter --- .../src/services/attribute_formatter.ts | 47 +------------------ packages/trilium-core/src/index.ts | 1 + .../src/services/attribute_formatter.ts | 46 ++++++++++++++++++ 3 files changed, 49 insertions(+), 45 deletions(-) create mode 100644 packages/trilium-core/src/services/attribute_formatter.ts diff --git a/apps/server/src/services/attribute_formatter.ts b/apps/server/src/services/attribute_formatter.ts index d67e5c7a3d..8ed90a6810 100644 --- a/apps/server/src/services/attribute_formatter.ts +++ b/apps/server/src/services/attribute_formatter.ts @@ -1,46 +1,3 @@ -"use strict"; +import { attribute_formatter } from "@triliumnext/core"; -import type { AttributeRow } from "@triliumnext/commons"; - -function formatAttrForSearch(attr: AttributeRow, searchWithValue: boolean) { - let searchStr = ""; - - if (attr.type === "label") { - searchStr += "#"; - } else if (attr.type === "relation") { - searchStr += "~"; - } else { - throw new Error(`Unrecognized attribute type ${JSON.stringify(attr)}`); - } - - searchStr += attr.name; - - if (searchWithValue && attr.value) { - if (attr.type === "relation") { - searchStr += ".noteId"; - } - - searchStr += "="; - searchStr += formatValue(attr.value); - } - - return searchStr; -} - -function formatValue(val: string) { - if (!/[^\w]/.test(val)) { - return val; - } else if (!val.includes('"')) { - return `"${val}"`; - } else if (!val.includes("'")) { - return `'${val}'`; - } else if (!val.includes("`")) { - return `\`${val}\``; - } else { - return `"${val.replace(/"/g, '\\"')}"`; - } -} - -export default { - formatAttrForSearch -}; +export default attribute_formatter; diff --git a/packages/trilium-core/src/index.ts b/packages/trilium-core/src/index.ts index 19a8228bff..b89241c66c 100644 --- a/packages/trilium-core/src/index.ts +++ b/packages/trilium-core/src/index.ts @@ -39,6 +39,7 @@ export { default as revisions } from "./services/revisions"; export { default as erase } from "./services/erase"; export { default as getSharedBootstrapItems } from "./services/bootstrap_utils"; export { default as branches } from "./services/branches"; +export { default as attribute_formatter} from "./services/attribute_formatter"; // Messaging system export * from "./services/messaging/index"; diff --git a/packages/trilium-core/src/services/attribute_formatter.ts b/packages/trilium-core/src/services/attribute_formatter.ts new file mode 100644 index 0000000000..d67e5c7a3d --- /dev/null +++ b/packages/trilium-core/src/services/attribute_formatter.ts @@ -0,0 +1,46 @@ +"use strict"; + +import type { AttributeRow } from "@triliumnext/commons"; + +function formatAttrForSearch(attr: AttributeRow, searchWithValue: boolean) { + let searchStr = ""; + + if (attr.type === "label") { + searchStr += "#"; + } else if (attr.type === "relation") { + searchStr += "~"; + } else { + throw new Error(`Unrecognized attribute type ${JSON.stringify(attr)}`); + } + + searchStr += attr.name; + + if (searchWithValue && attr.value) { + if (attr.type === "relation") { + searchStr += ".noteId"; + } + + searchStr += "="; + searchStr += formatValue(attr.value); + } + + return searchStr; +} + +function formatValue(val: string) { + if (!/[^\w]/.test(val)) { + return val; + } else if (!val.includes('"')) { + return `"${val}"`; + } else if (!val.includes("'")) { + return `'${val}'`; + } else if (!val.includes("`")) { + return `\`${val}\``; + } else { + return `"${val.replace(/"/g, '\\"')}"`; + } +} + +export default { + formatAttrForSearch +};