From 81c3de807d84e7ba5c134213544439ea5689455f Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Sat, 28 Jun 2025 07:29:24 -0700 Subject: [PATCH] Add json.minify --- plugins/template-function-json/src/index.ts | 41 +++++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/plugins/template-function-json/src/index.ts b/plugins/template-function-json/src/index.ts index 8383a0a1..5fb30e12 100755 --- a/plugins/template-function-json/src/index.ts +++ b/plugins/template-function-json/src/index.ts @@ -1,4 +1,4 @@ -import { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api'; +import type { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api'; import { JSONPath } from 'jsonpath-plus'; export const plugin: PluginDefinition = { @@ -7,7 +7,13 @@ export const plugin: PluginDefinition = { name: 'json.jsonpath', description: 'Filter JSON-formatted text using JSONPath syntax', args: [ - { type: 'text', name: 'input', label: 'Input', multiLine: true, placeholder: '{ "foo": "bar" }' }, + { + type: 'text', + name: 'input', + label: 'Input', + multiLine: true, + placeholder: '{ "foo": "bar" }', + }, { type: 'text', name: 'query', label: 'Query', placeholder: '$..foo' }, { type: 'checkbox', name: 'formatted', label: 'Format Output' }, ], @@ -28,7 +34,7 @@ export const plugin: PluginDefinition = { } else { return JSON.stringify(filtered); } - } catch (e) { + } catch { return null; } }, @@ -37,12 +43,39 @@ export const plugin: PluginDefinition = { name: 'json.escape', description: 'Escape a JSON string, useful when using the output in JSON values', args: [ - { type: 'text', name: 'input', label: 'Input', multiLine: true, placeholder: 'Hello "World"' }, + { + type: 'text', + name: 'input', + label: 'Input', + multiLine: true, + placeholder: 'Hello "World"', + }, ], async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise { const input = String(args.values.input ?? ''); return input.replace(/\\/g, '\\\\').replace(/"/g, '\\"'); }, }, + { + name: 'json.minify', + description: 'Remove unnecessary whitespace from a valid JSON string.', + args: [ + { + type: 'editor', + language: 'json', + name: 'input', + label: 'Input', + placeholder: '{ "foo": "bar" }', + }, + ], + async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise { + const input = String(args.values.input ?? ''); + try { + return JSON.stringify(JSON.parse(input)); + } catch { + return input; + } + }, + }, ], };