mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-25 01:58:39 +02:00
Add json.minify
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
|
import type { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
|
||||||
import { JSONPath } from 'jsonpath-plus';
|
import { JSONPath } from 'jsonpath-plus';
|
||||||
|
|
||||||
export const plugin: PluginDefinition = {
|
export const plugin: PluginDefinition = {
|
||||||
@@ -7,7 +7,13 @@ export const plugin: PluginDefinition = {
|
|||||||
name: 'json.jsonpath',
|
name: 'json.jsonpath',
|
||||||
description: 'Filter JSON-formatted text using JSONPath syntax',
|
description: 'Filter JSON-formatted text using JSONPath syntax',
|
||||||
args: [
|
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: 'text', name: 'query', label: 'Query', placeholder: '$..foo' },
|
||||||
{ type: 'checkbox', name: 'formatted', label: 'Format Output' },
|
{ type: 'checkbox', name: 'formatted', label: 'Format Output' },
|
||||||
],
|
],
|
||||||
@@ -28,7 +34,7 @@ export const plugin: PluginDefinition = {
|
|||||||
} else {
|
} else {
|
||||||
return JSON.stringify(filtered);
|
return JSON.stringify(filtered);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -37,12 +43,39 @@ export const plugin: PluginDefinition = {
|
|||||||
name: 'json.escape',
|
name: 'json.escape',
|
||||||
description: 'Escape a JSON string, useful when using the output in JSON values',
|
description: 'Escape a JSON string, useful when using the output in JSON values',
|
||||||
args: [
|
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<string | null> {
|
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||||
const input = String(args.values.input ?? '');
|
const input = String(args.values.input ?? '');
|
||||||
return input.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
|
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<string | null> {
|
||||||
|
const input = String(args.values.input ?? '');
|
||||||
|
try {
|
||||||
|
return JSON.stringify(JSON.parse(input));
|
||||||
|
} catch {
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user