silverbullet/plug-api/lib/attribute.ts

61 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-07-25 01:54:31 +08:00
import {
findNodeOfType,
ParseTree,
renderToText,
2023-07-26 23:12:56 +08:00
replaceNodesMatchingAsync,
2024-02-29 22:23:05 +08:00
} from "./tree.ts";
2023-07-25 01:54:31 +08:00
import { system, YAML } from "$sb/syscalls.ts";
2023-07-26 23:12:56 +08:00
2023-07-25 01:54:31 +08:00
/**
* Extracts attributes from a tree, optionally cleaning them out of the tree.
* @param tree tree to extract attributes from
* @param clean whether or not to clean out the attributes from the tree
* @returns mapping from attribute name to attribute value
*/
2023-07-26 23:12:56 +08:00
export async function extractAttributes(
tags: string[],
2023-07-25 01:54:31 +08:00
tree: ParseTree,
clean: boolean,
2023-07-26 23:12:56 +08:00
): Promise<Record<string, any>> {
let attributes: Record<string, any> = {};
2023-07-26 23:12:56 +08:00
await replaceNodesMatchingAsync(tree, async (n) => {
2023-07-25 01:54:31 +08:00
if (n.type === "ListItem") {
// Find top-level only, no nested lists
return n;
}
if (n.type === "Attribute") {
const nameNode = findNodeOfType(n, "AttributeName");
const valueNode = findNodeOfType(n, "AttributeValue");
if (nameNode && valueNode) {
2023-07-26 23:12:56 +08:00
const name = nameNode.children![0].text!;
const val = valueNode.children![0].text!;
try {
attributes[name] = await YAML.parse(val);
} catch (e: any) {
console.error("Error parsing attribute value as YAML", val, e);
2023-07-25 01:54:31 +08:00
}
}
// Remove from tree
if (clean) {
return null;
} else {
return n;
}
}
// Go on...
return undefined;
});
const text = renderToText(tree);
const spaceScriptAttributes = await system.applyAttributeExtractors(
tags,
text,
tree,
);
attributes = {
...attributes,
...spaceScriptAttributes,
};
2023-07-25 01:54:31 +08:00
return attributes;
}