aboutsummaryrefslogtreecommitdiff
path: root/src/background/completion/BookmarkRepository.ts
blob: 12f5455e2aa7c3b6a7f8721ac74c36b2a301848e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { injectable } from "tsyringe";

export type BookmarkItem = {
  title: string
  url: string
}

const COMPLETION_ITEM_LIMIT = 10;

@injectable()
export default class BookmarkRepository {
  async queryBookmarks(query: string): Promise<BookmarkItem[]> {
    const items = await browser.bookmarks.search({ query });
    return items
      .filter(item => item.title && item.title.length > 0)
      .filter(item => item.type === 'bookmark' && item.url)
      .filter((item) => {
        let url = undefined;
        try {
          url = new URL(item.url!!);
        } catch (e) {
          return false;
        }
        return url.protocol !== 'place:';
      })
      .slice(0, COMPLETION_ITEM_LIMIT)
      .map(item => ({
        title: item.title!!,
        url: item.url!!,
      }));
  }
}