Changed all indexes to use (pre) parsed trees.
This commit is contained in:
@@ -29,6 +29,10 @@ functions:
|
||||
path: ./page.ts:pageQueryProvider
|
||||
events:
|
||||
- query:page
|
||||
parseIndexTextRepublish:
|
||||
path: "./page.ts:parseIndexTextRepublish"
|
||||
events:
|
||||
- page:index_text
|
||||
indexLinks:
|
||||
path: "./page.ts:indexLinks"
|
||||
events:
|
||||
|
||||
+1
-22
@@ -1,30 +1,9 @@
|
||||
import { insertAtCursor } from "plugos-silverbullet-syscall/editor";
|
||||
import { IndexEvent } from "../../webapp/app_event";
|
||||
import { batchSet } from "plugos-silverbullet-syscall";
|
||||
import { whiteOutQueries } from "../query/util";
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
export function niceDate(d: Date): string {
|
||||
return new Date().toISOString().split("T")[0];
|
||||
return d.toISOString().split("T")[0];
|
||||
}
|
||||
|
||||
export async function insertToday() {
|
||||
|
||||
+5
-7
@@ -1,9 +1,8 @@
|
||||
import { IndexEvent } from "../../webapp/app_event";
|
||||
import { IndexTreeEvent } from "../../webapp/app_event";
|
||||
|
||||
import { batchSet, scanPrefixGlobal } from "plugos-silverbullet-syscall/index";
|
||||
import { parseMarkdown } from "plugos-silverbullet-syscall/markdown";
|
||||
import { collectNodesOfType, ParseTree, renderToText } from "../../common/tree";
|
||||
import { whiteOutQueries } from "../query/util";
|
||||
import { removeQueries } from "../query/util";
|
||||
import { applyQuery, QueryProviderEvent } from "../query/engine";
|
||||
|
||||
export type Item = {
|
||||
@@ -14,14 +13,13 @@ export type Item = {
|
||||
pos?: number;
|
||||
};
|
||||
|
||||
export async function indexItems({ name, text }: IndexEvent) {
|
||||
export async function indexItems({ name, tree }: IndexTreeEvent) {
|
||||
let items: { key: string; value: Item }[] = [];
|
||||
text = whiteOutQueries(text);
|
||||
removeQueries(tree);
|
||||
|
||||
console.log("Indexing items", name);
|
||||
let mdTree = await parseMarkdown(text);
|
||||
|
||||
let coll = collectNodesOfType(mdTree, "ListItem");
|
||||
let coll = collectNodesOfType(tree, "ListItem");
|
||||
|
||||
coll.forEach((n) => {
|
||||
if (!n.children) {
|
||||
|
||||
+21
-16
@@ -1,4 +1,4 @@
|
||||
import { IndexEvent } from "../../webapp/app_event";
|
||||
import { IndexEvent, IndexTreeEvent } from "../../webapp/app_event";
|
||||
import {
|
||||
batchSet,
|
||||
clearPageIndex as clearPageIndexSyscall,
|
||||
@@ -28,23 +28,20 @@ import {
|
||||
import { applyQuery, QueryProviderEvent } from "../query/engine";
|
||||
import { PageMeta } from "../../common/types";
|
||||
|
||||
export async function indexLinks({ name, text }: IndexEvent) {
|
||||
export async function indexLinks({ name, tree }: IndexTreeEvent) {
|
||||
let backLinks: { key: string; value: string }[] = [];
|
||||
// [[Style Links]]
|
||||
console.log("Now indexing", name);
|
||||
let mdTree = await parseMarkdown(text);
|
||||
collectNodesMatching(mdTree, (n) => n.type === "WikiLinkPage").forEach(
|
||||
(n) => {
|
||||
let toPage = n.children![0].text!;
|
||||
if (toPage.includes("@")) {
|
||||
toPage = toPage.split("@")[0];
|
||||
}
|
||||
backLinks.push({
|
||||
key: `pl:${toPage}:${n.from}`,
|
||||
value: name,
|
||||
});
|
||||
collectNodesMatching(tree, (n) => n.type === "WikiLinkPage").forEach((n) => {
|
||||
let toPage = n.children![0].text!;
|
||||
if (toPage.includes("@")) {
|
||||
toPage = toPage.split("@")[0];
|
||||
}
|
||||
);
|
||||
backLinks.push({
|
||||
key: `pl:${toPage}:${n.from}`,
|
||||
value: name,
|
||||
});
|
||||
});
|
||||
console.log("Found", backLinks.length, "wiki link(s)");
|
||||
await batchSet(name, backLinks);
|
||||
}
|
||||
@@ -193,10 +190,11 @@ export async function reindexSpace() {
|
||||
let pages = await listPages();
|
||||
for (let { name } of pages) {
|
||||
console.log("Indexing", name);
|
||||
const pageObj = await readPage(name);
|
||||
const { text } = await readPage(name);
|
||||
let parsed = await parseMarkdown(text);
|
||||
await dispatch("page:index", {
|
||||
name,
|
||||
text: pageObj.text,
|
||||
tree: parsed,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -206,6 +204,13 @@ export async function clearPageIndex(page: string) {
|
||||
await clearPageIndexForPage(page);
|
||||
}
|
||||
|
||||
export async function parseIndexTextRepublish({ name, text }: IndexEvent) {
|
||||
await dispatch("page:index", {
|
||||
name,
|
||||
tree: await parseMarkdown(text),
|
||||
});
|
||||
}
|
||||
|
||||
export async function parseServerPageCommand() {
|
||||
console.log(await invokeFunction("server", "parsePage", await getText()));
|
||||
}
|
||||
|
||||
+11
-12
@@ -4,7 +4,6 @@ import { parseMarkdown } from "plugos-silverbullet-syscall/markdown";
|
||||
import { extractMeta } from "../query/data";
|
||||
import { renderToText } from "../../common/tree";
|
||||
import { niceDate } from "./dates";
|
||||
import { dispatch } from "plugos-syscall/event";
|
||||
import { invokeFunction } from "plugos-silverbullet-syscall/system";
|
||||
|
||||
const pageTemplatePrefix = `template/page/`;
|
||||
@@ -60,17 +59,17 @@ export async function replaceTemplateVarsCommand() {
|
||||
|
||||
export function replaceTemplateVars(s: string, pageName: string): string {
|
||||
return s.replaceAll(/\{\{([^\}]+)\}\}/g, (match, v) => {
|
||||
if (v === "today") {
|
||||
return niceDate(new Date());
|
||||
}
|
||||
if (v.startsWith("placeholder:")) {
|
||||
// Dispatch event, to be replaced in the file async later
|
||||
dispatch(v, {
|
||||
pageName: pageName,
|
||||
placeholder: v,
|
||||
}).catch((e) => {
|
||||
console.error("Failed to dispatch placeholder event", e);
|
||||
});
|
||||
switch (v) {
|
||||
case "today":
|
||||
return niceDate(new Date());
|
||||
case "yesterday":
|
||||
let yesterday = new Date();
|
||||
yesterday.setDate(yesterday.getDate() - 1);
|
||||
return niceDate(yesterday);
|
||||
case "lastWeek":
|
||||
let lastWeek = new Date();
|
||||
lastWeek.setDate(lastWeek.getDate() - 7);
|
||||
return niceDate(lastWeek);
|
||||
}
|
||||
return match;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user