chore(core): integrate attribute_formatter

This commit is contained in:
Elian Doran 2026-02-09 19:24:06 +02:00
parent a7b2b631c5
commit c5518b64b7
No known key found for this signature in database
3 changed files with 49 additions and 45 deletions

View File

@ -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;

View File

@ -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";

View File

@ -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
};