-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
484 lines (462 loc) · 32.2 KB
/
Copy pathindex.html
File metadata and controls
484 lines (462 loc) · 32.2 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>Nexus</title>
<link rel="icon" href="favicon.svg" type="image/svg+xml">
<link rel="manifest" href="manifest.json">
<style>
/* Self-hosted NeXT fonts (the real WorldWideWeb typography, ~74KB total). */
@font-face{ font-family:NeXT; src:url("fonts/next-regular.woff2") format("woff2"); font-weight:normal; font-style:normal; font-display:swap; }
@font-face{ font-family:NeXT; src:url("fonts/next-bold.woff2") format("woff2"); font-weight:bold; font-style:normal; font-display:swap; }
@font-face{ font-family:NeXT; src:url("fonts/next-oblique.woff2") format("woff2"); font-weight:normal; font-style:italic; font-display:swap; }
@font-face{ font-family:NeXTHead; src:url("fonts/next-headline.woff2") format("woff2"); font-weight:bold; font-style:normal; font-display:swap; }
@font-face{ font-family:NeXTMono; src:url("fonts/next-mono.woff2") format("woff2"); font-display:swap; }
:root{ --steel:#9a9a9a; --win:#fff; --chrome:#bcbcbc; --chrome-d:#8a8a8a; --ink:#111; --line:#555; --sel:#aaddee; }
*{box-sizing:border-box}
html,body{height:100%;margin:0}
body{ font-family:NeXT,Helvetica,Arial,sans-serif; color:var(--ink);
background:#5e8585; /* NeXTSTEP teal workspace */
background-image:repeating-linear-gradient(45deg,rgba(0,0,0,.04) 0 2px,transparent 2px 4px);
overflow:hidden; user-select:none; -webkit-user-select:none; }
/* ---- floating NeXT menu (top-left vertical menu) ---- */
#menu{ position:fixed; top:8px; left:8px; width:150px; z-index:90; font-size:13px; }
.mtitle{ background:var(--chrome); border:1px solid var(--line); border-bottom:0;
text-align:center; font-weight:bold; padding:3px; box-shadow:inset 1px 1px 0 #fff, inset -1px -1px 0 var(--chrome-d); }
.mitem{ background:var(--chrome); border:1px solid var(--line); border-top:0; padding:4px 8px; cursor:pointer;
display:flex; justify-content:space-between; box-shadow:inset 1px 0 0 #fff, inset -1px -1px 0 var(--chrome-d); }
.mitem:hover{ background:#000; color:#fff; }
.mitem.off{ color:#777; cursor:default; }
.mitem.off:hover{ background:var(--chrome); color:#777; }
.mitem .k{ color:#666; } .mitem:hover .k{ color:#ccc; }
#marked{ margin-top:6px; background:#cfe8d8; border:1px solid #5e8585; font-size:11px; padding:4px 6px; word-break:break-all; }
#marked b{ display:block; color:#2f5d5d; } #marked a{ color:#2f5d5d; cursor:pointer; font-weight:bold; float:right; }
/* ---- windows ---- */
.win{ position:absolute; background:var(--win); border:1px solid #000;
box-shadow:2px 2px 0 rgba(0,0,0,.35); display:flex; flex-direction:column; min-width:240px; min-height:140px; }
.titlebar{ height:22px; background:linear-gradient(#cfcfcf,#aeaeae); border-bottom:1px solid var(--line);
display:flex; align-items:center; cursor:move;
box-shadow:inset 0 1px 0 #fff; }
.titlebar .tb-close{ width:16px; height:14px; margin:0 4px; background:linear-gradient(#e6e6e6,#c2c2c2);
border:1px solid var(--line); box-shadow:inset 1px 1px 0 #fff; cursor:pointer; flex:0 0 auto; }
.titlebar .tb-close:active{ background:#888; }
.titlebar .tb-title{ flex:1; text-align:center; font-weight:bold; font-size:12px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; padding-right:24px; }
.win.blur .titlebar{ background:#cfcfcf; }
.win.blur .titlebar .tb-title{ color:#777; }
.toolbar{ display:flex; gap:4px; align-items:center; padding:4px; background:var(--chrome); border-bottom:1px solid var(--line); }
.toolbar button{ font-family:inherit; font-size:12px; background:linear-gradient(#eaeaea,#cccccc); border:1px solid var(--line);
box-shadow:inset 1px 1px 0 #fff, inset -1px -1px 0 var(--chrome-d); padding:2px 7px; cursor:pointer; }
.toolbar button:active{ background:#999; box-shadow:inset -1px -1px 0 #fff; }
.toolbar button:disabled{ color:#888; box-shadow:none; cursor:default; background:#cfcfcf; }
.toolbar .url{ flex:1; font-family:NeXTMono,Courier,monospace; font-size:12px; border:1px solid var(--line);
box-shadow:inset 1px 1px 0 var(--chrome-d); padding:2px 5px; min-width:60px; user-select:text; }
.doc{ flex:1; overflow:auto; background:var(--win); padding:10px 14px; user-select:text; }
.doc.loading{ color:#777; font-style:italic; }
.doc.editing{ background:#fffbe6; outline:2px solid #5e8585; outline-offset:-2px; cursor:text; }
.toolbar .b-save{ background:linear-gradient(#bfe3c7,#8fce9f); }
.resize{ position:absolute; right:0; bottom:0; width:16px; height:16px; cursor:nwse-resize;
background:linear-gradient(135deg,transparent 45%,var(--chrome-d) 45% 55%,transparent 55%); }
/* ---- faithful WorldWideWeb document rendering (Tim's default.style, 1991) ---- */
.ntx-doc{ font-family:NeXT,Helvetica,Arial,sans-serif; font-size:12px; line-height:1.35; color:#000; }
.ntx-doc p, .ntx-doc div, .ntx-doc span{ font-size:12px; }
.ntx-doc h1{ font-family:NeXTHead,NeXT,Helvetica,sans-serif; font-weight:bold; font-size:18px; margin:.5em 0; }
.ntx-doc h2{ font-weight:bold; font-size:14px; margin:.5em 0; }
.ntx-doc h3{ font-style:italic; font-size:14px; margin:.5em 0; }
.ntx-doc h4{ font-weight:bold; font-size:12px; margin:.5em 0; }
.ntx-doc h5{ font-weight:bold; font-style:italic; font-size:12px; margin:.5em 0; }
.ntx-doc h6{ font-weight:bold; font-size:12px; margin:.5em 0; }
.ntx-doc address{ font-style:italic; font-size:12px; }
.ntx-doc pre, .ntx-doc code, .ntx-doc tt{ font-family:NeXTMono,Courier,monospace; font-size:12px; }
.ntx-doc ul, .ntx-doc ol, .ntx-doc dl{ margin:.4em 0 .4em 1.2em; }
.ntx-doc img{ max-width:100%; }
/* WorldWideWeb drew links as a boxed, underlined region */
.ntx-doc a{ color:#000; text-decoration:underline; border:1px solid #000; padding:0 1px; cursor:pointer; }
.ntx-doc a:hover{ background:var(--sel); }
#hint{ position:fixed; right:8px; bottom:6px; z-index:80; color:#eafafa; font-size:11px; opacity:.85; }
#hint a{ color:#fff; }
</style>
</head>
<body>
<div id="menu">
<div class="mtitle">WorldWideWeb</div>
<div class="mitem" id="m-open">Open…<span class="k">⌘O</span></div>
<div class="mitem" id="m-new">New Window<span class="k">⌘N</span></div>
<div class="mitem" id="m-home">Home<span class="k">⌘H</span></div>
<div class="mitem" id="m-edit">Edit / Save<span class="k">⌘E</span></div>
<div class="mitem" id="m-mark">Mark Document<span class="k">⌘M</span></div>
<div class="mitem" id="m-link">Link to Marked<span class="k">⌘L</span></div>
<div class="mitem" id="m-help">Help<span class="k">?</span></div>
<div id="marked" style="display:none"></div>
</div>
<div id="hint">Nexus · the read-write web · <a href="../../../">↑ root</a></div>
<script src="https://unpkg.com/xlogin"></script>
<script src="https://cdn.jsdelivr.net/npm/marked@4.2.12/marked.min.js"></script>
<script>
// Nexus - a faithful recreation of Tim Berners-Lee's 1990 WorldWideWeb browser
// (the NeXT app, later renamed "Nexus"), as a window manager over YOUR pod.
// Carbon-copy of the NeXTSTEP chrome + real WWW typography (Tim's default.style).
// P1 (this): browse - open a URL, render faithfully, boxed links, Back/Next, multi-window.
// P2: edit documents in place + save to the pod (the read-write turn).
// P3: the mark->link authoring workflow (make hypertext by hand, like the original).
"use strict";
var Z=10, WINS=[], FOCUSED=null, MARKED=null, desktop=document.body;
function me(){ return window.xlogin && window.xlogin.id; }
function authFetch(u,o){ return ((window.xlogin && window.xlogin.authFetch)||fetch)(u,o); }
function esc(s){ return String(s==null?"":s).replace(/[&<>"]/g,function(c){return {"&":"&","<":"<",">":">",'"':"""}[c];}); }
function podRoot(){ var x=window.xlogin; if(!x||!x.id)return null; return String(x.id).replace(/#.*$/,"").replace(/\/profile\/card$|\/[^\/]*$/,""); }
function homeUrl(){ var r=podRoot(); return r?r+"/":(location.href.replace(/[^\/]*$/,"")+"#about"); }
// ---- window model ----
function makeWindow(opts){
opts=opts||{};
var w={ id:"w"+(WINS.length+1)+"-"+Date.now(), url:null, hist:[], hi:-1,
x:opts.x||(40+WINS.length*26), y:opts.y||(40+WINS.length*26), w:opts.w||560, h:opts.h||420 };
var el=document.createElement("div"); el.className="win"; el.style.cssText=
"left:"+w.x+"px;top:"+w.y+"px;width:"+w.w+"px;height:"+w.h+"px;z-index:"+(++Z);
el.innerHTML=
'<div class="titlebar"><div class="tb-close" title="Close"></div><div class="tb-title">Document</div></div>'+
'<div class="toolbar">'+
'<button class="b-back" title="Back" disabled><</button>'+
'<button class="b-fwd" title="Next" disabled>></button>'+
'<input class="url" placeholder="Open URL (a pod resource, http(s)…)" spellcheck="false">'+
'<button class="b-go">Go</button>'+
'<button class="b-edit" title="Edit this document" disabled>Edit</button>'+
'<button class="b-save" title="Save to your pod" style="display:none">Save</button>'+
'<button class="b-revert" title="Discard changes" style="display:none">Revert</button>'+
'<button class="b-new" title="New window">New</button>'+
'</div>'+
'<div class="doc ntx-doc"></div><div class="resize"></div>';
desktop.appendChild(el);
w.el=el; w.titleEl=el.querySelector(".tb-title"); w.docEl=el.querySelector(".doc");
w.urlEl=el.querySelector(".url"); w.backB=el.querySelector(".b-back"); w.fwdB=el.querySelector(".b-fwd");
w.editB=el.querySelector(".b-edit"); w.saveB=el.querySelector(".b-save"); w.revertB=el.querySelector(".b-revert");
w.mode="view"; w.kind=null; w.raw=null; w.sourceCT=null; w.savedRange=null;
// remember the live text selection while editing, so "Link to Marked" survives a menu click
function saveSel(){ if(w.mode!=="edit")return; var s=window.getSelection(); if(s&&s.rangeCount&&!s.isCollapsed&&w.docEl.contains(s.anchorNode)) w.savedRange=s.getRangeAt(0).cloneRange(); }
w.docEl.addEventListener("mouseup",saveSel); w.docEl.addEventListener("keyup",saveSel);
el.addEventListener("mousedown",function(){ focusWin(w); });
el.querySelector(".tb-close").addEventListener("click",function(e){ e.stopPropagation(); closeWin(w); });
el.querySelector(".b-go").addEventListener("click",function(){ navigate(w, w.urlEl.value.trim()); });
w.editB.addEventListener("click",function(){ enterEdit(w); });
w.saveB.addEventListener("click",function(){ saveDoc(w); });
w.revertB.addEventListener("click",function(){ revertEdit(w); });
el.querySelector(".b-new").addEventListener("click",function(){ var nw=makeWindow(); navigate(nw, w.url||homeUrl()); });
w.urlEl.addEventListener("keydown",function(e){ if(e.key==="Enter") navigate(w, w.urlEl.value.trim()); });
w.urlEl.addEventListener("mousedown",function(e){ e.stopPropagation(); });
w.backB.addEventListener("click",function(){ go(w,-1); });
w.fwdB.addEventListener("click",function(){ go(w,1); });
dragify(w, el.querySelector(".titlebar"));
resizify(w, el.querySelector(".resize"));
WINS.push(w); focusWin(w);
return w;
}
function focusWin(w){ FOCUSED=w; WINS.forEach(function(o){ o.el.classList.add("blur"); }); w.el.classList.remove("blur"); w.el.style.zIndex=(++Z); }
function closeWin(w){ w.el.remove(); WINS=WINS.filter(function(o){return o!==w;}); }
function dragify(w, handle){
handle.addEventListener("mousedown",function(e){
if(e.target.classList.contains("tb-close"))return;
focusWin(w); var sx=e.clientX-w.x, sy=e.clientY-w.y;
function mv(ev){ w.x=Math.max(0,ev.clientX-sx); w.y=Math.max(0,ev.clientY-sy); w.el.style.left=w.x+"px"; w.el.style.top=w.y+"px"; }
function up(){ document.removeEventListener("mousemove",mv); document.removeEventListener("mouseup",up); }
document.addEventListener("mousemove",mv); document.addEventListener("mouseup",up); e.preventDefault();
});
}
function resizify(w, grip){
grip.addEventListener("mousedown",function(e){
e.stopPropagation(); focusWin(w); var sw=w.w, sh=w.h, sx=e.clientX, sy=e.clientY;
function mv(ev){ w.w=Math.max(240,sw+ev.clientX-sx); w.h=Math.max(140,sh+ev.clientY-sy); w.el.style.width=w.w+"px"; w.el.style.height=w.h+"px"; }
function up(){ document.removeEventListener("mousemove",mv); document.removeEventListener("mouseup",up); }
document.addEventListener("mousemove",mv); document.addEventListener("mouseup",up); e.preventDefault();
});
}
// ---- navigation ----
function setNavButtons(w){ w.backB.disabled=(w.hi<=0); w.fwdB.disabled=(w.hi>=w.hist.length-1); }
function go(w,delta){ var i=w.hi+delta; if(i<0||i>=w.hist.length)return; w.hi=i; load(w, w.hist[i]); }
function navigate(w, url){
if(!url) return;
if(url.indexOf("#about")>=0 || url==="#about"){ renderAbout(w); w.url="#about"; w.urlEl.value=""; w.hist=w.hist.slice(0,w.hi+1); w.hist.push("#about"); w.hi=w.hist.length-1; setNavButtons(w); return; }
// resolve relative to current doc
try{ url=new URL(url, w.url&&w.url!=="#about"?w.url:location.href).href; }catch(e){}
w.hist=w.hist.slice(0,w.hi+1); w.hist.push(url); w.hi=w.hist.length-1;
load(w, url);
}
function load(w, url){
w.mode="view"; w.docEl.contentEditable="false"; w.docEl.classList.remove("editing");
if(url==="#about"){ renderAbout(w); w.url=url; setNavButtons(w); return; }
w.url=url; w.urlEl.value=url; setNavButtons(w);
w.docEl.classList.add("loading"); w.docEl.textContent="Reading "+url+" …"; w.titleEl.textContent="…";
// In Solid, a container URL always ends in "/" — browse it as a clickable index.
if(/\/$/.test(url.split(/[?#]/)[0])) loadContainer(w, url); else loadResource(w, url);
}
function unreachable(w){ w.docEl.classList.remove("loading"); w.titleEl.textContent="Unreachable";
w.docEl.innerHTML='<h1>Unreachable</h1><p>Could not fetch this URL. If it\'s on another server it may block cross-origin reads (CORS). Your own pod works.</p>'; }
function loadResource(w, url){
authFetch(url,{headers:{Accept:"text/html,application/xhtml+xml,*/*"}}).then(function(r){
var ct=(r.headers.get("content-type")||"").toLowerCase();
return r.text().then(function(body){ return { ok:r.ok, status:r.status, ct:ct, body:body }; });
}).then(function(res){
w.docEl.classList.remove("loading");
if(!res.ok){
if(res.status===404 && me()){ // offer to create a new document here (the read-write web)
if(isMarkdown(url,"")){
renderMarkdown(w, "# "+shortTitle(url).replace(/\.(md|markdown)$/i,"")+"\n\n", url);
} else {
var tmpl="<!doctype html>\n<html><head><meta charset=\"utf-8\"><title>"+esc(shortTitle(url))+"</title></head>\n<body>\n<h1>"+esc(shortTitle(url))+"</h1>\n<p>New document.</p>\n</body></html>\n";
renderHTML(w, tmpl, url);
}
var note=document.createElement("p"); note.style.cssText="color:#777;font-style:italic";
note.textContent="This document doesn’t exist yet — click Edit, then Save to create it on your pod.";
w.docEl.appendChild(note); return;
}
w.titleEl.textContent="Error "+res.status; w.docEl.innerHTML='<h1>'+res.status+'</h1><p>Couldn\'t read this document.</p>'; w.kind=null; setEditUI(w); return;
}
if(isMarkdown(url, res.ct)){
renderMarkdown(w, res.body, url); // .md pages (the pages-app convention) -> WWW-styled, [[wikilinks]] live
} else if(/text\/html|xhtml|xml/.test(res.ct) || /^\s*<(!doctype|html|head|body|h1|p|ul)/i.test(res.body)){
renderHTML(w, res.body, url);
} else {
renderSource(w, url, res.body, res.ct); // data resource (turtle/json-ld/text) — source, with links live
}
}).catch(function(){ unreachable(w); });
}
// ---- LDP container -> clickable WWW-style index ----
function loadContainer(w, url){
authFetch(url,{headers:{Accept:"application/ld+json"}}).then(function(r){
var ct=(r.headers.get("content-type")||"").toLowerCase();
return r.text().then(function(body){ return { ok:r.ok, status:r.status, ct:ct, body:body }; });
}).then(function(res){
w.docEl.classList.remove("loading");
if(!res.ok){ w.titleEl.textContent="Error "+res.status; w.docEl.innerHTML='<h1>'+res.status+'</h1><p>Couldn\'t read this container.</p>'; w.kind=null; setEditUI(w); return; }
var data=tryJSON(res.body), items=data?containerChildren(data,url):null;
if(items) renderContainer(w, url, items); else renderSource(w, url, res.body, res.ct);
}).catch(function(){ unreachable(w); });
}
function tryJSON(s){ try{ return JSON.parse(s); }catch(e){ return null; } }
function jsubjects(d){ if(!d)return[]; if(Array.isArray(d))return d; if(Array.isArray(d["@graph"]))return d["@graph"]; return [d]; }
function jid(v){ if(v==null)return null; if(typeof v==="string")return v; if(Array.isArray(v))return v.length?jid(v[0]):null; return v["@id"]||null; }
function jval(v){ if(v==null)return null; if(typeof v!=="object")return v; if(Array.isArray(v))return v.length?jval(v[0]):null; return v["@value"]!=null?v["@value"]:null; }
function jtypes(n){ var t=n["@type"]||n["a"]; t=Array.isArray(t)?t:(t?[t]:[]); return t.map(function(x){return String(jid(x)||x);}); }
function isContainerType(n){ return jtypes(n).some(function(t){return /Container$/.test(t);}); }
function childName(id, base){ var n=id; try{ if(id.indexOf(base)===0) n=id.slice(base.length); else { var u=new URL(id); n=u.pathname.replace(/\/$/,"").split("/").pop()+(/\/$/.test(id)?"/":""); } }catch(e){} return n||id; }
function parentUrl(url){ try{ var u=new URL(url); if(u.pathname==="/"||u.pathname==="")return null; var parts=u.pathname.replace(/\/$/,"").split("/"); parts.pop(); return u.origin+(parts.join("/")||"")+"/"; }catch(e){ return null; } }
function containerChildren(data, url){
var subs=jsubjects(data), byId={}, entries=[], seen={};
subs.forEach(function(n){ var id=jid(n["@id"]); if(id)byId[id]=n; });
subs.forEach(function(n){
var c=n["contains"]||n["ldp:contains"]||n["http://www.w3.org/ns/ldp#contains"];
if(c){ (Array.isArray(c)?c:[c]).forEach(function(x){ var id=jid(x); if(id&&!seen[id]){ seen[id]=1;
entries.push({ id:id, node:(x&&typeof x==="object"&&!Array.isArray(x))?x:null }); } }); }
});
if(!entries.length && !subs.some(isContainerType)) return null; // not a container doc -> caller shows source
return entries.map(function(e){
var n=Object.assign({}, byId[e.id]||{}, e.node||{}); // metadata may be inline in contains OR a separate subject
var size=jval(n["stat:size"]||n["http://www.w3.org/ns/posix/stat#size"]);
var mod=jval(n["dcterms:modified"]||n["dct:modified"]||n["http://purl.org/dc/terms/modified"]);
return { url:e.id, name:childName(e.id,url), isC:(/\/$/.test(e.id)||isContainerType(n)), size:size, mod:mod };
}).sort(function(a,b){ if(a.isC!==b.isC)return a.isC?-1:1; return a.name<b.name?-1:1; });
}
function fmtSize(b){ b=+b; if(!b&&b!==0)return ""; if(b<1024)return b+" B"; if(b<1048576)return (b/1024).toFixed(1)+" KB"; return (b/1048576).toFixed(1)+" MB"; }
function renderContainer(w, url, items){
w.kind="container"; w.raw=null;
var name=childName(url.replace(/\/$/,"")+"/", parentUrl(url)||url)||shortTitle(url);
w.titleEl.textContent=name||"Index";
var parent=parentUrl(url);
var rows=items.map(function(it){
var meta=[it.isC?"folder":fmtSize(it.size), it.mod?String(it.mod).slice(0,10):""].filter(Boolean).join(" · ");
return '<li><a href="'+esc(it.url)+'">'+esc(it.name)+(it.isC&&!/\/$/.test(it.name)?"/":"")+'</a>'+(meta?' <span style="color:#777;font-size:11px">'+esc(meta)+'</span>':"")+'</li>';
}).join("");
w.docEl.innerHTML=
'<h2>'+esc(name||"Index")+'</h2>'+
'<address>'+esc(url)+'</address>'+
(parent?'<p><a href="'+esc(parent)+'">../ (parent)</a></p>':"")+
(items.length?'<ul>'+rows+'</ul>':'<p><i>Empty container.</i></p>');
w.docEl.querySelectorAll("a[href]").forEach(function(a){
a.addEventListener("click",function(e){ e.preventDefault(); navigate(w, a.getAttribute("href")); });
});
w.docEl.scrollTop=0; setEditUI(w);
}
// data resource as source, but with any URLs live (the whole pod stays click-through)
function linkifySource(text){
var re=/(https?:\/\/[^\s"'<>\\]+)/g, out="", last=0, m;
while((m=re.exec(text))){ out+=esc(text.slice(last,m.index))+'<a href="'+esc(m[1])+'">'+esc(m[1])+'</a>'; last=re.lastIndex; }
return out+esc(text.slice(last));
}
function renderSource(w, url, body, ct){
w.kind="source"; w.raw=body; w.sourceCT=ct||w.sourceCT||"text/plain";
w.titleEl.textContent=shortTitle(url);
w.docEl.innerHTML='<pre style="font-family:NeXTMono,Courier,monospace;font-size:12px;white-space:pre-wrap;word-break:break-word;margin:0;">'+linkifySource(body)+'</pre>';
w.docEl.querySelectorAll("a[href]").forEach(function(a){
a.addEventListener("click",function(e){ if(w.mode==="edit")return; e.preventDefault(); navigate(w, a.getAttribute("href")); });
});
w.docEl.scrollTop=0; setEditUI(w);
}
function shortTitle(url){ try{ var u=new URL(url); var p=u.pathname.replace(/\/$/,"").split("/").pop(); return p||u.hostname; }catch(e){ return url; } }
// ---- markdown pages (the pages-app convention: <name>.md, [[wiki links]]) ----
function isMarkdown(url, ct){ var path=(url||"").split(/[?#]/)[0]; return /\.(md|markdown)$/i.test(path) || /markdown/.test(ct||""); }
function dirOf(url){ try{ return new URL(".", url.split(/[?#]/)[0]).href; }catch(e){ return url; } }
function wikiToMd(md, baseUrl){ // [[Name]] / [[Name|label]] -> a markdown link to the sibling .md page
var dir=dirOf(baseUrl);
return String(md).replace(/\[\[([^\]|]+)(?:\|([^\]]+))?\]\]/g, function(_, name, label){
var n=name.trim(), target; try{ target=new URL(encodeURIComponent(n)+".md", dir).href; }catch(e){ target=encodeURIComponent(n)+".md"; }
return "["+(label||n)+"]("+target+")";
});
}
function renderMarkdown(w, md, url){
w.kind="markdown"; w.raw=md; w.sourceCT="text/markdown";
if(typeof marked==="undefined"){ renderSource(w, url, md, "text/markdown"); w.kind="markdown"; return; }
var html; try{ html=marked.parse(wikiToMd(md, url)); }catch(e){ renderSource(w, url, md, "text/markdown"); w.kind="markdown"; return; }
var doc; try{ doc=new DOMParser().parseFromString(html,"text/html"); }catch(e){ doc=null; }
if(doc){
doc.querySelectorAll("script,style,iframe,object,embed").forEach(function(n){ n.remove(); });
doc.querySelectorAll("a[href]").forEach(function(a){ try{ a.setAttribute("href", new URL(a.getAttribute("href"), url).href); }catch(e){} });
doc.querySelectorAll("img[src]").forEach(function(im){ try{ im.setAttribute("src", new URL(im.getAttribute("src"), url).href); }catch(e){} });
var h1=doc.querySelector("h1");
w.titleEl.textContent=(h1&&h1.textContent.trim())||shortTitle(url);
html=doc.body?doc.body.innerHTML:html;
} else { w.titleEl.textContent=shortTitle(url); }
w.docEl.innerHTML=html;
w.docEl.querySelectorAll("a[href]").forEach(function(a){
a.addEventListener("click",function(e){ if(w.mode==="edit")return; e.preventDefault(); navigate(w, a.getAttribute("href")); });
});
w.docEl.scrollTop=0; setEditUI(w);
}
function renderHTML(w, html, base){
w.kind="html"; w.raw=html; // keep the original source so Save can preserve <head>
var doc;
try{ doc=new DOMParser().parseFromString(html,"text/html"); }catch(e){ doc=null; }
if(!doc){ w.docEl.textContent=html; return; }
// remove anything executable/styling that could escape our chrome
doc.querySelectorAll("script,style,link,meta,iframe,object,embed,base").forEach(function(n){ n.remove(); });
// resolve links + images against the document's own URL
doc.querySelectorAll("a[href]").forEach(function(a){ try{ a.setAttribute("href", new URL(a.getAttribute("href"), base).href); }catch(e){} });
doc.querySelectorAll("img[src]").forEach(function(im){ try{ im.setAttribute("src", new URL(im.getAttribute("src"), base).href); }catch(e){} });
var title=(doc.querySelector("title")&&doc.querySelector("title").textContent.trim())||shortTitle(base);
w.titleEl.textContent=title;
w.docEl.innerHTML = doc.body ? doc.body.innerHTML : html;
// intercept link clicks -> navigate within this window (the WWW way); inert while editing
w.docEl.querySelectorAll("a[href]").forEach(function(a){
a.addEventListener("click",function(e){ if(w.mode==="edit")return; e.preventDefault(); var href=a.getAttribute("href"); if(href) navigate(w, href); });
});
w.docEl.scrollTop=0; setEditUI(w);
}
function renderAbout(w){
w.kind="about"; w.raw=null; w.mode="view"; w.docEl.contentEditable="false";
w.titleEl.textContent="Welcome to Nexus";
w.urlEl.value="";
w.docEl.classList.remove("loading");
w.docEl.innerHTML =
'<h1>WorldWideWeb</h1>'+
'<address>A recreation of the world\'s first browser — now over your own pod.</address>'+
'<p>This is <b>Nexus</b>: Tim Berners-Lee\'s 1990 <i>WorldWideWeb</i> browser, faithfully reborn '+
'on the NeXT, but pointed at <b>your data</b>. Open any document on your pod and follow its links.</p>'+
'<h2>Browsing</h2>'+
'<ul>'+
'<li>Type a URL in a window\'s bar and press <b>Go</b> — a pod resource, or any readable web page.</li>'+
'<li>Links are drawn <a href="#about">boxed and underlined</a>; click to follow them, like 1990.</li>'+
'<li><b>New</b> opens another window. Drag title bars to move; drag the corner to resize.</li>'+
'<li><b><</b> / <b>></b> walk back and forward through what you\'ve visited.</li>'+
'</ul>'+
'<h2>Editing — the read-write web</h2>'+
'<ul>'+
'<li>On any HTML document you can write to, hit <b>Edit</b> (or ⌘E), change the text, then <b>Save</b> — it\'s written straight back to your pod.</li>'+
'<li>Open a URL that doesn\'t exist yet on your pod and nexus offers to <b>create</b> it.</li>'+
'<li><b>Markdown</b> pages (<code>.md</code>, like the <i>pages</i> app) render in the WWW style — <code>[[wiki links]]</code> become clickable boxed links; Edit drops to the raw markdown, Save writes it back. Click a missing <code>[[link]]</code> to create that page.</li>'+
'<li>Data resources (Turtle, JSON-LD) are editable as source too.</li>'+
'</ul>'+
'<h2>Making links by hand</h2>'+
'<p>The signature WorldWideWeb move — author hypertext yourself:</p>'+
'<ul>'+
'<li>Open the page you want to link <i>to</i> and choose <b>Mark Document</b> (⌘M).</li>'+
'<li>Go to the page you\'re writing, hit <b>Edit</b>, select some text, then <b>Link to Marked</b> (⌘L) — nexus wraps it in a link to your marked target.</li>'+
'<li><b>Save</b>, and the link lives on your pod. You can even mark a page that doesn\'t exist yet, then create it.</li>'+
'</ul>'+
(me()?'<p>You\'re signed in. <a href="'+esc(homeUrl())+'">Open your pod →</a></p>'
:'<p><i>Sign in with the login pill to browse your own pod read-write.</i></p>');
w.docEl.querySelectorAll("a[href]").forEach(function(a){
a.addEventListener("click",function(e){ e.preventDefault(); navigate(w, a.getAttribute("href")); });
});
setEditUI(w);
}
// ---- edit in place + save to the pod (the read-write turn) ----
function setEditUI(w){
var editable=(w.kind==="html"||w.kind==="source"||w.kind==="markdown");
if(w.editB){ w.editB.style.display=(w.mode==="edit")?"none":""; w.editB.disabled=!editable; }
if(w.saveB) w.saveB.style.display=(w.mode==="edit")?"":"none";
if(w.revertB) w.revertB.style.display=(w.mode==="edit")?"":"none";
w.docEl.classList.toggle("editing", w.mode==="edit");
}
function enterEdit(w){
if(!(w.kind==="html"||w.kind==="source"||w.kind==="markdown"))return;
if(w.kind==="source"||w.kind==="markdown"){ // edit the raw text/markdown source
var pre=document.createElement("pre");
pre.style.cssText="font-family:NeXTMono,Courier,monospace;font-size:12px;white-space:pre-wrap;word-break:break-word;margin:0;";
pre.textContent=w.raw||""; w.docEl.innerHTML=""; w.docEl.appendChild(pre);
}
w.mode="edit"; w.docEl.contentEditable="true"; setEditUI(w); w.docEl.focus();
}
function revertEdit(w){ w.mode="view"; w.docEl.contentEditable="false"; load(w, w.url); }
function flashTitle(w, msg){ var t=w.titleEl.textContent; w.titleEl.textContent=msg; setTimeout(function(){ if(w.titleEl.textContent===msg) w.titleEl.textContent=t; },1900); }
function saveDoc(w){
if(w.mode!=="edit") return;
var body, ct;
if(w.kind==="html"){
var src; try{ src=new DOMParser().parseFromString(w.raw||"<!doctype html><html><head></head><body></body></html>","text/html"); }catch(e){ src=null; }
if(!src||!src.body) src=document.implementation.createHTMLDocument("");
src.body.innerHTML=w.docEl.innerHTML; // swap edited body into the original document (keeps <head>/<title>)
if(!src.querySelector("title")){ var ti=src.createElement("title"); ti.textContent=w.titleEl.textContent||shortTitle(w.url); (src.head||src.documentElement).appendChild(ti); }
body="<!doctype html>\n"+src.documentElement.outerHTML; ct="text/html";
} else {
body=w.docEl.innerText; ct=w.sourceCT||"text/plain";
}
var btn=w.saveB, old=btn.textContent; btn.disabled=true; btn.textContent="Saving…";
authFetch(w.url,{ method:"PUT", headers:{"Content-Type":ct}, body:body }).then(function(r){
btn.disabled=false; btn.textContent=old;
if(!r.ok){ flashTitle(w,(r.status===403||r.status===401)?"No write access":("Save failed "+r.status)); return; }
w.mode="view"; w.docEl.contentEditable="false"; w.raw=body;
if(w.kind==="html") renderHTML(w, body, w.url); else if(w.kind==="markdown") renderMarkdown(w, body, w.url); else renderSource(w, w.url, body, ct);
flashTitle(w,"Saved ✓");
}).catch(function(){ btn.disabled=false; btn.textContent=old; flashTitle(w,"Save failed (network)"); });
}
// ---- mark -> link (the signature WorldWideWeb move: author hypertext by hand) ----
function setMarked(url){
MARKED={ url:url, name:shortTitle(url) };
var el=document.getElementById("marked");
el.style.display=""; el.innerHTML='<a id="m-unmark" title="clear">✕</a><b>Marked target</b>'+esc(MARKED.name);
document.getElementById("m-unmark").onclick=function(){ MARKED=null; el.style.display="none"; };
}
function markDocument(w){ if(!w||!w.url||w.url==="#about")return; setMarked(w.url); flashTitle(w,"Marked ✓"); }
function linkToMarked(){
var w=FOCUSED||WINS[WINS.length-1]; if(!w)return;
if(!MARKED){ flashTitle(w,"Mark a document first (⌘M)"); return; }
if(w.mode!=="edit"){ flashTitle(w,"Edit this document first (⌘E)"); return; }
w.docEl.focus();
var sel=window.getSelection();
if(w.savedRange){ sel.removeAllRanges(); sel.addRange(w.savedRange); }
if(!sel.rangeCount || sel.isCollapsed || !w.docEl.contains(sel.anchorNode)){ flashTitle(w,"Select some text first"); return; }
document.execCommand("createLink", false, MARKED.url); // wraps the selection in <a href=marked>
w.savedRange=null; flashTitle(w,"Linked → "+MARKED.name);
}
// ---- menu ----
document.getElementById("m-open").onclick=function(){ var w=WINS[WINS.length-1]||makeWindow(); focusWin(w); w.urlEl.focus(); w.urlEl.select(); };
document.getElementById("m-mark").onclick=function(){ markDocument(FOCUSED||WINS[WINS.length-1]); };
document.getElementById("m-link").onclick=linkToMarked;
document.getElementById("m-new").onclick=function(){ var w=makeWindow(); navigate(w, homeUrl()); };
document.getElementById("m-home").onclick=function(){ var w=makeWindow(); navigate(w, homeUrl()); };
document.getElementById("m-help").onclick=function(){ var w=makeWindow(); renderAbout(w); };
document.getElementById("m-edit").onclick=function(){ var w=FOCUSED||WINS[WINS.length-1]; if(!w)return; if(w.mode==="edit")saveDoc(w); else enterEdit(w); };
document.addEventListener("keydown",function(e){
if(!(e.metaKey||e.ctrlKey))return; var k=e.key.toLowerCase();
if(k==="n"){ e.preventDefault(); var w=makeWindow(); navigate(w, homeUrl()); }
else if(k==="o"){ e.preventDefault(); var w2=WINS[WINS.length-1]||makeWindow(); focusWin(w2); w2.urlEl.focus(); }
else if(k==="e"){ var we=FOCUSED||WINS[WINS.length-1]; if(we&&(we.kind==="html"||we.kind==="source")){ e.preventDefault(); we.mode==="edit"?saveDoc(we):enterEdit(we); } }
else if(k==="s"){ var ws=FOCUSED||WINS[WINS.length-1]; if(ws&&ws.mode==="edit"){ e.preventDefault(); saveDoc(ws); } }
else if(k==="m"){ e.preventDefault(); markDocument(FOCUSED||WINS[WINS.length-1]); }
else if(k==="l"){ e.preventDefault(); linkToMarked(); }
});
// ---- boot: open the home / about window ----
function boot(){ var w=makeWindow({x:200,y:60}); if(me()) navigate(w, homeUrl()); else renderAbout(w); }
document.addEventListener("xlogin",function(){ /* keep current windows; user can open pod */ });
boot();
</script>
</body>
</html>