2022-04-12 11:33:07 +00:00
|
|
|
// Index key space:
|
|
|
|
// data:page@pos
|
|
|
|
|
2022-10-10 12:50:21 +00:00
|
|
|
import type { IndexTreeEvent } from "../../web/app_event.ts";
|
2022-04-25 08:33:38 +00:00
|
|
|
import {
|
|
|
|
batchSet,
|
2022-05-17 13:54:55 +00:00
|
|
|
queryPrefix,
|
2022-10-10 12:50:21 +00:00
|
|
|
} from "../../syscall/silverbullet-syscall/index.ts";
|
2022-04-25 08:33:38 +00:00
|
|
|
import {
|
2022-07-04 09:30:30 +00:00
|
|
|
addParentPointers,
|
2022-04-25 08:33:38 +00:00
|
|
|
collectNodesOfType,
|
|
|
|
findNodeOfType,
|
|
|
|
ParseTree,
|
|
|
|
replaceNodesMatching,
|
2022-10-10 12:50:21 +00:00
|
|
|
} from "../../common/tree.ts";
|
|
|
|
import type { QueryProviderEvent } from "./engine.ts";
|
|
|
|
import { applyQuery } from "./engine.ts";
|
|
|
|
import { removeQueries } from "./util.ts";
|
|
|
|
import * as YAML from "yaml";
|
2022-04-12 11:33:07 +00:00
|
|
|
|
2022-04-20 08:56:43 +00:00
|
|
|
export async function indexData({ name, tree }: IndexTreeEvent) {
|
2022-04-12 11:33:07 +00:00
|
|
|
let dataObjects: { key: string; value: Object }[] = [];
|
|
|
|
|
2022-04-21 09:46:33 +00:00
|
|
|
removeQueries(tree);
|
|
|
|
|
2022-04-20 08:56:43 +00:00
|
|
|
collectNodesOfType(tree, "FencedCode").forEach((t) => {
|
2022-04-12 11:33:07 +00:00
|
|
|
let codeInfoNode = findNodeOfType(t, "CodeInfo");
|
|
|
|
if (!codeInfoNode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (codeInfoNode.children![0].text !== "data") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let codeTextNode = findNodeOfType(t, "CodeText");
|
|
|
|
if (!codeTextNode) {
|
|
|
|
// Honestly, this shouldn't happen
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let codeText = codeTextNode.children![0].text!;
|
|
|
|
try {
|
|
|
|
// We support multiple YAML documents in one block
|
2022-04-13 12:46:52 +00:00
|
|
|
for (let doc of parseAllDocuments(codeText)) {
|
2022-04-12 11:33:07 +00:00
|
|
|
if (!doc.contents) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
console.log(doc.contents.toJSON());
|
|
|
|
dataObjects.push({
|
|
|
|
key: `data:${name}@${t.from! + doc.range[0]}`,
|
|
|
|
value: doc.contents.toJSON(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// console.log("Parsed data", parsedData);
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Could not parse data", codeText, "error:", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
2022-04-13 12:46:52 +00:00
|
|
|
console.log("Found", dataObjects.length, "data objects");
|
2022-04-12 11:33:07 +00:00
|
|
|
await batchSet(name, dataObjects);
|
|
|
|
}
|
2022-04-13 12:46:52 +00:00
|
|
|
|
2022-05-06 16:55:04 +00:00
|
|
|
export function extractMeta(
|
|
|
|
parseTree: ParseTree,
|
2022-10-10 12:50:21 +00:00
|
|
|
removeKeys: string[] = [],
|
2022-05-06 16:55:04 +00:00
|
|
|
): any {
|
|
|
|
let data: any = {};
|
2022-07-04 09:30:30 +00:00
|
|
|
addParentPointers(parseTree);
|
2022-04-13 12:46:52 +00:00
|
|
|
replaceNodesMatching(parseTree, (t) => {
|
2022-07-04 09:30:30 +00:00
|
|
|
if (t.type === "Hashtag") {
|
|
|
|
// Check if if nested directly into a Paragraph
|
|
|
|
if (t.parent && t.parent.type === "Paragraph") {
|
|
|
|
let tagname = t.children![0].text;
|
|
|
|
if (!data.tags) {
|
|
|
|
data.tags = [];
|
|
|
|
}
|
|
|
|
if (!data.tags.includes(tagname)) {
|
|
|
|
data.tags.push(tagname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Find a fenced code block
|
2022-04-13 12:46:52 +00:00
|
|
|
if (t.type !== "FencedCode") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let codeInfoNode = findNodeOfType(t, "CodeInfo");
|
|
|
|
if (!codeInfoNode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (codeInfoNode.children![0].text !== "meta") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let codeTextNode = findNodeOfType(t, "CodeText");
|
|
|
|
if (!codeTextNode) {
|
|
|
|
// Honestly, this shouldn't happen
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let codeText = codeTextNode.children![0].text!;
|
2022-10-10 12:50:21 +00:00
|
|
|
data = YAML.parse(codeText);
|
2022-05-06 16:55:04 +00:00
|
|
|
if (removeKeys.length > 0) {
|
|
|
|
let newData = { ...data };
|
|
|
|
for (let key of removeKeys) {
|
|
|
|
delete newData[key];
|
|
|
|
}
|
2022-10-10 12:50:21 +00:00
|
|
|
codeTextNode.children![0].text = YAML.stringify(newData).trim();
|
2022-08-08 11:09:19 +00:00
|
|
|
// If nothing is left, let's just delete this thing
|
|
|
|
if (Object.keys(newData).length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-05-06 16:55:04 +00:00
|
|
|
}
|
|
|
|
return undefined;
|
2022-04-13 12:46:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
2022-04-19 14:54:47 +00:00
|
|
|
|
|
|
|
export async function queryProvider({
|
|
|
|
query,
|
2022-04-28 09:55:38 +00:00
|
|
|
}: QueryProviderEvent): Promise<any[]> {
|
2022-04-19 14:54:47 +00:00
|
|
|
let allData: any[] = [];
|
2022-05-17 13:54:55 +00:00
|
|
|
for (let { key, page, value } of await queryPrefix("data:")) {
|
2022-04-19 14:54:47 +00:00
|
|
|
let [, pos] = key.split("@");
|
|
|
|
allData.push({
|
|
|
|
...value,
|
|
|
|
page: page,
|
|
|
|
pos: +pos,
|
|
|
|
});
|
|
|
|
}
|
2022-04-28 09:55:38 +00:00
|
|
|
return applyQuery(query, allData);
|
2022-04-19 14:54:47 +00:00
|
|
|
}
|