import { getAllWikiPaths, getWikiContent } from "../../../lib/wiki/api"; import WikiPage from "../../../interfaces/WikiPage"; import DashLayout from "../../../layouts/DashLayout"; import Link from "next/link"; import { m } from "framer-motion"; import PageBody from "../../../components/wiki/PageBody"; import { ReactElement, useEffect, useState } from "react"; import Image from "next/image"; interface WikiLandingPageProps { children: React.ReactNode; page: WikiPage; } interface TableOfContentsItem { id: string; type: string; text: string; } function WikiLandingPage(props: WikiLandingPageProps) { const [wikiContent, setWikiContent] = useState(<>); const [indexContent, setIndexContent] = useState([]); const [showMobileIndex, setShowMobileIndex] = useState(false); // needed for proper hydration due to replacing some elements useEffect(() => { setWikiContent({props.page.content}); }, [props.page]); useEffect(() => { const toc: TableOfContentsItem[] = []; const headings = document.querySelectorAll("h2, h3"); // store the heading text, id, and type, keep order headings.forEach((heading) => { const id = heading.getAttribute("id"); const type = heading.tagName.toLowerCase(); const text = heading.textContent; if (id && type && text) { toc.push({ id, type, text }); } }); setIndexContent(toc); }, [wikiContent]); const dirPath = props.page.path.split("/").map((path, index) => { // if home page, don't show if (path === "home") return <>; return (

{path}

); }); return (
{/* Desktop Header */}
{/* Title */}

toffee wiki

toffee logo
{/* Dir Path */}

home

{dirPath}
{/* Sidebar */}
Contents
{indexContent.map((item) => { return ( // increase indent based on heading level

{item.text}

); })}
{/* Dir Path Mobile */}

home

{dirPath}
{/* Mobile "Side"-bar */}
setShowMobileIndex(!showMobileIndex)} >
Contents
{indexContent.map((item) => { return ( // increase indent based on heading level

{item.text}

); })}
{/* Main content */}
{wikiContent}
); } type Params = { params: { slug: string[]; }; }; export async function getStaticProps({ params }: Params) { // only language if (params.slug.length < 2) { // if langauge is two letters, redirect to its home if (params.slug[0].length === 2 && params.slug[0].match(/^[a-z]+$/i)) { return { redirect: { destination: `/wiki/${params.slug[0]}/home`, }, }; } // else, 404 to prevemt building pointless pages (e.g. /wiki/this-is-not-a-language x 580258538 times) return { notFound: true, }; } // slug[0] = language // slug[1...n] = page path const lang = params.slug[0]; const path = params.slug.slice(1).join("/"); const pageData = getWikiContent(lang, path); // if no content, 404 if (!pageData) { return { notFound: true, }; } return { props: { page: { slug: params.slug, content: pageData.content, language: lang, path: path, data: pageData.data, }, }, }; } export async function getStaticPaths() { const paths = getAllWikiPaths(); return { paths: paths.map((path) => { return { params: { slug: path.split("/"), }, }; }), fallback: "blocking", }; } WikiLandingPage.getLayout = function getLayout(page: React.ReactNode) { const metaTags = { title: "Wiki - toffee", description: "Wiki for toffee", }; return {page}; }; export default WikiLandingPage;