Move "tags" plug into core
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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",
|
||||
})),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user