Files
notesnook/packages/core/src/api/lookup.ts

97 lines
2.6 KiB
TypeScript
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
2023-01-16 13:44:52 +05:00
Copyright (C) 2023 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";
import Database from ".";
import {
Attachment,
2023-08-21 16:24:05 +05:00
GroupedItems,
Note,
Notebook,
Reminder,
Tag,
TrashItem,
isDeleted
} from "../types";
import { isUnencryptedContent } from "../collections/content";
2020-03-09 12:39:49 +05:00
export default class Lookup {
constructor(private readonly db: Database) {}
2020-03-19 12:38:33 +05:00
async notes(notes: Note[], query: string) {
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;
const noteContent = note.contentId ? contents[note.contentId] : "";
if (
!note.locked &&
noteContent &&
!isDeleted(noteContent) &&
isUnencryptedContent(noteContent)
)
text += noteContent.data;
2021-12-21 11:24:45 +05:00
return text;
});
2020-03-09 12:39:49 +05:00
}
notebooks(array: Notebook[], query: string) {
return search(array, query, (n) => `${n.title} ${n.description}}`);
2020-03-09 12:39:49 +05:00
}
2023-08-21 16:24:05 +05:00
tags(array: GroupedItems<Tag>, query: string) {
return this.byTitle(array, query);
2020-03-09 12:39:49 +05:00
}
reminders(array: Reminder[], query: string) {
2022-11-17 15:41:20 +05:00
return search(array, query, (n) => `${n.title} ${n.description || ""}`);
}
trash(array: TrashItem[], query: string) {
return this.byTitle(array, query);
2020-03-09 12:39:49 +05:00
}
attachments(array: Attachment[], query: string) {
return search(
array,
query,
(n) => `${n.metadata.filename} ${n.metadata.type} ${n.metadata.hash}`
2022-02-28 13:02:16 +05:00
);
}
private byTitle<T extends { title: string }>(array: T[], query: string) {
return search(array, query, (n) => n.title);
2021-12-21 11:24:45 +05:00
}
}
function search<T>(items: T[], query: string, selector: (item: T) => string) {
2021-12-21 11:24:45 +05:00
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
}
}