From 2a4ea9b3e17857ba6a6526a5c6b1d64f30bc538c Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Mon, 4 Jul 2022 15:43:34 +0200 Subject: [PATCH] Tweaking the hashtag parser --- packages/plugs/core/core.plug.yaml | 6 +++++- packages/plugs/core/tags.ts | 30 +++++++++++++++++++++++++++--- packages/plugs/tasks/task.ts | 2 +- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/packages/plugs/core/core.plug.yaml b/packages/plugs/core/core.plug.yaml index dcf61377..bb1b0e62 100644 --- a/packages/plugs/core/core.plug.yaml +++ b/packages/plugs/core/core.plug.yaml @@ -3,7 +3,7 @@ syntax: Hashtag: firstCharacters: - "#" - regex: "#[^#\\s]+" + regex: "#[^#\\d\\s]+\\w" styles: color: blue NakedURL: @@ -89,6 +89,10 @@ functions: path: "./tags.ts:tagComplete" events: - page:complete + tagProvider: + path: "./tags.ts:tagProvider" + events: + - query:tag # Full text search searchIndex: diff --git a/packages/plugs/core/tags.ts b/packages/plugs/core/tags.ts index c274fa12..261dcc64 100644 --- a/packages/plugs/core/tags.ts +++ b/packages/plugs/core/tags.ts @@ -5,10 +5,11 @@ import { } from "@silverbulletmd/plugos-silverbullet-syscall"; import { matchBefore } from "@silverbulletmd/plugos-silverbullet-syscall/editor"; import type { IndexTreeEvent } from "@silverbulletmd/web/app_event"; +import { applyQuery, QueryProviderEvent } from "../query/engine"; import { removeQueries } from "../query/util"; // Key space -// ht:TAG => true (for completion) +// tag:TAG => true (for completion) export async function indexTags({ name, tree }: IndexTreeEvent) { removeQueries(tree); @@ -18,7 +19,7 @@ export async function indexTags({ name, tree }: IndexTreeEvent) { }); batchSet( name, - [...allTags].map((t) => ({ key: `ht:${t}`, value: t })) + [...allTags].map((t) => ({ key: `tag:${t}`, value: t })) ); } @@ -28,7 +29,7 @@ export async function tagComplete() { if (!prefix) { return null; } - let allTags = await queryPrefix(`ht:${prefix.text}`); + let allTags = await queryPrefix(`tag:${prefix.text}`); return { from: prefix.from, options: allTags.map((tag) => ({ @@ -37,3 +38,26 @@ export async function tagComplete() { })), }; } + +type Tag = { + name: string; + freq: number; +}; + +export async function tagProvider({ query }: QueryProviderEvent) { + let allTags = new Map(); + for (let { value } of await queryPrefix("tag:")) { + let currentFreq = allTags.get(value); + if (!currentFreq) { + currentFreq = 0; + } + allTags.set(value, currentFreq + 1); + } + return applyQuery( + query, + [...allTags.entries()].map(([name, freq]) => ({ + name, + freq, + })) + ); +} diff --git a/packages/plugs/tasks/task.ts b/packages/plugs/tasks/task.ts index 571d6c56..13accbc6 100644 --- a/packages/plugs/tasks/task.ts +++ b/packages/plugs/tasks/task.ts @@ -82,7 +82,7 @@ export async function indexTasks({ name, tree }: IndexTreeEvent) { key: `task:${n.from}`, value: task, }); - console.log("Task", task); + // console.log("Task", task); }); console.log("Found", tasks.length, "task(s)");