Tweaking the hashtag parser

pull/3/head
Zef Hemel 2022-07-04 15:43:34 +02:00
parent 60cfefbc95
commit 2a4ea9b3e1
3 changed files with 33 additions and 5 deletions

View File

@ -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:

View File

@ -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<string, number>();
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,
}))
);
}

View File

@ -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)");