Files
plainleaf/plugs/index/space_lua.ts
T
Zef Hemel 0773df31e2 Add validation and error handling for object indexing
- Introduced a new `validate` callback for `tag.define`
- Added `index.validateObjects` API to validate and transform objects before indexing.
- Updated relevant documentation and examples to reflect these changes.
2026-02-03 15:59:04 +01:00

51 lines
1.3 KiB
TypeScript

import {
collectNodesOfType,
findNodeOfType,
type ParseTree,
} from "@silverbulletmd/silverbullet/lib/tree";
import type {
ObjectValue,
PageMeta,
} from "@silverbulletmd/silverbullet/type/index";
import type { FrontMatter } from "./frontmatter.ts";
export type SpaceLuaObject = ObjectValue<{
script: string;
priority?: number;
}>;
export function indexSpaceLua(
pageMeta: PageMeta,
_frontmatter: FrontMatter,
tree: ParseTree,
) {
const allSpaceLuas: SpaceLuaObject[] = [];
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!;
// Parse out "-- priority: <number>"
const priority = codeText.match(/--\s*priority:\s*(-?\d+)/)?.[1];
allSpaceLuas.push({
ref: `${pageMeta.name}@${t.from!}`,
tag: "space-lua",
range: [t.from!, t.to!],
script: codeText,
priority: priority !== undefined ? +priority : undefined,
});
});
return Promise.resolve(allSpaceLuas);
}