mirror of
https://github.com/zadam/trilium.git
synced 2025-11-02 11:26:15 +01:00
becca conversion WIP
This commit is contained in:
50
src/services/attribute_formatter.js
Normal file
50
src/services/attribute_formatter.js
Normal file
@@ -0,0 +1,50 @@
|
||||
"use strict"
|
||||
|
||||
function formatAttrForSearch(attr, searchWithValue) {
|
||||
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) {
|
||||
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, '\\"') + '"';
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
formatAttrForSearch
|
||||
}
|
||||
Reference in New Issue
Block a user