Skip to content

Commit f9cb085

Browse files
committed
return json formatted strings sent to notice as a formatted code block
1 parent bdac802 commit f9cb085

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/components/Notice/Notice.jsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,29 @@ const Notice = ({ alert, buttonProps, dismissible, withBackButton }) => {
1616
{title && (
1717
<Alert.Heading>{title}</Alert.Heading>
1818
)}
19-
{body.map((text, index) => (
20-
<p className='mb-0' key={index}>{text}</p>
21-
))}
19+
{body.map((text, index) => {
20+
// If "text" is a JSON string, return it as a formatted code block
21+
// Otherwise, return it as a regular string
22+
const isJSONParsable = () => {
23+
try {
24+
JSON.parse(text)
25+
return true
26+
} catch (error) {
27+
return false
28+
}
29+
}
30+
const isJavascriptObject = isJSONParsable(text)
31+
32+
return (
33+
<>
34+
{isJavascriptObject ? (
35+
<pre className='mb-0' key={index}>{text}</pre>
36+
) : (
37+
<p className='mb-0' key={index}>{text}</p>
38+
)}
39+
</>
40+
)
41+
})}
2242
{withBackButton && (
2343
<>
2444
<hr />

0 commit comments

Comments
 (0)