File size: 9,037 Bytes
e62ce2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77eda8e
 
 
 
 
 
 
 
 
41b4040
 
e62ce2b
41b4040
77eda8e
 
41b4040
e62ce2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<script lang="ts">
    import { onMount, createEventDispatcher, tick, afterUpdate } from "svelte";
    import { Marked } from "marked";
    import { markedHighlight } from "marked-highlight";
    import hljs from 'highlight.js';
    import "highlight.js/styles/github.css"; // You can choose a different style

    export let value: any;
    export let depth = 0;
    export let is_root = false;
    export let is_last_item = true;
    export let key: string | number | null = null;
    export let open = false;
    export let theme_mode: "system" | "light" | "dark" = "system";
    export let show_indices = false;

    const dispatch = createEventDispatcher();
    let root_element: HTMLElement;
    let collapsed = open ? false : depth >= 3;
    let child_nodes: any[] = [];

    function is_collapsible(val: any): boolean {
        return val !== null && (typeof val === "object" || Array.isArray(val));
    }

    async function toggle_collapse(): Promise<void> {
        collapsed = !collapsed;
        await tick();
        dispatch("toggle", { collapsed, depth });
    }

    function get_collapsed_preview(val: any): string {
        if (Array.isArray(val)) return `Array(${val.length})`;
        if (typeof val === "object" && val !== null)
            return `Object(${Object.keys(val).length})`;
        return String(val);
    }

    const marked = new Marked(
      markedHighlight({
        langPrefix: 'hljs language-',
        highlight(code, lang, info) {
          const language = hljs.getLanguage(lang) ? lang : 'plaintext';
          return hljs.highlight(code, { language }).value;
        }
      })
    );

    function escapeXmlTagsOutsideCodeBlocks(value: string): string {
        const codeBlockRegex = /(```[\s\S]*?```)/g;
        const parts = value.split(codeBlockRegex);
        return parts.map(part => {
            if (part.startsWith('```') && part.endsWith('```')) {
                return part; // Do not escape inside code blocks
            }
            return part.replace(/</g, '&lt;').replace(/>/g, '&gt;'); // Escape outside code blocks
        }).join('');
    }

    function toMarkdown(value: string): string {
        // console.log("Render:" + value);
        const escapedValue = escapeXmlTagsOutsideCodeBlocks(value);
        //console.log("Escaped:" + escapedValue);
        const parsed = marked.parse(escapedValue);
        const parser = new DOMParser();
        const doc = parser.parseFromString(parsed, 'text/html');
        doc.querySelectorAll('code').forEach((codeElement) => {
            codeElement.setAttribute('style', 'border: 1px solid #d3d3d3; background-color: rgba(150,150,150,0.05);');
        });
        return doc.body.innerHTML;
    }

    $: if (is_collapsible(value)) {
        child_nodes = Object.entries(value);
    } else {
        child_nodes = [];
    }
    $: if (is_root && root_element) {
        updateLineNumbers();
    }

    function updateLineNumbers(): void {
        const lines = root_element.querySelectorAll(".line");
        lines.forEach((line, index) => {
            const line_number = line.querySelector(".line-number");
            if (line_number) {
                line_number.setAttribute("data-pseudo-content", (index + 1).toString());
                line_number?.setAttribute(
                    "aria-roledescription",
                    `Line number ${index + 1}`
                );
                line_number?.setAttribute("title", `Line number ${index + 1}`);
            }
        });
    }

    onMount(() => {
        if (is_root) {
            updateLineNumbers();
        }
    });

    afterUpdate(() => {
        if (is_root) {
            updateLineNumbers();
        }
    });
</script>

<div
    class="json-node"
    class:root={is_root}
    class:dark-mode={theme_mode === "dark"}
    bind:this={root_element}
    on:toggle
    style="--depth: {depth};"
