1
0
silverbullet/plugs/core/dates.ts

39 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-04-01 15:07:08 +00:00
import { insertAtCursor } from "plugos-silverbullet-syscall/editor";
2022-04-10 09:04:07 +00:00
import { IndexEvent } from "../../webapp/app_event";
import { batchSet } from "plugos-silverbullet-syscall";
2022-04-11 18:34:09 +00:00
import { whiteOutQueries } from "../query/materialized_queries";
2022-04-10 09:04:07 +00:00
const dateMatchRegex = /(\d{4}\-\d{2}\-\d{2})/g;
// Index key space:
// d:[date]:page@pos
export async function indexDates({ name, text }: IndexEvent) {
let dates: { key: string; value: boolean }[] = [];
text = whiteOutQueries(text);
console.log("Now date indexing", name);
for (let match of text.matchAll(dateMatchRegex)) {
// console.log("Date match", match[0]);
dates.push({
key: `d:${match[0]}:${name}@${match.index}`,
value: true,
});
}
console.log("Found", dates.length, "dates");
await batchSet(name, dates);
}
2022-02-24 16:24:49 +00:00
2022-04-11 18:34:09 +00:00
export function niceDate(d: Date): string {
return new Date().toISOString().split("T")[0];
}
2022-02-24 16:24:49 +00:00
export async function insertToday() {
2022-04-11 18:34:09 +00:00
await insertAtCursor(niceDate(new Date()));
2022-04-10 09:04:07 +00:00
}
export async function insertTomorrow() {
let d = new Date();
d.setDate(d.getDate() + 1);
2022-04-11 18:34:09 +00:00
await insertAtCursor(niceDate(d));
2022-02-24 16:24:49 +00:00
}