1import type { Database } from "bun:sqlite";2import type { Context } from "hono";3import { openMirror } from "../mirror/store.ts";4import { publicRepo, type RepoSettings, setting } from "../server/db.ts";5import { type Html, html } from "./html.ts";67export type PageContext = { domain: string; kepr: Database; mirrorDir: string };89export type PublicRepo = { settings: RepoSettings; mirror: Database };1011const HEAD_CACHE = "public, max-age=60";1213const SAVE_CACHE = "public, max-age=31536000";14const FONTS =15 "https://fonts.bunny.net/css?family=bellota:700|comfortaa:700|martian-mono:400|quicksand:400,600|sacramento:400&display=swap";161718export function openPublicRepo(ctx: PageContext, name: string): PublicRepo | null {19 const settings = publicRepo(ctx.kepr, name);20 return settings ? { settings, mirror: openMirror(ctx.mirrorDir, name) } : null;21}2223export function cacheFor(c: Context, atSave: boolean): void {24 c.header("Cache-Control", atSave ? SAVE_CACHE : HEAD_CACHE);25}2627export function notFound(c: Context, ctx: PageContext): Response {28 c.header("Cache-Control", "no-store");29 const body = html`<h1 class="file-page-title">Not found</h1>30 <p class="muted">Nothing lives at this address.</p>`;31 return c.html(page(ctx, { title: "Not found", description: "", path: c.req.path, body }), 404);32}3334type PageOptions = { title: string; description: string; path: string; body: Html };3536export function page(ctx: PageContext, options: PageOptions): string {37 const site = setting(ctx.kepr, "site_name") ?? "kepr";38 const branding = setting(ctx.kepr, "branding");39 const title = `${options.title} — ${site}`;40 const url = `https://${ctx.domain}${options.path}`;41 return html`<!doctype html>42<html lang="en">43<head>44<meta charset="utf-8">45<meta name="viewport" content="width=device-width, initial-scale=1">46<title>${title}</title>47<meta name="description" content="${options.description}">48<meta property="og:site_name" content="${site}">49<meta property="og:type" content="website">50<meta property="og:url" content="${url}">51<meta property="og:title" content="${title}">52<meta property="og:description" content="${options.description}">53<meta name="twitter:card" content="summary">54<meta name="twitter:title" content="${title}">55<meta name="twitter:description" content="${options.description}">56<meta name="theme-color" content="#090706">57<link rel="canonical" href="${url}">58<link rel="icon" href="/favicon.svg" type="image/svg+xml">59<link rel="preconnect" href="https://fonts.bunny.net">60<link rel="stylesheet" href="${FONTS}">61<link rel="stylesheet" href="/static/css/kepr.css">62<script src="/static/js/kepr.js" defer></script>63</head>64<body>65<nav>66<a class="wordmark" href="/" aria-label="${site}"><svg viewBox="0 0 32 32" class="wordmark-icon" aria-hidden="true"><g class="tri-outer"><polygon points="16,3 27,23 5,23"/></g><g class="tri-inner"><polygon points="16,10 22,21 10,21"/></g></svg></a>67${branding && html`<span class="wordmark-by">${branding}</span>`}68<span class="nav-links"><a href="/browse">browse</a><a href="/docs">docs</a><a href="/support">support</a></span>69</nav>70<main>71${options.body}72</main>73<footer><div class="credit">${site}</div></footer>74</body>75</html>`.value;76}7778const UNITS: [number, string][] = [79 [365 * 86400, "year"],80 [30 * 86400, "month"],81 [7 * 86400, "week"],82 [86400, "day"],83 [3600, "hour"],84 [60, "minute"],85];8687export function relativeTime(unixSeconds: number, now = Date.now() / 1000): string {88 const elapsed = now - unixSeconds;89 for (const [seconds, unit] of UNITS) {90 const n = Math.floor(elapsed / seconds);91 if (n >= 1) return `${n} ${unit}${n === 1 ? "" : "s"} ago`;92 }93 return "just now";94}9596export function formatDate(unixSeconds: number): string {97 return new Date(unixSeconds * 1000).toISOString().slice(0, 16).replace("T", " ");98}99100export function plural(n: number, noun: string): string {101 return `${n.toLocaleString("en-US")} ${noun}${n === 1 ? "" : "s"}`;102}103104export function formatSize(bytes: number): string {105 if (bytes < 1024) return `${bytes} B`;106 if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;107 return `${(bytes / 1024 / 1024).toFixed(1)} MB`;108}109110111export function pathUrl(path: string): string {112 return path.split("/").map(encodeURIComponent).join("/");113}