import { Handlers, PageProps } from "$fresh/server.ts"; import { asset, Head, Partial } from "$fresh/runtime.ts"; import { SidebarCategory } from "../../components/DocsSidebar.tsx"; import Footer from "../../components/Footer.tsx"; import Header from "../../components/Header.tsx"; import { CATEGORIES, getFirstPageUrl, LATEST_VERSION, TABLE_OF_CONTENTS, TableOfContentsEntry, } from "../../data/docs.ts"; import { frontMatter, renderMarkdown } from "../../utils/markdown.ts"; import toc from "../../../docs/toc.ts"; import { TableOfContents } from "../../islands/TableOfContents.tsx"; import SearchButton from "$fresh/www/islands/SearchButton.tsx"; import VersionSelect from "$fresh/www/islands/VersionSelect.tsx"; interface Data { page: Page; } interface NavEntry { title: string; category?: string; href: string; } export interface VersionLink { label: string; href: string; value: string; } interface Page extends TableOfContentsEntry { markdown: string; data: Record; versionLinks: VersionLink[]; version: string; prevNav?: NavEntry; nextNav?: NavEntry; } const pattern = new URLPattern({ pathname: "/:version/:page*" }); export const handler: Handlers = { async GET(_req, ctx) { const slug = ctx.params.slug; // Check if the slug is the index page of a version tag if (TABLE_OF_CONTENTS[slug]) { const href = getFirstPageUrl(slug); return new Response("", { status: 307, headers: { location: href }, }); } const match = pattern.exec("https://localhost/" + slug); if (!match) { return ctx.renderNotFound(); } let { version, page = "" } = match.pathname.groups; if (!version) { return ctx.renderNotFound(); } // Latest version doesn't show up in the url if (!TABLE_OF_CONTENTS[version]) { page = version + (page ? "/" + page : ""); version = LATEST_VERSION; } // Check if the page exists const currentToc = TABLE_OF_CONTENTS[version]; const entry = currentToc[page]; if (!entry) { return ctx.renderNotFound(); } // Build up the link map for the version selector. const versionLinks: VersionLink[] = []; for (const version in TABLE_OF_CONTENTS) { const label = toc[version].label; const maybeEntry = TABLE_OF_CONTENTS[version][page]; // Check if the same page is available for this version and // link to that. Pick the index page for that version if an // exact match doesn't exist. versionLinks.push({ label, value: version, href: maybeEntry ? maybeEntry.href : getFirstPageUrl(version), }); } // Add previous and next page entry if available const entryKeys = Object.keys(currentToc); const idx = entryKeys.findIndex((name) => name === entry.slug); let nextNav: NavEntry | undefined; let prevNav: NavEntry | undefined; const prevEntry = currentToc[entryKeys[idx - 1]]; const nextEntry = currentToc[entryKeys[idx + 1]]; if (prevEntry) { let category = prevEntry.category; category = category ? currentToc[category].title : ""; prevNav = { title: prevEntry.title, category, href: prevEntry.href }; } if (nextEntry) { let category = nextEntry.category; category = category ? currentToc[category].title : ""; nextNav = { title: nextEntry.title, category, href: nextEntry.href }; } // Parse markdown front matter const url = new URL(`../../../${entry.file}`, import.meta.url); const fileContent = await Deno.readTextFile(url); const { body, attrs } = frontMatter>(fileContent); return ctx.render({ page: { ...entry, markdown: body, data: attrs ?? {}, versionLinks, version, prevNav, nextNav, }, }); }, }; export default function DocsPage(props: PageProps) { const { page } = props.data; const ogImageUrl = new URL(asset("/og-image.webp"), props.url).href; const title = `${page?.title ?? "Not Found"} | Fresh docs`; let description = "Fresh Document"; if (page.data.description) { description = String(page.data.description); } const { html, headings } = renderMarkdown(page.markdown); return ( <> {title}
); } function MobileSidebar({ page }: { page: Page }) { return (
); } function ForwardBackButtons(props: { slug: string; version: string; prev?: NavEntry; next?: NavEntry; }) { const { prev, next } = props; return (
{prev ? ( Previous page {prev.title} ) :
} {next ? ( Next page {next.title} ) :
}
); }