Last active
July 2, 2026 13:29
-
-
Save zlataovce/18bd7001bcacdfce05b900b9873ec024 to your computer and use it in GitHub Desktop.
A slicer script for formatting JSON.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const indentSizeOption = { | |
| type: "radio", | |
| id: "json-formatter/indent-size", | |
| label: "Indent size", | |
| items: [ | |
| { | |
| id: "2", | |
| label: "2 spaces", | |
| }, | |
| { | |
| id: "4", | |
| label: "4 spaces", | |
| }, | |
| { | |
| id: "8", | |
| label: "8 spaces", | |
| }, | |
| ], | |
| selected: "4", | |
| }; | |
| const utf8Decoder = new TextDecoder(); | |
| const utf8Encoder = new TextEncoder(); | |
| export default { | |
| name: "JSON formatter", | |
| description: "Formats JSON.", | |
| version: "1.0.0", | |
| options: [indentSizeOption], | |
| load(context) { | |
| context.addEventListener("transform", (e) => { | |
| if (e.name.endsWith(".json")) { | |
| const data = utf8Decoder.decode(e.data); | |
| const formatted = JSON.stringify(JSON.parse(data), null, parseInt(indentSizeOption.selected, 10)); | |
| e.data = utf8Encoder.encode(formatted); | |
| } | |
| }); | |
| }, | |
| unload() {}, | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment