-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.html
More file actions
294 lines (280 loc) · 12 KB
/
Copy pathtemplate.html
File metadata and controls
294 lines (280 loc) · 12 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{TITLE}} — PRD</title>
<script src="https://cdn.jsdelivr.net/npm/marked@14/marked.min.js"></script>
<style>
:root {
--paper: #faf8f4;
--card: #fffdf9;
--ink: #2b2a28;
--muted: #7c756b;
--line: #e7e0d4;
--accent: #b5532f;
--accent-soft: #f3e6df;
--code-bg: #f4efe6;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
background: var(--paper);
color: var(--ink);
font: 16px/1.65 Georgia, "Iowan Old Style", "Times New Roman", serif;
}
.topbar {
display: flex; align-items: baseline; gap: 14px;
padding: 12px 22px; border-bottom: 1px solid var(--line);
background: var(--card); position: sticky; top: 0; z-index: 10;
}
.topbar .title { font-weight: 700; font-size: 18px; }
.topbar .path { color: var(--muted); font-size: 12px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.topbar #chat-toggle {
margin-left: auto; font: 13px system-ui, sans-serif; cursor: pointer;
border: 1px solid var(--line); background: var(--paper); color: var(--ink);
padding: 5px 12px; border-radius: 6px;
}
.layout { display: grid; grid-template-columns: minmax(0,1fr) 380px; height: calc(100% - 49px); }
.layout.chat-hidden { grid-template-columns: minmax(0,1fr) 0; }
.doc { overflow-y: auto; padding: 28px 40px 120px; max-width: 860px; margin: 0 auto; width: 100%; }
.doc h1 { font-size: 30px; line-height: 1.2; margin: 8px 0 18px; }
.doc h2, .doc h3 { cursor: pointer; position: relative; padding-left: 20px; margin-top: 30px; }
.doc h2 { font-size: 22px; border-bottom: 1px solid var(--line); padding-bottom: 6px; }
.doc h3 { font-size: 18px; }
.doc h2::before, .doc h3::before {
content: "▾"; position: absolute; left: 0; color: var(--accent);
font-size: 12px; top: 0.35em; transition: transform .15s;
}
.doc h2.collapsed::before, .doc h3.collapsed::before { transform: rotate(-90deg); }
.doc code { background: var(--code-bg); padding: 1px 5px; border-radius: 4px; font-size: 0.88em; }
.doc pre { background: var(--code-bg); padding: 14px 16px; border-radius: 8px; overflow-x: auto; }
.doc pre code { background: none; padding: 0; }
.doc blockquote { border-left: 3px solid var(--accent); margin: 0; padding: 2px 16px; color: var(--muted); }
.doc table { border-collapse: collapse; width: 100%; font-size: 0.95em; }
.doc th, .doc td { border: 1px solid var(--line); padding: 7px 10px; text-align: left; }
.doc th { background: var(--accent-soft); }
.doc a { color: var(--accent); }
.doc ul.contains-task-list, .doc li.task-list-item { list-style: none; }
.doc li.task-list-item { margin-left: -20px; }
.doc input[type=checkbox] { width: 15px; height: 15px; margin-right: 8px; accent-color: var(--accent); cursor: pointer; vertical-align: -2px; }
.mermaid {
background: var(--card); border: 1px solid var(--line); border-radius: 10px;
padding: 18px; margin: 18px 0; text-align: center; overflow-x: auto;
}
.chat {
border-left: 1px solid var(--line); background: var(--card);
display: flex; flex-direction: column; min-width: 0;
font: 14px/1.5 system-ui, -apple-system, sans-serif;
}
.layout.chat-hidden .chat { display: none; }
.chat-head { padding: 12px 16px; font-weight: 600; border-bottom: 1px solid var(--line); }
.chat-log { flex: 1; overflow-y: auto; padding: 14px 16px; display: flex; flex-direction: column; gap: 12px; }
.msg { padding: 9px 12px; border-radius: 10px; white-space: pre-wrap; word-wrap: break-word; }
.msg.user { background: var(--accent-soft); align-self: flex-end; max-width: 88%; }
.msg.assistant { background: var(--paper); border: 1px solid var(--line); align-self: flex-start; max-width: 95%; }
.msg.assistant.error { background: #fbeae6; border-color: #e8b4a6; color: #8a2c12; }
#chat-form { display: flex; flex-direction: column; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); }
#chat-input { resize: vertical; min-height: 56px; font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--paper); color: var(--ink); }
#chat-form button { font: inherit; cursor: pointer; background: var(--accent); color: #fff; border: none; padding: 8px; border-radius: 8px; }
#chat-form button:disabled { opacity: .5; cursor: default; }
.chat-status { padding: 0 16px 10px; color: var(--muted); font-size: 12px; min-height: 16px; }
@media (max-width: 760px) {
.layout { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
.chat { border-left: none; border-top: 1px solid var(--line); max-height: 45vh; }
}
</style>
</head>
<body>
<header class="topbar">
<span class="title">{{TITLE}}</span>
<span class="path" id="prd-path">{{PRD_PATH}}</span>
<button id="chat-toggle">Hide chat</button>
</header>
<div class="layout" id="layout">
<main id="doc" class="doc"></main>
<aside class="chat" id="chat">
<div class="chat-head">Ask about this PRD</div>
<div class="chat-log" id="chat-log"></div>
<form id="chat-form">
<textarea id="chat-input" placeholder="Ask a question, or request an edit to the PRD…"></textarea>
<button type="submit">Send</button>
</form>
<div class="chat-status" id="chat-status"></div>
</aside>
</div>
<script id="prd-data" type="application/json">{{PRD_JSON}}</script>
<script type="module">
const marked = window.marked;
marked.setOptions({ gfm: true });
let prdMd = JSON.parse(document.getElementById('prd-data').textContent);
const served = location.protocol.startsWith('http');
let mermaidLib = null;
function renderDoc(md) {
const doc = document.getElementById('doc');
doc.innerHTML = marked.parse(md);
makeCollapsible(doc);
wireCheckboxes(doc);
upgradeMermaid(doc);
}
async function upgradeMermaid(root) {
const blocks = root.querySelectorAll('code.language-mermaid');
if (!blocks.length) return;
blocks.forEach((code) => {
const div = document.createElement('div');
div.className = 'mermaid';
div.textContent = code.textContent;
code.closest('pre').replaceWith(div);
});
try {
if (!mermaidLib) {
mermaidLib = (await import('https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs')).default;
mermaidLib.initialize({ startOnLoad: false, theme: 'neutral' });
}
await mermaidLib.run({ querySelector: '.mermaid' });
} catch (e) {
root.querySelectorAll('.mermaid').forEach(d => { d.style.color = 'var(--muted)'; });
}
}
function applyCollapsed(h, collapsed) {
const level = +h.tagName[1];
h.classList.toggle('collapsed', collapsed);
let el = h.nextElementSibling;
while (el && !(/^H[1-6]$/.test(el.tagName) && +el.tagName[1] <= level)) {
el.style.display = collapsed ? 'none' : '';
el = el.nextElementSibling;
}
}
function makeCollapsible(root) {
root.querySelectorAll('h2, h3').forEach((h) => {
h.addEventListener('click', () => applyCollapsed(h, !h.classList.contains('collapsed')));
});
}
// Capture/restore which sections are collapsed, keyed by heading, so an
// edit-triggered re-render doesn't reset the reader's view.
const headKey = (h) => h.tagName + ':' + h.textContent.trim();
function getCollapsedKeys(root) {
const set = new Set();
root.querySelectorAll('h2.collapsed, h3.collapsed').forEach((h) => set.add(headKey(h)));
return set;
}
function restoreCollapsed(root, keys) {
if (!keys || !keys.size) return;
root.querySelectorAll('h2, h3').forEach((h) => { if (keys.has(headKey(h))) applyCollapsed(h, true); });
}
function wireCheckboxes(root) {
// marked@14 emits a bare <li><input type=checkbox disabled> with no classes —
// add them so the bullet-suppression CSS applies, enable the box, and persist
// by stable index (item text can duplicate or nest, which collides).
root.querySelectorAll('li input[type=checkbox]').forEach((b, i) => {
b.disabled = false;
const li = b.closest('li');
if (li) { li.classList.add('task-list-item'); li.parentElement?.classList.add('contains-task-list'); }
const key = 'prd-check:' + location.pathname + ':' + i;
if (localStorage.getItem(key) === '1') b.checked = true;
b.addEventListener('change', () => localStorage.setItem(key, b.checked ? '1' : '0'));
});
}
// Chat
const log = document.getElementById('chat-log');
const statusEl = document.getElementById('chat-status');
const form = document.getElementById('chat-form');
const input = document.getElementById('chat-input');
const sendBtn = form.querySelector('button');
document.getElementById('chat-toggle').addEventListener('click', () => {
const layout = document.getElementById('layout');
const hidden = layout.classList.toggle('chat-hidden');
document.getElementById('chat-toggle').textContent = hidden ? 'Show chat' : 'Hide chat';
});
if (!served) {
input.disabled = true; sendBtn.disabled = true;
statusEl.textContent = 'Chat needs the local server. Run: node ~/.claude/skills/prd-html/serve.js <this file>.html';
}
function addMsg(role, text) {
const d = document.createElement('div');
d.className = 'msg ' + role;
d.textContent = text;
log.appendChild(d);
log.scrollTop = log.scrollHeight;
return d;
}
form.addEventListener('submit', async (e) => {
e.preventDefault();
const message = input.value.trim();
if (!message || sendBtn.disabled) return;
input.value = '';
addMsg('user', message);
const pending = addMsg('assistant', '…');
sendBtn.disabled = true;
statusEl.textContent = 'Claude is working…';
let acc = '';
try {
const res = await fetch('/chat', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ message }),
});
if (!res.ok) {
let msg = 'HTTP ' + res.status;
try { msg = (await res.json()).error || msg; } catch {}
throw new Error(msg);
}
// Read the SSE stream: {delta} frames build the reply live; {done}/{error} end it.
const reader = res.body.getReader();
const dec = new TextDecoder();
let sse = '', finalReply = null, errMsg = null;
for (;;) {
const { value, done } = await reader.read();
if (done) break;
sse += dec.decode(value, { stream: true });
let i;
while ((i = sse.indexOf('\n\n')) >= 0) {
const frame = sse.slice(0, i); sse = sse.slice(i + 2);
const dl = frame.split('\n').find((l) => l.startsWith('data:'));
if (!dl) continue;
let obj; try { obj = JSON.parse(dl.slice(5).trim()); } catch { continue; }
if (obj.delta != null) {
acc += obj.delta;
pending.textContent = acc;
log.scrollTop = log.scrollHeight;
} else if (obj.error) {
errMsg = obj.error;
} else if (obj.done) {
finalReply = obj.reply;
}
}
}
if (errMsg) throw new Error(errMsg);
pending.textContent = acc || finalReply || '(no reply)';
await refreshDoc();
} catch (err) {
pending.textContent = 'Error: ' + err.message;
pending.classList.add('error');
} finally {
sendBtn.disabled = false;
statusEl.textContent = '';
}
});
async function refreshDoc() {
if (!served) return;
try {
const res = await fetch('/prd.md', { cache: 'no-store' });
if (!res.ok) return;
const md = await res.text();
if (md && md !== prdMd) {
// Re-render to show the edit, but keep the reader's view: preserve which
// sections are collapsed and the scroll position so nothing visibly resets.
const doc = document.getElementById('doc');
const collapsed = getCollapsedKeys(doc);
const scroll = doc.scrollTop;
prdMd = md;
renderDoc(prdMd);
restoreCollapsed(doc, collapsed);
doc.scrollTop = scroll;
}
} catch {}
}
renderDoc(prdMd);
</script>
</body>
</html>