repo.ts

5.2 KB · raw

1import type { Hono } from "hono";2import { describeFile } from "../describe/describe.ts";3import {4	fileAt,5	filesAt,6	headId,7	history,8	objectBytes,9	repoStats,10	saveByPrefix,11	timeline,12} from "../mirror/queries.ts";13import { setting } from "../server/db.ts";14import { atlasSection } from "./atlas.ts";15import { type Html, html } from "./html.ts";16import {17	cacheFor,18	notFound,19	openPublicRepo,20	type PageContext,21	type PublicRepo,22	page,23	plural,24	relativeTime,25} from "./layout.ts";26import { renderMarkdown, renderParagraph } from "./markdown.ts";27import { timelineSvg } from "./timeline.ts";2829const PAGE_SIZE = 50;30const HTTP_URL = /^https?:\/\//i;3132export function registerRepoPage(app: Hono, ctx: PageContext): void {33	app.get("/:repo", (c) => {34		const repo = openPublicRepo(ctx, c.req.param("repo"));35		if (!repo) return notFound(c, ctx);36		// `?at=<id>` shows the Atlas at that save (§21.5); the rest of the page stays at HEAD.37		const at = c.req.query("at");38		const atSave = at === undefined ? null : saveByPrefix(repo.mirror, at);39		if (at !== undefined && !atSave) return notFound(c, ctx);40		const pageNumber = Math.max(1, Number.parseInt(c.req.query("page") ?? "1", 10) || 1);41		const readme = readmeAtHead(repo);42		const name = repo.settings.name;43		const paragraph = readme === null ? null : describeFile("README.md", readme);44		const intro =45			paragraph === null ? null : renderParagraph(paragraph, { repo: name, dir: "", at: null });46		cacheFor(c, false);47		return c.html(48			page(ctx, {49				title: name,50				description: intro?.text ?? "",51				path: `/${name}`,52				body: html`${header(ctx, repo, intro?.html ?? null)}53${headId(repo.mirror) && sections(repo, pageNumber, atSave?.id ?? null)}54${readme && details(renderMarkdown(readme, { repo: name, dir: "", at: null }))}`,55			}),56		);57	});58}5960function readmeAtHead(repo: PublicRepo): string | null {61	const head = headId(repo.mirror);62	const path =63		head && filesAt(repo.mirror, head).find((f) => f.path.toLowerCase() === "readme.md")?.path;64	const file = head && path ? fileAt(repo.mirror, head, path) : null;65	const bytes = file && objectBytes(repo.mirror, file.hash);66	return bytes ? new TextDecoder().decode(bytes) : null;67}6869function header(ctx: PageContext, repo: PublicRepo, intro: Html | null): Html {70	const { name, homepage, installLines } = repo.settings;71	const stats = repoStats(repo.mirror);72	const handle = setting(ctx.kepr, "handle");73	const meta = [74		plural(stats.saves, "save"),75		stats.lines > 0 && plural(stats.lines, "line"),76		stats.updated !== null && `updated ${relativeTime(stats.updated)}`,77	].filter(Boolean);78	return html`<header class="repo-head">79<h1 class="repo-name">${name}</h1>80${handle && html`<p class="repo-contributor mono"><a href="/@${handle}">@${handle}</a></p>`}81${intro && html`<div class="repo-desc"><p>${intro}</p></div>`}82${HTTP_URL.test(homepage) && html`<p class="repo-homepage"><a href="${homepage}">${homepage}</a></p>`}83${stats.language && html`<div class="lang-badges"><span class="lang-badge">${stats.language}</span></div>`}84<p class="repo-meta">${meta.join(" · ")}</p>85<div class="repo-tabbar">86<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>87${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>`}88</div>89</header>`;90}9192function sections(repo: PublicRepo, pageNumber: number, at: string | null): Html {93	const name = repo.settings.name;94	const atlas = atlasSection(repo, at ?? (headId(repo.mirror) as string), at);95	const saves = timeline(repo.mirror);96	const rows = history(repo.mirror, PAGE_SIZE, (pageNumber - 1) * PAGE_SIZE);97	const count = plural(saves.length, "save");98	const older = pageNumber * PAGE_SIZE < saves.length;99	return html`<section class="repo-section">100<details class="repo-section-toggle" open>101<summary class="section-head"><span class="section-label">timeline</span><span class="section-count">${count}</span></summary>102${timelineSvg(name, saves)}103</details>104</section>105${atlas}106<section class="repo-section" id="history">107<details class="repo-section-toggle" open>108<summary class="section-head"><span class="section-label">history</span><span class="section-count">${count}</span></summary>109${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>`)}110${(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>`}111</details>112</section>`;113}114115function details(readme: Html): Html {116	return html`<section class="repo-details">117<details class="repo-details-toggle" open>118<summary class="repo-details-head">details</summary>119<div class="repo-details-body markdown">${readme}</div>120</details>121</section>`;122}