mirror of
https://github.com/zadam/trilium.git
synced 2026-02-21 21:24:25 +01:00
chore(core): integrate attribute_formatter
This commit is contained in:
parent
a7b2b631c5
commit
c5518b64b7
@ -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;
|
||||
|
||||
@ -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";
|
||||
|
||||
46
packages/trilium-core/src/services/attribute_formatter.ts
Normal file
46
packages/trilium-core/src/services/attribute_formatter.ts
Normal 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
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user