Skip to content

Build your personal knowledge base with Trilium Notes

chore(core): integrate attribute_formatter

Elian Doranc5518b64b76c
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
+};