forked from unimatrix/web
6 lines
3.1 KiB
JavaScript
6 lines
3.1 KiB
JavaScript
let chapters=[];const content=document.querySelector('#chapter-content'),navigation=document.querySelector('#chapter-nav'),search=document.querySelector('#chapter-search');
|
|
function renderNav(){const q=search.value.trim().toLowerCase();let group='';navigation.replaceChildren();const results=chapters.filter(c=>(c.title+' '+c.category+' '+c.search).toLowerCase().includes(q));for(const chapter of results){if(chapter.category!==group){const title=document.createElement('p');title.className='chapter-group';title.textContent=chapter.category;navigation.append(title);group=chapter.category}const link=document.createElement('a');link.href='#'+chapter.id;link.textContent=chapter.title;if(location.hash.slice(1)===chapter.id)link.setAttribute('aria-current','page');navigation.append(link)}document.querySelector('#search-count').textContent=q?`${results.length} matching chapters`:`${chapters.length} chapters · 68 illustrated icons`;}
|
|
function showChapter(){const id=decodeURIComponent(location.hash.slice(1));const index=Math.max(0,chapters.findIndex(c=>c.id===id));const chapter=chapters[index];if(!chapter)return;content.replaceChildren();const label=document.createElement('p');label.className='eyebrow';label.textContent=chapter.category;const title=document.createElement('h2');title.textContent=chapter.title;content.append(label,title);if(chapter.art){const figure=document.createElement('figure');figure.className='chapter-art';const img=document.createElement('img');img.src=`assets/${chapter.art}.webp`;img.alt=`Thematic illustration for ${chapter.title}`;figure.append(img);const caption=document.createElement('figcaption');caption.textContent='Minecraft-inspired concept artwork. The text below describes the released plugin.';figure.append(caption);content.append(figure)}const body=document.createElement('div');body.className='chapter-body';body.innerHTML=chapter.body;content.append(body);const pager=document.createElement('div');pager.className='chapter-pager';for(const [offset,text] of [[-1,'← Previous chapter'],[1,'Next chapter →']]){const next=chapters[index+offset];if(next){const link=document.createElement('a');link.href='#'+next.id;link.textContent=text;link.title=next.title;pager.append(link)}}content.append(pager);renderNav();document.title=chapter.title+' — dunerealms.com Field Manual';}
|
|
fetch('manual-data.json',{cache:'no-store'}).then(r=>{if(!r.ok)throw Error();return r.json()}).then(data=>{chapters=data;renderNav();showChapter()}).catch(()=>{content.textContent='The manual could not load. Please reload or use the PDF download.'});search.addEventListener('input',renderNav);window.addEventListener('hashchange',showChapter);
|
|
const toggle=document.querySelector('.menu-toggle'),nav=document.querySelector('header nav');toggle.addEventListener('click',()=>{const open=nav.classList.toggle('open');toggle.setAttribute('aria-expanded',String(open));toggle.setAttribute('aria-label',open?'Close navigation':'Open navigation')});
|
|
document.querySelectorAll('#realm-menu a').forEach(a=>{if(location.pathname.endsWith('/'+a.getAttribute('href')))a.setAttribute('aria-current','page')});
|

Staff