-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclaude_json_renderer.js
More file actions
259 lines (240 loc) · 8.19 KB
/
claude_json_renderer.js
File metadata and controls
259 lines (240 loc) · 8.19 KB
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
javascript:(function(){
/* @title: Render Conversation JSON as HTML */
/* @description: Format Claude conversation JSON with syntax highlighting, code blocks, and download functionality */
/* @domains: claude.ai */
/* Load required libraries dynamically */
function loadScript(url) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = url;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
/* Process the conversation data into HTML */
function getConversationPath(data) {
const messageMap = new Map(data.chat_messages.map(msg => [msg.uuid, msg]));
const path = [];
let currentMsg = messageMap.get(data.current_leaf_message_uuid);
while(currentMsg) {
path.unshift(currentMsg);
currentMsg = messageMap.get(currentMsg.parent_message_uuid);
}
return path;
}
/* Process message content parts into a single string */
function processMessageContent(message) {
if (message.content && Array.isArray(message.content)) {
return message.content
.filter(part => part.type === 'text')
.map(part => part.text)
.join('');
}
return message.text || '';
}
/* Format the conversation with markdown support */
function formatConversation(data, marked) {
const pathMessages = getConversationPath(data);
const title = data.name || 'Conversation';
let html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>${title}</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.fluid.classless.green.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github.min.css">
<style>
:root {
--human-bg: #f0f4f8;
--assistant-bg: #fff;
--border-color: #e2e8f0;
--text-color: #2d3748;
--meta-color: #718096;
}
body {
padding: 1rem;
max-width: 1280px;
margin: 0 auto;
background-color: #f8fafc;
color: var(--text-color);
line-height: 1.6;
}
@media(min-width: 768px) { body { padding: 2rem; } }
h1 { color: var(--text-color); font-size: 1.875rem; margin-bottom: 1rem; }
.meta {
color: var(--meta-color);
font-size: 0.875rem;
margin-bottom: 2rem;
padding-bottom: 1rem;
border-bottom: 1px solid var(--border-color);
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 1rem;
}
.meta-info { flex: 1; }
.download-btn {
background-color: #4a5568;
color: white;
padding: 0.5rem 1rem;
border-radius: 0.375rem;
text-decoration: none;
display: inline-flex;
align-items: center;
gap: 0.5rem;
font-size: 0.875rem;
}
.download-btn:hover { background-color: #2d3748; }
#conversation { display: flex; flex-direction: column; gap: 1rem; }
.message {
padding: 1rem;
border-radius: 0.5rem;
border: 1px solid var(--border-color);
overflow-x: auto;
}
.human { background-color: var(--human-bg); margin-right: 2rem; }
.assistant { background-color: var(--assistant-bg); margin-left: 2rem; }
.sender {
font-weight: 600;
margin-bottom: 0.5rem;
color: var(--text-color);
font-size: 0.875rem;
text-transform: uppercase;
letter-spacing: 0.05em;
}
.content { margin: 0; }
.content p { margin: 0.5em 0; }
pre {
margin: 0;
padding: 0;
}
pre code {
display: block;
border-radius: 0.375rem;
font-size: 0.875rem;
padding: 1rem !important;
background-color: #f7fafc !important;
border: 1px solid var(--border-color);
white-space: pre;
overflow-x: auto;
line-height: 1.5;
}
code {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}
.artifact {
margin: 1rem 0;
padding: 1rem;
border: 1px solid #e2e8f0;
border-radius: 0.5rem;
background-color: #f8fafc;
}
.artifact-header {
font-weight: 600;
margin-bottom: 0.5rem;
color: #4a5568;
}
.artifact-content { margin-top: 0.5rem; }
</style>
</head>
<body>
<h1>${title}</h1>
<div class="meta">
<div class="meta-info">
<p>Created: <span style="margin-left:4px;">
<a href="https://claude.ai/chat/${data.uuid}" target="_blank">
${new Date(data.created_at).toLocaleString('en-US', {dateStyle:'full', timeStyle:'long'})}
</a>
</span></p>
${data.project ? `<p>Project: ${data.project.name}</p>` : ''}
</div>
<a href="#" class="download-btn" onclick="downloadHTML()">
Download HTML
</a>
</div>
<div id="conversation">`;
/* Process each message */
pathMessages.forEach(message => {
let processedText = processMessageContent(message);
/* Handle artifacts */
processedText = processedText.replace(
/<antArtifact[^>]*identifier="([^"]*)"[^>]*title="([^"]*)"[^>]*>([\s\S]*?)<\/antArtifact>/g,
(match, identifier, title, content) => {
/* Clean and format the code content */
const cleanContent = content
.trim()
.split('\n')
.map(line => line.trim())
.join('\n')
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
/* Detect if content contains JSX/XML */
const hasJSX = cleanContent.includes('</') || cleanContent.includes('/>');
const language = hasJSX ? 'jsx' : 'javascript';
return `<div class="artifact">
<div class="artifact-header">${title}</div>
<div class="artifact-content">
<pre><code class="language-${language}">${cleanContent}</code></pre>
</div>
</div>`;
}
);
/* Convert markdown to HTML */
processedText = marked.parse(processedText);
html += `<div class="message ${message.sender}">
<div class="sender">${message.sender.charAt(0).toUpperCase() + message.sender.slice(1)}</div>
<div class="content">${processedText}</div>
</div>`;
});
html += `</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script>
hljs.highlightAll();
function downloadHTML() {
/* Hide download button before getting HTML content */
const downloadBtn = document.querySelector('.download-btn');
downloadBtn.style.display = 'none';
const htmlContent = document.documentElement.outerHTML;
/* Restore download button */
downloadBtn.style.display = '';
const blob = new Blob([htmlContent], { type: 'text/html' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = '${title.toLowerCase().replace(/[^a-z0-9]+/g, '-')}.html';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
</script>
</body>
</html>`;
return html;
}
/* Main execution */
async function main() {
try {
/* Load marked.js library */
await loadScript('https://cdn.jsdelivr.net/npm/marked/marked.min.js');
/* Configure marked */
marked.setOptions({
breaks: true,
gfm: true,
headerIds: false
});
const conversationData = JSON.parse(document.body.textContent);
const formattedHTML = formatConversation(conversationData, marked);
const newWindow = window.open('', '_blank');
newWindow.document.write(formattedHTML);
newWindow.document.close();
} catch(error) {
alert('Error processing conversation: ' + error.message);
}
}
main();
})();