Skip to content

Commit 2c8761f

Browse files
committed
fix raw string output from OJS response
1 parent dc925e5 commit 2c8761f

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

chapters/what-is-an-api.qmd

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,24 @@ async function getCatData(catId) {
4545
4646
catData = await getCatData("XFhRpYS_D");
4747
48-
JSON.stringify(catData, null, 2);
48+
viewof catDataString = {
49+
const pre = html`<pre style="background-color: #f8f9fa; padding: 15px; border-radius: 5px; overflow-x: auto;">`;
50+
const code = html`<code style="color: #333;">`;
51+
52+
// Formatted JSON with syntax highlighting
53+
const formatted = JSON.stringify(catData, null, 2)
54+
.replace(/&/g, '&amp;')
55+
.replace(/</g, '&lt;')
56+
.replace(/>/g, '&gt;')
57+
.replace(/"([^"]+)":/g, '<span style="color: #a31515;">"$1"</span>:')
58+
.replace(/: "([^"]+)"/g, ': <span style="color: #008000;">"$1"</span>')
59+
.replace(/: ([0-9]+)/g, ': <span style="color: #0000FF;">$1</span>')
60+
.replace(/\b(true|false|null)\b/g, '<span style="color: #FF0000;">$1</span>');
61+
62+
code.innerHTML = formatted;
63+
pre.appendChild(code);
64+
return pre;
65+
}
4966
```
5067

5168
This format is called JSON, and its widely used in APIs to return data. We will see more about this format later, but for now, we can say that this output is called a **response**, and it contains **keys** and **values**. Keys are the names of the information we requested and values are the information we received. For instance, in this response, the url to the image of a Ragdoll cat can be found under the `url` key, and its value is `https://cdn2.thecatapi.com/images/XFhRpYS_D.jpg`.

0 commit comments

Comments
 (0)