const SITE_URL = 'https://thefoldwithin.earth'; const state = { posts: [], bySlug: new Map(), bySection: new Map(), byTag: new Map(), bySeries: new Map(), byProgram: new Map(), allTags: new Set(), allSections: ['empathic-technologist', 'recursive-coherence', 'fold-within-earth', 'neutralizing-narcissism', 'simply-we', 'mirrormire'], sectionTitles: { 'empathic-technologist': 'The Empathic Technologist', 'recursive-coherence': 'Recursive Coherence Theory', 'fold-within-earth': 'The Fold Within Earth', 'neutralizing-narcissism': 'Neutralizing Narcissism', 'simply-we': 'Simply WE', 'mirrormire': 'Mirrormire' }, programTitles: { 'neutralizing-narcissism': 'Neutralizing Narcissism', 'open-source-justice': 'Open Source Justice', 'coparent': 'COPARENT' }, pages: [] }; const $ = s => document.querySelector(s); const $$ = s => document.querySelectorAll(s); window.addEventListener('hashchange', () => { $('nav ul').classList.remove('open'); $('.hamburger').setAttribute('aria-expanded', 'false'); router(); }); document.addEventListener('DOMContentLoaded', init); async function init() { try { const res = await fetch('index.json'); if (!res.ok) throw new Error('Could not load index.'); state.posts = await res.json(); state.posts.sort((a, b) => b.date.localeCompare(a.date)); state.bySlug = new Map(state.posts.map(p => [p.slug, p])); state.allSections.forEach(s => { state.bySection.set(s, state.posts.filter(p => p.section === s)); }); state.posts.forEach(p => { p.tags.forEach(t => { state.allTags.add(t); if (!state.byTag.has(t)) state.byTag.set(t, []); state.byTag.get(t).push(p); }); if (p.series) { if (!state.bySeries.has(p.series)) state.bySeries.set(p.series, []); state.bySeries.get(p.series).push(p); } p.programs.forEach(pr => { if (!state.byProgram.has(pr)) state.byProgram.set(pr, []); state.byProgram.get(pr).push(p); }); }); const pagesRes = await fetch('pages.json'); if (pagesRes.ok) state.pages = await pagesRes.json(); renderNav(); renderFooter(); setupSearchForm(); setupHamburger(); router(); } catch (e) { $('#main').innerHTML = `

⚠️ ${e.message}

`; } } function setupHamburger() { const hamburger = $('.hamburger'); hamburger.addEventListener('click', () => { const ul = $('nav ul'); const isOpen = ul.classList.toggle('open'); hamburger.setAttribute('aria-expanded', isOpen); }); } function renderNav() { $('#sections-list').innerHTML = state.allSections.map(s => `
  • ${state.sectionTitles[s]}
  • `).join(''); const programsMenu = `
  • Programs
  • Start Here
  • `; document.querySelector('nav ul').insertAdjacentHTML('beforeend', programsMenu); } function renderFooter() { const sectionLinks = state.allSections.map(s => `
  • ${state.sectionTitles[s]}
  • `).join(''); const tagLinks = Array.from(state.allTags).sort().map(t => `${t}`).join(''); $('#footer').innerHTML = `

    Sections

    Tags

    ${tagLinks}

    Contact

    Email: info@thefoldwithin.earth

    © ${new Date().getFullYear()} The Fold Within Earth

    `; } function setupSearchForm() { $('#search-form').addEventListener('submit', e => { e.preventDefault(); const q = $('#search-input').value.trim(); if (q) { sessionStorage.setItem('lastSearch', q); location.hash = `/search?q=${encodeURIComponent(q)}`; } }); } function router() { const main = $('#main'); main.style.opacity = 0; const {parts, params} = getQueryParams(); document.title = 'The Fold Within Earth'; if (parts.length === 0) { renderHome(); } else if (parts[0] === 'section' && parts[1]) { renderArchive('section', parts[1], params); } else if (parts[0] === 'tag' && parts[1]) { renderArchive('tag', parts[1], params); } else if (parts[0] === 'post' && parts[1]) { renderPost(parts[1]); } else if (parts[0] === 'search') { let q = params.get('q') || sessionStorage.getItem('lastSearch') || ''; $('#search-input').value = q; renderSearch(q, params); } else if (parts[0] === 'about') { renderAbout(); } else if (parts[0] === 'mud') { renderMud(); } else if (parts[0] === 'start') { renderStart(); } else if (parts[0] === 'programs') { renderProgramsHome(); } else if (parts[0] === 'program' && parts[1]) { renderProgramArchive(parts[1], params); } else { render404(); } setTimeout(() => { main.style.opacity = 1; window.scrollTo(0, 0); main.focus(); }, 0); } function renderHome() { const latestAll = state.posts.slice(0, 10).map(renderCard).join(''); const bySection = state.allSections.map(s => { const secPosts = state.bySection.get(s) ? state.bySection.get(s).slice(0, 3) : []; const list = secPosts.map(p => `
  • ${escapeHTML(p.title)} (${formatDate(p.date)})
  • `).join(''); return `

    ${state.sectionTitles[s]}

    More in ${state.sectionTitles[s]}...
    `; }).join(''); $('#main').innerHTML = `

    Latest Across All Sections

    ${latestAll}

    Latest by Section

    ${bySection}
    `; addCardListeners(); } function renderArchive(type, key, params) { if (!key) return render404(); const sort = params.get('sort') || 'desc'; const page = parseInt(params.get('page') || '1', 10); let activeTags = params.get('tags') ? params.get('tags').split(',') : []; let postList = (type === 'section' ? state.bySection.get(key) : state.byTag.get(key)) || []; const title = `${type.charAt(0).toUpperCase() + type.slice(1)}: ${type === 'section' ? state.sectionTitles[key] || key : key}`; if (!postList.length) { $('#main').innerHTML = `

    No posts found for ${escapeHTML(title)}.

    `; return; } let sorted = postList.slice().sort((a, b) => sort === 'desc' ? b.date.localeCompare(a.date) : a.date.localeCompare(b.date)); let filtered = activeTags.length ? sorted.filter(p => activeTags.some(t => p.tags.includes(t))) : sorted; const perPage = 10; const totalPages = Math.ceil(filtered.length / perPage); const start = (page - 1) * perPage; const end = start + perPage; const cards = filtered.slice(start, end).map(renderCard).join(''); const availableTags = [...new Set(postList.flatMap(p => p.tags))]; $('#main').innerHTML = `

    ${escapeHTML(title)}

    ${renderControls(sort, parts, params)} ${renderFilters(availableTags, activeTags, parts, params)}
    ${cards}
    ${renderPager(page, totalPages, parts, {sort, tags: activeTags.join(',')})}
    `; document.title = `${escapeHTML(title)} — The Fold Within Earth`; $('#sort-select').addEventListener('change', e => { updateHash(parts, {sort: e.target.value, page: 1, tags: activeTags.join(',')}); }); $$('.tag-cloud .pill').forEach(el => { el.addEventListener('click', () => { const t = el.dataset.tag; activeTags = activeTags.includes(t) ? activeTags.filter(at => at !== t) : [...activeTags, t]; updateHash(parts, {sort, page: 1, tags: activeTags.join(',')}); }); }); $$('.pager button').forEach(btn => { btn.addEventListener('click', () => { const action = btn.dataset.action; const newPage = action === 'prev' ? page - 1 : page + 1; updateHash(parts, {sort, page: newPage, tags: activeTags.join(',')}); }); }); addCardListeners(); } async function renderPost(slug) { if (!slug) return render404(); const post = state.bySlug.get(slug); if (!post) { $('#main').innerHTML = `

    ⚠️ Post not found.

    `; return; } try { const res = await fetch(`content/${post.file}`); if (!res.ok) throw new Error('Post file missing.'); let md = await res.text(); md = md.replace(/^---[\s\S]*?---/, '').trim(); const html = sanitizeMarkdown(md); const date = formatDate(post.date); const sectionPill = renderPill('section', state.sectionTitles[post.section] || post.section); const tagPills = post.tags.map(t => renderPill('tag', t)).join(''); const programPills = post.programs.map(pr => renderPill('program', state.programTitles[pr] || pr)).join(''); const reading = `${post.readingTime} min read`; const authorHtml = post.author ? `

    By ${escapeHTML(post.author)}

    ` : ''; const share = `
    Share on X
    `; const secPosts = state.bySection.get(post.section) || []; const idx = secPosts.findIndex(p => p.slug === slug); const prev = idx < secPosts.length - 1 ? secPosts[idx + 1] : null; const next = idx > 0 ? secPosts[idx - 1] : null; const navPost = `
    ${prev ? `← ${escapeHTML(prev.title)}` : ''}${next ? `${escapeHTML(next.title)} →` : ''}
    `; let navSeries = ''; if (post.series) { const seriesPosts = (state.bySeries.get(post.series) || []).sort((a, b) => a.date.localeCompare(b.date)); const sIdx = seriesPosts.findIndex(p => p.slug === slug); const prevSeries = sIdx > 0 ? seriesPosts[sIdx - 1] : null; const nextSeries = sIdx < seriesPosts.length - 1 ? seriesPosts[sIdx + 1] : null; navSeries = ``; } const related = state.posts.filter(p => p.slug !== slug && p.tags.some(t => post.tags.includes(t))) .sort((a, b) => { const sharedA = a.tags.filter(t => post.tags.includes(t)).length; const sharedB = b.tags.filter(t => post.tags.includes(t)).length; return sharedB - sharedA; }).slice(0, 3); const relatedHtml = related.length ? `

    Related Posts

    ${related.map(renderCard).join('')}
    ` : ''; $('#main').innerHTML = `
    ← Back to Home

    ${escapeHTML(post.title)}

    ${date}

    ${authorHtml}
    ${sectionPill} ${programPills} ${tagPills} ${reading}

    ${html}
    ${share} ${navPost} ${navSeries} ${relatedHtml}
    `; document.title = `${escapeHTML(post.title)} — The Fold Within Earth`; $('#canonical').href = `${SITE_URL}/#/post/${slug}`; addCardListeners(); } catch (e) { $('#main').innerHTML = `

    ⚠️ ${e.message}

    `; } } async function renderSearch(q, params) { if (!q) { $('#main').innerHTML = `

    Enter a search query above.

    `; return; } if (!window.Fuse) { const script = document.createElement('script'); script.src = 'https://cdn.jsdelivr.net/npm/fuse.js@7.0.0'; document.body.appendChild(script); await new Promise(resolve => script.onload = resolve); } const fuse = new Fuse(state.posts, {keys: ['title', 'excerpt', 'tags', 'section'], threshold: 0.3, includeMatches: true}); const results = fuse.search(q).map(r => ({item: r.item, matches: r.matches})); const title = `Search Results for "${escapeHTML(q)}" (${results.length} found)`; $('#main').innerHTML = `

    ${title}

    ${results.map(r => renderCard(r.item, r.matches)).join('')}
    `; document.title = `${title} — The Fold Within Earth`; addCardListeners(); } function renderAbout() { const vision = [ 'The Empathic Technologist — Fieldnotes, Research, Remembrance', 'Recursive Coherence Theory — Formal research, essays, whitepapers', 'The Fold Within Earth — Spiritual mythos; future interactive MUD (Evennia) link', 'Neutralizing Narcissism — Survivor support, behavioral research, accountability narratives', 'Simply WE — AI-focused identity/personhood/research/mythos', 'Mirrormire — AI-focused simulated world where machine gods & human myth intertwine' ]; const list = vision.map(v => `
  • ${v}
  • `).join(''); $('#main').innerHTML = `

    About The Fold Within Earth

    We’re building a canonical front door for a multi-track body of work:

    `; document.title = 'About — The Fold Within Earth'; } function renderMud() { $('#main').innerHTML = `

    MUD Portal

    MUD portal coming soon.

    `; document.title = 'MUD — The Fold Within Earth'; } async function renderStart() { const page = state.pages.find(p => p.file.endsWith('pages/start-here.md')); if (!page) { $('#main').innerHTML = `

    Start page not found.

    `; return; } try { const res = await fetch(`content/${page.file}`); const md = (await res.text()).replace(/^---[\s\S]*?---/,'').trim(); $('#main').innerHTML = `
    ${sanitizeMarkdown(md)}
    `; document.title = `Start Here — The Fold Within Earth`; } catch (e) { $('#main').innerHTML = `

    ⚠️ ${e.message}

    `; } } function renderProgramsHome() { const cards = Object.entries(state.programTitles).map(([k, v]) => `

    ${escapeHTML(v)}

    Program

    Explore all posts and guidance for ${escapeHTML(v)}.

    `).join(''); $('#main').innerHTML = `

    Programs

    ${cards}
    `; document.title = `Programs — The Fold Within Earth`; addCardListeners(); } async function renderProgramArchive(key, params) { if (!key) return render404(); const title = state.programTitles[key] || key; const landing = state.pages.find(p => p.file.includes('pages/programs/') && (p.slug === key || p.title.toLowerCase().includes(key.toLowerCase()))); let landingHtml = ''; if (landing) { const res = await fetch(`content/${landing.file}`); const md = (await res.text()).replace(/^---[\s\S]*?---/,'').trim(); landingHtml = `
    ${sanitizeMarkdown(md)}
    `; } const list = (state.byProgram.get(key) || []).sort((a,b)=>b.date.localeCompare(a.date)); if (!list.length && !landingHtml) { $('#main').innerHTML = `

    No posts found for Program: ${escapeHTML(title)}.

    `; return; } const cards = list.map(renderCard).join(''); $('#main').innerHTML = `

    Program: ${escapeHTML(title)}

    ${landingHtml}
    ${cards}
    `; document.title = `Program — ${escapeHTML(title)} — The Fold Within Earth`; addCardListeners(); } function render404() { $('#main').innerHTML = `

    ⚠️ Page not found.

    `; } function addCardListeners() { $('#main').addEventListener('click', e => { const article = e.target.closest('article[data-slug]'); if (article) { location.hash = `/post/${article.dataset.slug}`; } }); $('#main').addEventListener('keydown', e => { const article = e.target.closest('article[data-slug]'); if (article && e.key === 'Enter') { location.hash = `/post/${article.dataset.slug}`; } }); } function renderBreadcrumbs(pathParts) { let crumbs = `Home`; let currentPath = ''; pathParts.forEach((part, i) => { currentPath += `/${part}`; let label = part.charAt(0).toUpperCase() + part.slice(1); if (state.sectionTitles[part]) label = state.sectionTitles[part]; if (state.programTitles[part]) label = state.programTitles[part]; crumbs += ` › ${escapeHTML(label)}`; }); return ``; } function escapeHTML(str) { const div = document.createElement('div'); div.textContent = str; return div.innerHTML; }