1
0
silverbullet/plugs/query/data.ts

164 lines
4.5 KiB
TypeScript
Raw Normal View History

// Index key space:
// data:page@pos
2022-10-14 13:11:33 +00:00
import type { IndexTreeEvent, QueryProviderEvent } from "$sb/app_event.ts";
import { index } from "$sb/silverbullet-syscall/mod.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,
renderToText,
2022-04-25 08:33:38 +00:00
replaceNodesMatching,
2022-10-14 13:11:33 +00:00
} from "$sb/lib/tree.ts";
import { applyQuery, removeQueries } from "$sb/lib/query.ts";
import * as YAML from "yaml";
import { text } from "https://esm.sh/v96/@fortawesome/fontawesome-svg-core@1.3.0/X-ZS9AZm9ydGF3ZXNvbWUvZm9udGF3ZXNvbWUtY29tbW9uLXR5cGVz/index.d.ts";
export async function indexData({ name, tree }: IndexTreeEvent) {
2022-10-14 13:11:33 +00:00
const dataObjects: { key: string; value: any }[] = [];
removeQueries(tree);
collectNodesOfType(tree, "FencedCode").forEach((t) => {
2022-10-14 13:11:33 +00:00
const codeInfoNode = findNodeOfType(t, "CodeInfo");
if (!codeInfoNode) {
return;
}
if (codeInfoNode.children![0].text !== "data") {
return;
}
2022-10-14 13:11:33 +00:00
const codeTextNode = findNodeOfType(t, "CodeText");
if (!codeTextNode) {
// Honestly, this shouldn't happen
return;
}
2022-10-14 13:11:33 +00:00
const codeText = codeTextNode.children![0].text!;
try {
2022-10-14 13:11:33 +00:00
const docs = codeText.split("---").map((d) => YAML.parse(d));
// We support multiple YAML documents in one block
2022-10-14 13:11:33 +00:00
for (let i = 0; i < docs.length; i++) {
const doc = docs[i];
if (!doc) {
continue;
}
dataObjects.push({
2022-10-14 13:11:33 +00:00
key: `data:${name}@${i}`,
value: doc,
});
}
// console.log("Parsed data", parsedData);
} catch (e) {
console.error("Could not parse data", codeText, "error:", e);
return;
}
});
console.log("Found", dataObjects.length, "data objects");
2022-10-14 13:11:33 +00:00
await index.batchSet(name, dataObjects);
}
2022-05-06 16:55:04 +00:00
export function extractMeta(
parseTree: ParseTree,
removeKeys: string[] = [],
2022-05-06 16:55:04 +00:00
): any {
let data: any = {};
2022-07-04 09:30:30 +00:00
addParentPointers(parseTree);
replaceNodesMatching(parseTree, (t) => {
// Find top-level hash tags
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") {
const tagname = t.children![0].text!.substring(1);
2022-07-04 09:30:30 +00:00
if (!data.tags) {
data.tags = [];
}
if (!data.tags.includes(tagname)) {
data.tags.push(tagname);
}
}
return;
}
// Find FrontMatter and parse it
if (t.type === "FrontMatter") {
const yamlText = renderToText(t.children![1].children![0]);
const parsedData: any = YAML.parse(yamlText);
const newData = { ...parsedData };
data = { ...data, ...parsedData };
if (removeKeys.length > 0) {
let removedOne = false;
for (const key of removeKeys) {
if (key in newData) {
delete newData[key];
removedOne = true;
}
}
if (removedOne) {
t.children![0].text = YAML.stringify(newData);
}
}
// If nothing is left, let's just delete this whole block
if (Object.keys(newData).length === 0) {
return null;
}
}
// Find a fenced code block with `meta` as the language type
if (t.type !== "FencedCode") {
return;
}
2022-10-14 13:11:33 +00:00
const codeInfoNode = findNodeOfType(t, "CodeInfo");
if (!codeInfoNode) {
return;
}
if (codeInfoNode.children![0].text !== "meta") {
return;
}
2022-10-14 13:11:33 +00:00
const codeTextNode = findNodeOfType(t, "CodeText");
if (!codeTextNode) {
// Honestly, this shouldn't happen
return;
}
2022-10-14 13:11:33 +00:00
const codeText = codeTextNode.children![0].text!;
const parsedData: any = YAML.parse(codeText);
const newData = { ...parsedData };
data = { ...data, ...parsedData };
2022-05-06 16:55:04 +00:00
if (removeKeys.length > 0) {
let removedOne = false;
2022-10-14 13:11:33 +00:00
for (const key of removeKeys) {
if (key in newData) {
delete newData[key];
removedOne = true;
}
2022-05-06 16:55:04 +00:00
}
if (removedOne) {
codeTextNode.children![0].text = YAML.stringify(newData).trim();
2022-08-08 11:09:19 +00:00
}
2022-05-06 16:55:04 +00:00
}
// If nothing is left, let's just delete this whole block
if (Object.keys(newData).length === 0) {
return null;
}
2022-05-06 16:55:04 +00:00
return undefined;
});
return data;
}
export async function queryProvider({
query,
}: QueryProviderEvent): Promise<any[]> {
2022-10-14 13:11:33 +00:00
const allData: any[] = [];
for (const { key, page, value } of await index.queryPrefix("data:")) {
const [, pos] = key.split("@");
allData.push({
...value,
page: page,
pos: +pos,
});
}
return applyQuery(query, allData);
}