Fix #641 format JSON output
Some checks are pending
Deploy to GitHub Pages / build (push) Waiting to run
Deploy to GitHub Pages / deploy (push) Blocked by required conditions

This commit is contained in:
Misode
2024-12-10 18:03:00 +01:00
parent 7ceb74fa15
commit 1860f86cb0

View File

@@ -1,7 +1,7 @@
import { NbtTag } from 'deepslate'
import yaml from 'js-yaml'
import { Store } from '../Store.js'
import { jsonToNbt, safeJsonParse } from '../Utils.js'
import { jsonToNbt, message, safeJsonParse } from '../Utils.js'
const INDENTS: Record<string, number | string | undefined> = {
'2_spaces': 2,
@@ -16,7 +16,15 @@ const FORMATS: Record<string, {
}> = {
json: {
parse: (s) => s,
stringify: (s) => s,
stringify: (s, i) => {
try {
const data = JSON.parse(s)
return JSON.stringify(data, null, i)
} catch (e) {
console.warn(`Failed to format JSON output. Falling back to source. ${message(e)}`)
return s
}
},
},
snbt: {
parse: (s) => JSON.stringify(NbtTag.fromString(s).toSimplifiedJson(), null, 2),