Move "tags" plug into core

This commit is contained in:
Zef Hemel
2022-07-04 11:38:05 +02:00
parent 1ad9b82631
commit f29f9315a1
4 changed files with 17 additions and 18 deletions
+16
View File
@@ -1,5 +1,11 @@
name: core
syntax:
Hashtag:
firstCharacters:
- "#"
regex: "#[^#\\s]+"
styles:
color: blue
NakedURL:
firstCharacters:
- "h"
@@ -63,6 +69,16 @@ functions:
events:
- page:click
# Hashtags
indexTags:
path: "./tags.ts:indexTags"
events:
- page:index
tagComplete:
path: "./tags.ts:tagComplete"
events:
- page:complete
# Full text search
searchIndex:
path: ./search.ts:index
+39
View File
@@ -0,0 +1,39 @@
import { collectNodesOfType } from "@silverbulletmd/common/tree";
import {
batchSet,
queryPrefix,
} from "@silverbulletmd/plugos-silverbullet-syscall";
import { matchBefore } from "@silverbulletmd/plugos-silverbullet-syscall/editor";
import type { IndexTreeEvent } from "@silverbulletmd/web/app_event";
import { removeQueries } from "../query/util";
// Key space
// ht:TAG => true (for completion)
export async function indexTags({ name, tree }: IndexTreeEvent) {
removeQueries(tree);
let allTags = new Set<string>();
collectNodesOfType(tree, "Hashtag").forEach((n) => {
allTags.add(n.children![0].text!);
});
batchSet(
name,
[...allTags].map((t) => ({ key: `ht:${t}`, value: t }))
);
}
export async function tagComplete() {
let prefix = await matchBefore("#[^#\\s]+");
// console.log("Running tag complete", prefix);
if (!prefix) {
return null;
}
let allTags = await queryPrefix(`ht:${prefix.text}`);
return {
from: prefix.from,
options: allTags.map((tag) => ({
label: tag.value,
type: "tag",
})),
};
}