Skip to content

Commit a216b56

Browse files
committed
full update for the computer networkign website !
1 parent c354684 commit a216b56

2 files changed

Lines changed: 696 additions & 174 deletions

File tree

cn_webviewer/app.js

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,43 @@ var searchIndex = [];
124124
});
125125
})();
126126

127+
// ── BASE PATH DETECTION ──
128+
// Locally: index.html is in cn_webviewer/ → assets are at ../
129+
// GitHub Pages: cn_webviewer/ is deployed as site root → assets are at ./
130+
// We probe once with a HEAD request; all fetches go through resolveAsset().
131+
var _base = null;
132+
var _baseQueue = [];
133+
134+
function detectBase(cb) {
135+
if (_base !== null) { cb(); return; }
136+
_baseQueue.push(cb);
137+
if (_baseQueue.length > 1) return;
138+
fetch('readme.md', { method: 'HEAD' })
139+
.then(function(r) { _base = r.ok ? '' : '../'; flush(); })
140+
.catch(function() { _base = '../'; flush(); });
141+
function flush() {
142+
_baseQueue.forEach(function(fn) { fn(); });
143+
_baseQueue = [];
144+
}
145+
}
146+
147+
// Strips any leading '../' from a stored path, then prepends resolved base.
148+
function resolveAsset(path) {
149+
return _base + path.replace(/^\.\.\//, '');
150+
}
151+
127152
// ── CACHE ──
128153
var readmeRaw = null;
129154
var currentFile = null; // null = readme, else path string
130155

131156
function fetchReadme(cb) {
132157
if (readmeRaw) { cb(null, readmeRaw); return; }
133-
fetch('../readme.md')
134-
.then(function(r) { if (!r.ok) throw new Error(r.status); return r.text(); })
135-
.then(function(t) { readmeRaw = t; cb(null, t); })
136-
.catch(cb);
158+
detectBase(function() {
159+
fetch(resolveAsset('../readme.md'))
160+
.then(function(r) { if (!r.ok) throw new Error(r.status); return r.text(); })
161+
.then(function(t) { readmeRaw = t; cb(null, t); })
162+
.catch(cb);
163+
});
137164
}
138165

139166
// ── THEME ──
@@ -283,16 +310,19 @@ function loadReadme(cb) {
283310
// ── LOAD DOC FILE ──
284311
function loadFile(path, label) {
285312
loading();
286-
fetch(path)
287-
.then(function(r) { if (!r.ok) throw new Error(r.status + ' ' + r.statusText); return r.text(); })
288-
.then(function(md) {
289-
currentFile = path;
290-
var html = marked.parse(md);
291-
html = fixLinks(html, path);
292-
doc().innerHTML = html;
293-
addHeadingIds();
294-
})
295-
.catch(function(err) { error('Cannot load ' + path + '<br>' + err.message); });
313+
detectBase(function() {
314+
var url = resolveAsset(path);
315+
fetch(url)
316+
.then(function(r) { if (!r.ok) throw new Error(r.status + ' ' + r.statusText); return r.text(); })
317+
.then(function(md) {
318+
currentFile = path;
319+
var html = marked.parse(md);
320+
html = fixLinks(html, path);
321+
doc().innerHTML = html;
322+
addHeadingIds();
323+
})
324+
.catch(function(err) { error('Cannot load ' + path + '<br>' + err.message); });
325+
});
296326
}
297327

298328
// ── FIX LINKS ──

0 commit comments

Comments
 (0)