A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search
feat(search): add notes and highlights qualifiers (#3053)
Waynef4596f49473f
diff --git a/apps/web/components/dashboard/search/QueryExplainerTooltip.tsx b/apps/web/components/dashboard/search/QueryExplainerTooltip.tsxindex 73735702..3003fa1a 100644--- a/apps/web/components/dashboard/search/QueryExplainerTooltip.tsx+++ b/apps/web/components/dashboard/search/QueryExplainerTooltip.tsx@@ -144,6 +144,26 @@ export default function QueryExplainerTooltip({</TableCell></TableRow>);+ case "hasNotes":+ return (+ <TableRow>+ <TableCell colSpan={2} className="text-center">+ {matcher.hasNotes+ ? t("search.has_notes")+ : t("search.has_no_notes")}+ </TableCell>+ </TableRow>+ );+ case "hasHighlights":+ return (+ <TableRow>+ <TableCell colSpan={2} className="text-center">+ {matcher.hasHighlights+ ? t("search.has_highlights")+ : t("search.has_no_highlights")}+ </TableCell>+ </TableRow>+ );case "and":case "or":return (diff --git a/apps/web/components/dashboard/search/useSearchAutocomplete.ts b/apps/web/components/dashboard/search/useSearchAutocomplete.tsindex 1885f19b..18d9ba21 100644--- a/apps/web/components/dashboard/search/useSearchAutocomplete.ts+++ b/apps/web/components/dashboard/search/useSearchAutocomplete.ts@@ -54,6 +54,18 @@ const QUALIFIER_DEFINITIONS = [negatedDescriptionKey: "search.is_not_in_any_list",appendSpace: true,},+ {+ value: "has:notes",+ descriptionKey: "search.has_notes",+ negatedDescriptionKey: "search.has_no_notes",+ appendSpace: true,+ },+ {+ value: "has:highlights",+ descriptionKey: "search.has_highlights",+ negatedDescriptionKey: "search.has_no_highlights",+ appendSpace: true,+ },{value: "is:link",appendSpace: true,diff --git a/apps/web/lib/i18n/locales/en/translation.json b/apps/web/lib/i18n/locales/en/translation.jsonindex 860bc9f1..5bd1f3b9 100644--- a/apps/web/lib/i18n/locales/en/translation.json+++ b/apps/web/lib/i18n/locales/en/translation.json@@ -924,6 +924,10 @@"is_not_archived": "Is Not Archived","has_any_tag": "Has Any Tag","has_no_tags": "Has No Tag",+ "has_notes": "Has Notes",+ "has_no_notes": "Has No Notes",+ "has_highlights": "Has Highlights",+ "has_no_highlights": "Has No Highlights","is_in_any_list": "Is In Any List","is_not_in_any_list": "Is not In Any List","created_on_or_after": "Created on or After",diff --git a/apps/web/lib/i18n/locales/en_US/translation.json b/apps/web/lib/i18n/locales/en_US/translation.jsonindex cec7ec7f..776f96d3 100644--- a/apps/web/lib/i18n/locales/en_US/translation.json+++ b/apps/web/lib/i18n/locales/en_US/translation.json@@ -1013,6 +1013,10 @@"is_not_archived": "Is Not Archived","has_any_tag": "Has Any Tag","has_no_tags": "Has No Tag",+ "has_notes": "Has Notes",+ "has_no_notes": "Has No Notes",+ "has_highlights": "Has Highlights",+ "has_no_highlights": "Has No Highlights","is_in_any_list": "Is In Any List","is_not_in_any_list": "Is not In Any List","created_on_or_after": "Created on or After",diff --git a/docs/docs/04-using-karakeep/search-query-language.md b/docs/docs/04-using-karakeep/search-query-language.mdindex cdf1f56e..52658917 100644--- a/docs/docs/04-using-karakeep/search-query-language.md+++ b/docs/docs/04-using-karakeep/search-query-language.md@@ -24,6 +24,8 @@ Here's a comprehensive table of all supported qualifiers:| `is:archived` | Archived bookmarks | `-is:archived` || `is:tagged` | Bookmarks that has one or more tags | `is:tagged` || `is:inlist` | Bookmarks that are in one or more lists | `is:inlist` |+| `has:notes` | Bookmarks that have a non-empty note | `has:notes` |+| `has:highlights` | Bookmarks that have one or more highlights | `has:highlights` || `is:link`, `is:text`, `is:media` | Bookmarks that are of type link, text or media | `is:link` || `is:broken` | Bookmarks with broken/failed links (crawl failures or non-2xx status codes) | `is:broken` || `url:<value>` | Match bookmarks with URL substring | `url:example.com` |@@ -50,6 +52,10 @@ is:archived and (list:reading or #work)# Find bookmarks that are not tagged or not in any list-is:tagged or -is:inlist++# Find bookmarks that have notes but no highlights+has:notes -has:highlights+# Find bookmarks with "React" in the titletitle:React```diff --git a/packages/shared/searchQueryParser.test.ts b/packages/shared/searchQueryParser.test.tsindex 37275284..a40b4187 100644--- a/packages/shared/searchQueryParser.test.ts+++ b/packages/shared/searchQueryParser.test.ts@@ -141,6 +141,100 @@ describe("Search Query Parser", () => {});});+ describe("has queries", () => {+ test("parses supported values", () => {+ expect(parseSearchQuery("has:notes")).toEqual({+ result: "full",+ text: "",+ matcher: {+ type: "hasNotes",+ hasNotes: true,+ },+ });+ expect(parseSearchQuery("has:highlights")).toEqual({+ result: "full",+ text: "",+ matcher: {+ type: "hasHighlights",+ hasHighlights: true,+ },+ });+ });++ test("supports both negation forms", () => {+ expect(parseSearchQuery("-has:notes")).toEqual({+ result: "full",+ text: "",+ matcher: {+ type: "hasNotes",+ hasNotes: false,+ },+ });+ expect(parseSearchQuery("!has:notes")).toEqual({+ result: "full",+ text: "",+ matcher: {+ type: "hasNotes",+ hasNotes: false,+ },+ });+ expect(parseSearchQuery("-has:highlights")).toEqual({+ result: "full",+ text: "",+ matcher: {+ type: "hasHighlights",+ hasHighlights: false,+ },+ });+ expect(parseSearchQuery("!has:highlights")).toEqual({+ result: "full",+ text: "",+ matcher: {+ type: "hasHighlights",+ hasHighlights: false,+ },+ });+ });++ test("preserves unsupported values as pure text", () => {+ expect(parseSearchQuery("has:attachments")).toEqual({+ result: "full",+ text: "has:attachments",+ matcher: undefined,+ });+ expect(parseSearchQuery("-has:attachments")).toEqual({+ result: "full",+ text: "-has:attachments",+ matcher: undefined,+ });+ });++ test("composes with AND and OR expressions", () => {+ expect(parseSearchQuery("has:notes and has:highlights")).toEqual({+ result: "full",+ text: "",+ matcher: {+ type: "and",+ matchers: [+ { type: "hasNotes", hasNotes: true },+ { type: "hasHighlights", hasHighlights: true },+ ],+ },+ });+ expect(parseSearchQuery("has:notes or !has:highlights")).toEqual({+ result: "full",+ text: "",+ matcher: {+ type: "or",+ matchers: [+ { type: "hasNotes", hasNotes: true },+ { type: "hasHighlights", hasHighlights: false },+ ],+ },+ });+ });+ });+test("simple string queries", () => {expect(parseSearchQuery("url:https://example.com")).toEqual({result: "full",diff --git a/packages/shared/searchQueryParser.ts b/packages/shared/searchQueryParser.tsindex 7eb3b185..15039898 100644--- a/packages/shared/searchQueryParser.ts+++ b/packages/shared/searchQueryParser.ts@@ -43,7 +43,7 @@ const lexerRules: [RegExp, TokenType][] = [[/^#/, TokenType.Hash],[- /^(is|url|list|after|before|age|feed|title|tag|source):/,+ /^(is|has|url|list|after|before|age|feed|title|tag|source):/,TokenType.Qualifier,],@@ -213,6 +213,27 @@ MATCHER.setPattern(text: "",matcher: { type: "title", title: ident, inverse: !!minus },};+ case "has:":+ switch (ident) {+ case "notes":+ return {+ text: "",+ matcher: { type: "hasNotes", hasNotes: !minus },+ };+ case "highlights":+ return {+ text: "",+ matcher: {+ type: "hasHighlights",+ hasHighlights: !minus,+ },+ };+ default:+ return {+ text: (minus?.text ?? "") + qualifier.text + ident,+ matcher: undefined,+ };+ }case "#":case "tag:":return {diff --git a/packages/shared/types/search.ts b/packages/shared/types/search.tsindex b653d883..3e072e32 100644--- a/packages/shared/types/search.ts+++ b/packages/shared/types/search.ts@@ -88,6 +88,16 @@ const zBrokenLinksMatcher = z.object({brokenLinks: z.boolean(),});+const zHasNotesMatcher = z.object({+ type: z.literal("hasNotes"),+ hasNotes: z.boolean(),+});++const zHasHighlightsMatcher = z.object({+ type: z.literal("hasHighlights"),+ hasHighlights: z.boolean(),+});+const zSourceMatcher = z.object({type: z.literal("source"),source: zBookmarkSourceSchema,@@ -109,6 +119,8 @@ const zNonRecursiveMatcher = z.union([zTypeMatcher,zRssFeedNameMatcher,zBrokenLinksMatcher,+ zHasNotesMatcher,+ zHasHighlightsMatcher,zSourceMatcher,]);@@ -134,6 +146,8 @@ export const zMatcherSchema: z.ZodType<Matcher> = z.lazy(() => {zTypeMatcher,zRssFeedNameMatcher,zBrokenLinksMatcher,+ zHasNotesMatcher,+ zHasHighlightsMatcher,zSourceMatcher,z.object({type: z.literal("and"),diff --git a/packages/trpc/lib/__tests__/search.test.ts b/packages/trpc/lib/__tests__/search.test.tsindex 6081b79c..2898382d 100644--- a/packages/trpc/lib/__tests__/search.test.ts+++ b/packages/trpc/lib/__tests__/search.test.ts@@ -9,6 +9,7 @@ import {bookmarksInLists,bookmarkTags,bookmarkTexts,+ highlights,rssFeedImportsTable,rssFeedsTable,tagsOnBookmarks,@@ -34,6 +35,12 @@ beforeEach(async () => {email: "[email protected]",role: "user",},+ {+ id: "other-user",+ name: "Other User",+ email: "[email protected]",+ role: "user",+ },]);// Setup test data@@ -46,6 +53,7 @@ beforeEach(async () => {favourited: false,createdAt: new Date("2024-01-01"),title: null,+ note: "A bookmark note",},{id: "b2",@@ -55,6 +63,7 @@ beforeEach(async () => {favourited: true,createdAt: new Date("2024-01-02"),title: "example domain page",+ note: "",},{id: "b3",@@ -92,6 +101,16 @@ beforeEach(async () => {createdAt: new Date("2024-01-06"),title: "example asset",},+ {+ id: "b7",+ type: BookmarkTypes.TEXT,+ userId: "other-user",+ archived: false,+ favourited: false,+ createdAt: new Date("2024-01-07"),+ title: "another user's bookmark",+ note: "Another user's note",+ },]);await db.insert(bookmarkLinks).values([@@ -123,6 +142,25 @@ beforeEach(async () => {},]);+ await db.insert(highlights).values([+ {+ id: "h1",+ bookmarkId: "b3",+ userId: testUserId,+ startOffset: 0,+ endOffset: 4,+ text: "This",+ },+ {+ id: "h2",+ bookmarkId: "b4",+ userId: "other-user",+ startOffset: 0,+ endOffset: 7,+ text: "Another",+ },+ ]);+await db.insert(bookmarkTags).values([{ id: "t1", userId: testUserId, name: "tag1" },{ id: "t2", userId: testUserId, name: "tag2" },@@ -319,6 +357,36 @@ describe("getBookmarkIdsFromMatcher", () => {expect(result).toEqual(["b1", "b3", "b5", "b6"]);});+ it("should handle hasNotes matcher", async () => {+ const matcher: Matcher = { type: "hasNotes", hasNotes: true };+ const result = await getBookmarkIdsFromMatcher(mockCtx, matcher);+ expect(result).toEqual(["b1"]);+ });++ it("should handle hasNotes matcher with hasNotes=false", async () => {+ const matcher: Matcher = { type: "hasNotes", hasNotes: false };+ const result = await getBookmarkIdsFromMatcher(mockCtx, matcher);+ expect(result.sort()).toEqual(["b2", "b3", "b4", "b5", "b6"]);+ });++ it("should handle hasHighlights matcher for the current user", async () => {+ const matcher: Matcher = {+ type: "hasHighlights",+ hasHighlights: true,+ };+ const result = await getBookmarkIdsFromMatcher(mockCtx, matcher);+ expect(result).toEqual(["b3"]);+ });++ it("should handle hasHighlights=false and ignore another user's highlights", async () => {+ const matcher: Matcher = {+ type: "hasHighlights",+ hasHighlights: false,+ };+ const result = await getBookmarkIdsFromMatcher(mockCtx, matcher);+ expect(result.sort()).toEqual(["b1", "b2", "b4", "b5", "b6"]);+ });+it("should handle url matcher", async () => {const matcher: Matcher = {type: "url",diff --git a/packages/trpc/lib/search.ts b/packages/trpc/lib/search.tsindex 4708f67a..e7da80aa 100644--- a/packages/trpc/lib/search.ts+++ b/packages/trpc/lib/search.ts@@ -24,6 +24,7 @@ import {bookmarks,bookmarksInLists,bookmarkTags,+ highlights,rssFeedImportsTable,rssFeedsTable,tagsOnBookmarks,@@ -349,6 +350,41 @@ async function getIds(),);}+ case "hasNotes": {+ return db+ .select({ id: bookmarks.id })+ .from(bookmarks)+ .where(+ and(+ eq(bookmarks.userId, userId),+ matcher.hasNotes+ ? and(isNotNull(bookmarks.note), ne(bookmarks.note, ""))+ : or(isNull(bookmarks.note), eq(bookmarks.note, "")),+ ),+ );+ }+ case "hasHighlights": {+ const comp = matcher.hasHighlights ? exists : notExists;+ return db+ .select({ id: bookmarks.id })+ .from(bookmarks)+ .where(+ and(+ eq(bookmarks.userId, userId),+ comp(+ db+ .select()+ .from(highlights)+ .where(+ and(+ eq(highlights.bookmarkId, bookmarks.id),+ eq(highlights.userId, userId),+ ),+ ),+ ),+ ),+ );+ }case "dateAfter": {const comp = matcher.inverse ? lt : gte;return dbdiff --git a/packages/trpc/routers/lists.test.ts b/packages/trpc/routers/lists.test.tsindex 73fb4f4c..d3b6d588 100644--- a/packages/trpc/routers/lists.test.ts+++ b/packages/trpc/routers/lists.test.ts@@ -738,6 +738,71 @@ describe("recursive delete", () => {});});+describe("Smart list qualifiers", () => {+ test<CustomTestContext>("smart list applies note and highlight qualifiers per user", async ({+ apiCallers,+ }) => {+ const ownerApi = apiCallers[0];+ const otherApi = apiCallers[1];++ const withNote = await ownerApi.bookmarks.createBookmark({+ type: BookmarkTypes.TEXT,+ text: "Bookmark with a note",+ note: "Owner note",+ });+ await ownerApi.bookmarks.createBookmark({+ type: BookmarkTypes.TEXT,+ text: "Bookmark with an empty note",+ note: "",+ });+ const withHighlight = await ownerApi.bookmarks.createBookmark({+ type: BookmarkTypes.LINK,+ url: "https://example.com/owner-highlight",+ });+ await ownerApi.highlights.create({+ bookmarkId: withHighlight.id,+ startOffset: 0,+ endOffset: 4,+ color: "yellow",+ text: "Test highlight",+ note: "",+ });++ await otherApi.bookmarks.createBookmark({+ type: BookmarkTypes.TEXT,+ text: "Other user's bookmark with a note",+ note: "Other user note",+ });+ const otherWithHighlight = await otherApi.bookmarks.createBookmark({+ type: BookmarkTypes.LINK,+ url: "https://example.com/other-highlight",+ });+ await otherApi.highlights.create({+ bookmarkId: otherWithHighlight.id,+ startOffset: 0,+ endOffset: 4,+ color: "yellow",+ text: "Other user's highlight",+ note: "",+ });++ const smartList = await ownerApi.lists.create({+ name: "Notes or highlights",+ type: "smart",+ query: "has:notes or has:highlights",+ icon: "📝",+ });++ const result = await ownerApi.bookmarks.getBookmarks({+ listId: smartList.id,+ });++ expect(result.bookmarks.map((bookmark) => bookmark.id).sort()).toEqual(+ [withNote.id, withHighlight.id].sort(),+ );+ });+});+describe("Nested smart lists", () => {test<CustomTestContext>("smart list can reference another smart list", async ({apiCallers,diff --git a/skills/SKILL.md b/skills/SKILL.mdindex c99baa64..4092a5a2 100644--- a/skills/SKILL.md+++ b/skills/SKILL.md@@ -61,6 +61,8 @@ Spaces between conditions mean `and`. Use `and`, `or`, and parentheses for expli| `is:archived` | Archived bookmarks | `-is:archived` || `is:tagged` | Bookmarks with at least one tag | `-is:tagged` || `is:inlist` | Bookmarks in at least one list | `is:inlist` |+| `has:notes` | Bookmarks with a non-empty note | `has:notes` |+| `has:highlights` | Bookmarks with at least one highlight | `-has:highlights` || `is:link` | Link bookmarks | `is:link` || `is:text` | Text bookmarks | `is:text` || `is:media` | Image or PDF bookmarks | `is:media` |@@ -83,6 +85,7 @@ Examples:is:fav after:2026-01-01 #importantis:archived and (list:reading or #work)-is:tagged or -is:inlist+has:notes -has:highlightsmachine learning is:fav -is:archived```