2024-07-30 23:33:33 +08:00
|
|
|
import type { IndexTreeEvent } from "../../plug-api/types.ts";
|
2024-02-29 22:23:05 +08:00
|
|
|
import { collectNodesOfType, findNodeOfType } from "../../plug-api/lib/tree.ts";
|
2024-07-30 23:33:33 +08:00
|
|
|
import type { ObjectValue } from "../../plug-api/types.ts";
|
2024-02-06 23:51:04 +08:00
|
|
|
import { indexObjects } from "./api.ts";
|
|
|
|
|
|
|
|
export type ScriptObject = ObjectValue<{
|
|
|
|
script: string;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
export async function indexSpaceScript({ name, tree }: IndexTreeEvent) {
|
|
|
|
const allScripts: ScriptObject[] = [];
|
|
|
|
collectNodesOfType(tree, "FencedCode").map((t) => {
|
|
|
|
const codeInfoNode = findNodeOfType(t, "CodeInfo");
|
|
|
|
if (!codeInfoNode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const fenceType = codeInfoNode.children![0].text!;
|
|
|
|
if (fenceType !== "space-script") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const codeTextNode = findNodeOfType(t, "CodeText");
|
|
|
|
if (!codeTextNode) {
|
|
|
|
// Honestly, this shouldn't happen
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const codeText = codeTextNode.children![0].text!;
|
|
|
|
allScripts.push({
|
|
|
|
ref: `${name}@${t.from!}`,
|
|
|
|
tag: "space-script",
|
|
|
|
script: codeText,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
await indexObjects<ScriptObject>(name, allScripts);
|
|
|
|
}
|
2024-10-03 23:55:51 +08:00
|
|
|
|
|
|
|
export async function indexSpaceLua({ name, tree }: IndexTreeEvent) {
|
|
|
|
const allScripts: ScriptObject[] = [];
|
|
|
|
collectNodesOfType(tree, "FencedCode").map((t) => {
|
|
|
|
const codeInfoNode = findNodeOfType(t, "CodeInfo");
|
|
|
|
if (!codeInfoNode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const fenceType = codeInfoNode.children![0].text!;
|
|
|
|
if (fenceType !== "space-lua") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const codeTextNode = findNodeOfType(t, "CodeText");
|
|
|
|
if (!codeTextNode) {
|
|
|
|
// Honestly, this shouldn't happen
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const codeText = codeTextNode.children![0].text!;
|
|
|
|
allScripts.push({
|
|
|
|
ref: `${name}@${t.from!}`,
|
|
|
|
tag: "space-lua",
|
|
|
|
script: codeText,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
await indexObjects<ScriptObject>(name, allScripts);
|
|
|
|
}
|