Gregory Schier
2025-07-10 13:49:53 -07:00
parent f63da432b9
commit a31f818424
12 changed files with 66 additions and 43 deletions

View File

@@ -1,5 +1,5 @@
import { DOMParser } from '@xmldom/xmldom';
import { PluginDefinition } from '@yaakapp/api';
import type { PluginDefinition } from '@yaakapp/api';
import xpath from 'xpath';
export const plugin: PluginDefinition = {
@@ -8,13 +8,16 @@ export const plugin: PluginDefinition = {
description: 'Filter XPath',
onFilter(_ctx, args) {
const doc = new DOMParser().parseFromString(args.payload, 'text/xml');
const result = xpath.select(args.filter, doc, false);
if (Array.isArray(result)) {
return { filtered: result.map(r => String(r)).join('\n') };
} else {
// Not sure what cases this happens in (?)
return { filtered: String(result) };
try {
const result = xpath.select(args.filter, doc, false);
if (Array.isArray(result)) {
return { content: result.map((r) => String(r)).join('\n') };
} else {
// Not sure what cases this happens in (?)
return { content: String(result) };
}
} catch (err) {
return { content: '', error: `Invalid filter: ${err}` };
}
},
},