>
    <div class="line" class:collapsed>
        <span class="line-number"></span>
        <span class="content">
            {#if is_collapsible(value)}
                <button
                    data-pseudo-content={collapsed ? "▶" : "▼"}
                    aria-label={collapsed ? "Expand" : "Collapse"}
                    class="toggle"
                    on:click={toggle_collapse}
                />
            {/if}

            {#if key !== null}
                <span class="key">"{key}"</span><span class="punctuation colon"
                    >:
                </span>
            {/if}
            {#if is_collapsible(value)}
                <span
                    class="punctuation bracket"
                    class:square-bracket={Array.isArray(value)}
                    >{Array.isArray(value) ? "[" : "{"}</span
                >
                {#if collapsed}
                    <button on:click={toggle_collapse} class="preview">
                        {get_collapsed_preview(value)}
                    </button>
                    <span
                        class="punctuation bracket"
                        class:square-bracket={Array.isArray(value)}
                        >{Array.isArray(value) ? "]" : "}"}</span
                    >
                {/if}
            {:else if typeof value === "string"}
                <span class="value">{@html toMarkdown(value)}</span>
            {:else if typeof value === "number"}
                <span class="value number">{value}</span>
            {:else if typeof value === "boolean"}
                <span class="value bool">{value.toString()}</span>
            {:else if value === null}
                <span class="value null">null</span>
            {:else}
                <span>{value}</span>
            {/if}
            {#if !is_last_item && (!is_collapsible(value) || collapsed)}
                <span class="punctuation">,</span>
            {/if}
        </span>
    </div>

    {#if is_collapsible(value)}
        <div class="children" class:hidden={collapsed}>
            {#each child_nodes as [subKey, subVal], i}
                <svelte:self
                    value={subVal}
                    depth={depth + 1}
                    is_last_item={i === child_nodes.length - 1}
                    key={subKey}
                    {open}
                    {theme_mode}
                    {show_indices}
                    on:toggle
                />
            {/each}
            <div class="line">
                <span class="line-number"></span>
                <span class="content">
                    <span
                        class="punctuation bracket"
                        class:square-bracket={Array.isArray(value)}
                        >{Array.isArray(value) ? "]" : "}"}</span
                    >
                    {#if !is_last_item}<span class="punctuation">,</span>{/if}
                </span>
            </div>
        </div>
    {/if}
</div>

<style>
	.json-node {
		font-family: var(--font-mono);
		--text-color: #d18770;
		--key-color: var(--text-color);
		--string-color: #ce9178;
		--number-color: #719fad;

		--bracket-color: #5d8585;
		--square-bracket-color: #be6069;
		--punctuation-color: #8fbcbb;
		--line-number-color: #6a737d;
		--separator-color: var(--line-number-color);
	}
	.json-node.dark-mode {
		--bracket-color: #7eb4b3;
		--number-color: #638d9a;
	}
	.json-node.root {
		position: relative;
		padding-left: var(--size-14);
	}
	.json-node.root::before {
		content: "";
		position: absolute;
		top: 0;
		bottom: 0;
		left: var(--size-11);
		width: 1px;
		background-color: var(--separator-color);
	}
	.line {
		display: flex;
		align-items: flex-start;
		padding: 0;
		margin: 0;
		line-height: var(--line-md);
	}
	.line-number {
		position: absolute;
		left: 0;
		width: calc(var(--size-7));
		text-align: right;
		color: var(--line-number-color);
		user-select: none;
		text-overflow: ellipsis;
		text-overflow: ellipsis;
		direction: rtl;
		overflow: hidden;
	}
	.content {
		flex: 1;
		display: flex;
		align-items: center;
		padding-left: calc(var(--depth) * var(--size-2));
		flex-wrap: wrap;
	}
	.children {
		padding-left: var(--size-4);
	}
	.children.hidden {
		display: none;
	}
	.key {
		color: var(--key-color);
	}
	.string {
		color: var(--string-color);
	}
	.number {
		color: var(--number-color);
	}
	.bool {
		color: var(--text-color);
	}
	.null {
		color: var(--text-color);
	}
	.value {
		margin-left: var(--spacing-md);
	}
	.punctuation {
		color: var(--punctuation-color);
	}
	.bracket {
		margin-left: var(--spacing-sm);
		color: var(--bracket-color);
	}
	.square-bracket {
		margin-left: var(--spacing-sm);
		color: var(--square-bracket-color);
	}
	.toggle,
	.preview {
		background: none;
		border: none;
		color: inherit;
		cursor: pointer;
		padding: 0;
		margin: 0;
	}
	.toggle {
		user-select: none;
		margin-right: var(--spacing-md);
	}
	.preview {
		margin: 0 var(--spacing-sm) 0 var(--spacing-lg);
	}
	.preview:hover {
		text-decoration: underline;
	}

	:global([data-pseudo-content])::before {
		content: attr(data-pseudo-content);
	}
</style>