Files
Astral 4a32fab414
Deploy website to GitHub Pages / deploy (push) Failing after 3s
migrate website from KeruOS web to its own repo
2026-09-01 12:06:36 +02:00

41 lines
1.8 KiB
JavaScript

/* Keru website — inject shared nav + footer partials.
Usage:
<nav data-nav></nav>
<footer data-footer></footer>
<script src="assets/js/include.js" data-base="."></script>
data-base is the path prefix from this page to the web root.
All links inside the injected partials are resolved against that base. */
(function () {
var base = '.';
var scripts = document.querySelectorAll('script[src*="include.js"]');
for (var i = 0; i < scripts.length; i++) {
if (scripts[i].dataset.base) base = scripts[i].dataset.base;
}
var nav = document.querySelector('[data-nav]');
var footer = document.querySelector('[data-footer]');
function fill(el, url) {
if (!el) return;
fetch(base + '/' + url)
.then(function (r) { return r.text(); })
.then(function (t) {
// resolve links/asset refs in the partial against base
var div = document.createElement('div');
div.innerHTML = t;
['a', 'link', 'script', 'img'].forEach(function (sel) {
var nodes = div.querySelectorAll(sel + '[href], ' + sel + '[src]');
for (var j = 0; j < nodes.length; j++) {
var n = nodes[j];
var attr = n.hasAttribute('href') ? 'href' : 'src';
var val = n.getAttribute(attr);
if (val && val.indexOf('./') === 0) {
n.setAttribute(attr, base + '/' + val.replace(/^\.\//, ''));
}
}
});
el.innerHTML = div.innerHTML;
})
.catch(function () { /* offline / file:// — nav/footer stay empty */ });
}
fill(nav, '_includes/nav.html');
fill(footer, '_includes/footer.html');
})();