Files
notesnook/packages/core/api/lookup.js

85 lines
2.1 KiB
JavaScript
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2022 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-30 16:13:11 +05:00
2021-12-21 11:24:45 +05:00
import { filter, parse } from "liqe";
2020-03-09 12:39:49 +05:00
export default class Lookup {
2020-03-19 12:38:33 +05:00
/**
*
* @param {import('./index').default} db
2020-03-19 12:38:33 +05:00
*/
constructor(db) {
this._db = db;
}
async notes(notes, query) {
2021-02-12 10:15:37 +05:00
const contents = await this._db.content.multi(
2021-02-20 02:11:32 +05:00
notes.map((note) => note.contentId || "")
);
2021-12-20 09:29:25 +05:00
2021-12-21 11:24:45 +05:00
return search(notes, query, (note) => {
let text = note.title;
if (!note.locked && !!note.contentId && !!contents[note.contentId])
2021-12-21 11:24:45 +05:00
text += contents[note.contentId]["data"];
return text;
});
2020-03-09 12:39:49 +05:00
}
notebooks(array, query) {
2021-12-21 11:24:45 +05:00
return search(array, query, (n) => `${n.title} ${n.description}`);
2020-03-09 12:39:49 +05:00
}
topics(array, query) {
return this._byTitle(array, query);
}
tags(array, query) {
return this._byTitle(array, query);
}
trash(array, query) {
return this._byTitle(array, query);
}
2022-02-28 13:02:16 +05:00
attachments(array, query) {
return search(
array,
query,
(n) => `${n.metadata.filename} ${n.metadata.type} ${n.metadata.hash}`
);
}
2020-03-09 12:39:49 +05:00
_byTitle(array, query) {
2021-12-21 11:24:45 +05:00
return search(array, query, (n) => n.title);
}
}
function search(items, query, selector) {
try {
return filter(
parse(`text:"${query.toLowerCase()}"`),
items.map((item) => {
return { item, text: selector(item).toLowerCase() };
})
).map((v) => v.item);
} catch (e) {
return [];
2020-03-09 12:39:49 +05:00
}
}