Skip to content

Commit 1cfb968

Browse files
committed
remove double escaping 2
1 parent e87f2e6 commit 1cfb968

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

src/components/Preview.vue

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -172,32 +172,58 @@ const submit = async (form: FormInstance, fn: () => void) => {
172172
if (!form) return
173173
fn(form).then(() => {})
174174
}
175-
const highlightCode = (json) => {
176-
if (json.error) {
177-
// Parse double-encoded JSON error messages to display them cleanly
178-
let errorObj = json.error
175+
// Helper function to recursively parse double-encoded JSON strings
176+
const parseDoubleEncodedJson = (obj: any): any => {
177+
if (obj === null || obj === undefined) {
178+
return obj
179+
}
179180
180-
// Check if the error message is a JSON string that needs parsing
181-
if (errorObj.message && typeof errorObj.message === 'string') {
182-
try {
183-
const parsedMessage = JSON.parse(errorObj.message)
184-
// If successful, replace the string with the parsed object
185-
errorObj = {
186-
...errorObj,
187-
...parsedMessage
188-
}
189-
} catch (e) {
190-
// If parsing fails, keep the original error object
181+
// If it's a string, try to parse it as JSON
182+
if (typeof obj === 'string') {
183+
try {
184+
const parsed = JSON.parse(obj)
185+
// Recursively parse the result in case it's triple-encoded or more
186+
return parseDoubleEncodedJson(parsed)
187+
} catch (e) {
188+
// If parsing fails, return the original string
189+
return obj
190+
}
191+
}
192+
193+
// If it's an array, recursively parse each element
194+
if (Array.isArray(obj)) {
195+
return obj.map(item => parseDoubleEncodedJson(item))
196+
}
197+
198+
// If it's an object, recursively parse each property
199+
if (typeof obj === 'object') {
200+
const result = {}
201+
for (const key in obj) {
202+
if (obj.hasOwnProperty(key)) {
203+
result[key] = parseDoubleEncodedJson(obj[key])
191204
}
192205
}
206+
return result
207+
}
208+
209+
// For other types (numbers, booleans, etc.), return as-is
210+
return obj
211+
}
212+
213+
const highlightCode = (json) => {
214+
if (json.error) {
215+
// Parse double-encoded JSON error messages to display them cleanly
216+
const errorObj = parseDoubleEncodedJson(json.error)
193217
194218
// Display the full OBP error object with proper formatting
195219
successResponseBody.value = hljs.lineNumbersValue(
196220
hljs.highlightAuto(JSON.stringify(errorObj, null, 4), ['JSON']).value
197221
)
198222
} else if (json) {
223+
// Parse double-encoded JSON in successful responses too
224+
const parsedJson = parseDoubleEncodedJson(json)
199225
successResponseBody.value = hljs.lineNumbersValue(
200-
hljs.highlightAuto(JSON.stringify(json, null, 4), ['JSON']).value
226+
hljs.highlightAuto(JSON.stringify(parsedJson, null, 4), ['JSON']).value
201227
)
202228
} else {
203229
successResponseBody.value = ''

0 commit comments

Comments
 (0)