repo.ts

5.0 KB · raw

1import type { Hono } from "hono";2import {3	fileAt,4	filesAt,5	headId,6	history,7	objectBytes,8	repoStats,9	saveByPrefix,10	timeline,11} from "../mirror/queries.ts";12import { setting } from "../server/db.ts";13import { atlasSection } from "./atlas.ts";14import { type Html, html } from "./html.ts";15import {16	cacheFor,17	notFound,18	openPublicRepo,19	type PageContext,20	type PublicRepo,21	page,22	plural,23	relativeTime,24} from "./layout.ts";25import { firstParagraph, renderMarkdown } from "./markdown.ts";26import { timelineSvg } from "./timeline.ts";2728const PAGE_SIZE = 50;29const HTTP_URL = /^https?:\/\//i;3031export function registerRepoPage(app: Hono, ctx: PageContext): void {32	app.get("/:repo", (c) => {33		const repo = openPublicRepo(ctx, c.req.param("repo"));34		if (!repo) return notFound(c, ctx);35		// `?at=<id>` shows the Atlas at that save (§21.5); the rest of the page stays at HEAD.36		const at = c.req.query("at");37		const atSave = at === undefined ? null : saveByPrefix(repo.mirror, at);38		if (at !== undefined && !atSave) return notFound(c, ctx);39		const pageNumber = Math.max(1, Number.parseInt(c.req.query("page") ?? "1", 10) || 1);40		const readme = readmeAtHead(repo);41		const name = repo.settings.name;42		const intro =43			readme === null ? null : firstParagraph(readme, { repo: name, dir: "", at: null });44		cacheFor(c, false);45		return c.html(46			page(ctx, {47				title: name,48				description: intro?.text ?? "",49				path: `/${name}`,50				body: html`${header(ctx, repo, intro?.html ?? null)}51${headId(repo.mirror) && sections(repo, pageNumber, atSave?.id ?? null)}52${readme && details(renderMarkdown(readme, { repo: name, dir: "", at: null }))}`,53			}),54		);55	});56}5758function readmeAtHead(repo: PublicRepo): string | null {59	const head = headId(repo.mirror);60	const path =61		head && filesAt(repo.mirror, head).find((f) => f.path.toLowerCase() === "readme.md")?.path;62	const file = head && path ? fileAt(repo.mirror, head, path) : null;63	const bytes = file && objectBytes(repo.mirror, file.hash);64	return bytes ? new TextDecoder().decode(bytes) : null;65}6667function header(ctx: PageContext, repo: PublicRepo, intro: Html | null): Html {68	const { name, homepage, installLines } = repo.settings;69	const stats = repoStats(repo.mirror);70	const handle = setting(ctx.kepr, "handle");71	const meta = [72		plural(stats.saves, "save"),73		stats.lines > 0 && plural(stats.lines, "line"),74		stats.updated !== null && `updated ${relativeTime(stats.updated)}`,75	].filter(Boolean);76	return html`<header class="repo-head">77<h1 class="repo-name">${name}</h1>78${handle && html`<p class="repo-contributor mono"><a href="/@${handle}">@${handle}</a></p>`}79${intro && html`<div class="repo-desc"><p>${intro}</p></div>`}80${HTTP_URL.test(homepage) && html`<p class="repo-homepage"><a href="${homepage}">${homepage}</a></p>`}81${stats.language && html`<div class="lang-badges"><span class="lang-badge">${stats.language}</span></div>`}82<p class="repo-meta">${meta.join(" · ")}</p>83<div class="repo-tabbar">84<nav class="repo-tabs" aria-label="Repository panels"><a class="repo-tab" href="/${name}/issues">issues</a><a class="repo-tab" href="/${name}/releases">builds</a><a class="repo-tab" href="/${name}/archives">archives</a></nav>85${installLines.length > 0 && html`<div class="install-lines">${installLines.map((line) => html`<div class="install-line"><code>${line}</code><button type="button" class="copy-btn" data-copy="${line}" hidden>copy</button></div>`)}</div>`}86</div>87</header>`;88}8990function sections(repo: PublicRepo, pageNumber: number, at: string | null): Html {91	const name = repo.settings.name;92	const atlas = atlasSection(repo, at ?? (headId(repo.mirror) as string), at);93	const saves = timeline(repo.mirror);94	const rows = history(repo.mirror, PAGE_SIZE, (pageNumber - 1) * PAGE_SIZE);95	const count = plural(saves.length, "save");96	const older = pageNumber * PAGE_SIZE < saves.length;97	return html`<section class="repo-section">98<details class="repo-section-toggle" open>99<summary class="section-head"><span class="section-label">timeline</span><span class="section-count">${count}</span></summary>100${timelineSvg(name, saves)}101</details>102</section>103${atlas}104<section class="repo-section" id="history">105<details class="repo-section-toggle" open>106<summary class="section-head"><span class="section-label">history</span><span class="section-count">${count}</span></summary>107${rows.map((save) => html`<a class="history-row" href="/${name}/saves/${save.id}"><span class="history-id">${save.id}</span><span class="history-msg">${save.message.split("\n")[0]}</span><span class="history-when">${relativeTime(save.created_at)}</span></a>`)}108${(pageNumber > 1 || older) && html`<p class="pager mono">${pageNumber > 1 && html`<a href="?page=${pageNumber - 1}#history">← newer</a>`}${older && html`<a href="?page=${pageNumber + 1}#history">older →</a>`}</p>`}109</details>110</section>`;111}112113function details(readme: Html): Html {114	return html`<section class="repo-details">115<details class="repo-details-toggle" open>116<summary class="repo-details-head">details</summary>117<div class="repo-details-body markdown">${readme}</div>118</details>119</section>`;120}