snapshot of https://github.com/mountain-loop/yaak.git (main, 2026-09-23; blobs > 131072B omitted)
This commit is contained in:
17
plugins/template-function-xml/package.json
Normal file
17
plugins/template-function-xml/package.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "@yaak/template-function-xml",
|
||||
"displayName": "XML Template Functions",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"description": "Template functions for working with XML data",
|
||||
"main": "build/index.js",
|
||||
"types": "src/index.ts",
|
||||
"scripts": {
|
||||
"build": "yaakcli build",
|
||||
"dev": "yaakcli dev"
|
||||
},
|
||||
"dependencies": {
|
||||
"@xmldom/xmldom": "^0.9.12",
|
||||
"xpath": "^0.0.34"
|
||||
}
|
||||
}
|
||||
87
plugins/template-function-xml/src/index.ts
Executable file
87
plugins/template-function-xml/src/index.ts
Executable file
@@ -0,0 +1,87 @@
|
||||
/* oxlint-disable no-base-to-string */
|
||||
import { DOMParser } from "@xmldom/xmldom";
|
||||
import type { CallTemplateFunctionArgs, Context, PluginDefinition } from "@yaakapp/api";
|
||||
import xpath from "xpath";
|
||||
|
||||
const RETURN_FIRST = "first";
|
||||
const RETURN_ALL = "all";
|
||||
const RETURN_JOIN = "join";
|
||||
|
||||
export const plugin: PluginDefinition = {
|
||||
templateFunctions: [
|
||||
{
|
||||
name: "xml.xpath",
|
||||
description: "Filter XML-formatted text using XPath syntax",
|
||||
previewArgs: ["query"],
|
||||
args: [
|
||||
{
|
||||
type: "text",
|
||||
name: "input",
|
||||
label: "Input",
|
||||
multiLine: true,
|
||||
placeholder: "<foo></foo>",
|
||||
},
|
||||
{
|
||||
type: "h_stack",
|
||||
inputs: [
|
||||
{
|
||||
type: "select",
|
||||
name: "result",
|
||||
label: "Return Format",
|
||||
defaultValue: RETURN_FIRST,
|
||||
options: [
|
||||
{ label: "First result", value: RETURN_FIRST },
|
||||
{ label: "All results", value: RETURN_ALL },
|
||||
{ label: "Join with separator", value: RETURN_JOIN },
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "join",
|
||||
type: "text",
|
||||
label: "Separator",
|
||||
optional: true,
|
||||
defaultValue: ", ",
|
||||
dynamic(_ctx, args) {
|
||||
return { hidden: args.values.result !== RETURN_JOIN };
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{ type: "text", name: "query", label: "Query", placeholder: "//foo" },
|
||||
],
|
||||
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||
try {
|
||||
const result = (args.values.result || RETURN_FIRST) as XPathResult;
|
||||
const join = args.values.join == null ? null : String(args.values.join);
|
||||
return filterXPath(String(args.values.input), String(args.values.query), result, join);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
export type XPathResult = "first" | "join" | "all";
|
||||
export function filterXPath(
|
||||
body: string,
|
||||
path: string,
|
||||
result: XPathResult,
|
||||
join: string | null,
|
||||
): string {
|
||||
// oxlint-disable-next-line no-explicit-any
|
||||
const doc: any = new DOMParser().parseFromString(body, "text/xml");
|
||||
const items = xpath.select(path, doc, false);
|
||||
|
||||
if (!Array.isArray(items)) {
|
||||
return String(items);
|
||||
}
|
||||
if (!Array.isArray(items) || result === "first") {
|
||||
return items[0] != null ? String(items[0].firstChild ?? "") : "";
|
||||
}
|
||||
if (result === "join") {
|
||||
return items.map((item) => String(item.firstChild ?? "")).join(join ?? "");
|
||||
}
|
||||
// Not sure what cases this happens in (?)
|
||||
return String(items);
|
||||
}
|
||||
3
plugins/template-function-xml/tsconfig.json
Normal file
3
plugins/template-function-xml/tsconfig.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json"
|
||||
}
|
||||
Reference in New Issue
Block a user