repo.ts

6.7 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	type Save,11	saveByPrefix,12	timeline,13} from "../mirror/queries.ts";14import { visibleReleases } from "../releases/releases.ts";15import { setting } from "../server/db.ts";16import { atlasSection } from "./atlas.ts";17import { type Html, html } from "./html.ts";18import {19	cacheFor,20	notFound,21	openPublicRepo,22	type PageContext,23	type PublicRepo,24	page,25	plural,26	relativeTime,27} from "./layout.ts";28import { renderMarkdown, renderParagraph } from "./markdown.ts";29import { timelineSvg } from "./timeline.ts";3031const PAGE_SIZE = 50;32// Each opens inline under the tabs (kepr.js), as Kepr 2's panels did; the page is the fallback.33const PANELS = [34	["issues", "issues"],35	["builds", "releases"],36	["archives", "archives"],37];38const HTTP_URL = /^https?:\/\//i;3940export function registerRepoPage(app: Hono, ctx: PageContext): void {41	app.get("/:repo", (c) => {42		const repo = openPublicRepo(ctx, c.req.param("repo"));43		if (!repo) return notFound(c, ctx);44		// `?at=<id>` shows the Atlas at that save (§21.5); the rest of the page stays at HEAD.45		const at = c.req.query("at");46		const atSave = at === undefined ? null : saveByPrefix(repo.mirror, at);47		if (at !== undefined && !atSave) return notFound(c, ctx);48		const pageNumber = Math.max(1, Number.parseInt(c.req.query("page") ?? "1", 10) || 1);49		const readme = readmeAtHead(repo);50		const name = repo.settings.name;51		const intro = readmeIntro(name, readme);52		cacheFor(c, false);53		return c.html(54			page(ctx, {55				title: name,56				description: intro?.text ?? "",57				path: `/${name}`,58				body: html`${header(ctx, repo, intro?.html ?? null)}59${headId(repo.mirror) && sections(repo, pageNumber, atSave?.id ?? null)}60${readme && details(renderMarkdown(readme, { repo: name, dir: "", at: null }))}`,61			}),62		);63	});64}6566/** The README's first paragraph (§21.4's description), as markup and as plain text. */67export function readmeIntro(68	repo: string,69	readme: string | null,70): { html: Html; text: string } | null {71	const paragraph = readme === null ? null : describeFile("README.md", readme);72	return paragraph === null ? null : renderParagraph(paragraph, { repo, dir: "", at: null });73}7475export function readmeAtHead(repo: PublicRepo): string | null {76	const head = headId(repo.mirror);77	const path =78		head && filesAt(repo.mirror, head).find((f) => f.path.toLowerCase() === "readme.md")?.path;79	const file = head && path ? fileAt(repo.mirror, head, path) : null;80	const bytes = file && objectBytes(repo.mirror, file.hash);81	return bytes ? new TextDecoder().decode(bytes) : null;82}8384function header(ctx: PageContext, repo: PublicRepo, intro: Html | null): Html {85	const { name, homepage, installLines } = repo.settings;86	const steal = `koh steal ${ctx.domain}/${name}`;87	const stats = repoStats(repo.mirror);88	const handle = setting(ctx.kepr, "handle");89	// §21.4: the version is the newest release; with none, the header shows no version.90	const newest = visibleReleases(ctx.kepr, repo.mirror, name)[0];91	const meta = [92		newest && `v${newest.version}`,93		plural(stats.saves, "save"),94		stats.lines > 0 && plural(stats.lines, "line"),95		stats.updated !== null && `updated ${relativeTime(stats.updated)}`,96	].filter(Boolean);97	return html`<header class="repo-head">98<h1 class="repo-name">${name}</h1>99${handle && html`<p class="repo-contributor mono"><a href="/@${handle}">@${handle}</a></p>`}100${intro && html`<div class="repo-desc"><p>${intro}</p></div>`}101${102	HTTP_URL.test(homepage) &&103	html`<p class="repo-homepage"><a href="${homepage}">${homepage}</a></p>`104}105${106	stats.language &&107	html`<div class="lang-badges"><span class="lang-badge">${stats.language}</span></div>`108}109<p class="repo-meta">${meta.join(" · ")}</p>110<div class="repo-tabbar">111<nav class="repo-tabs" aria-label="Repository panels">${PANELS.map(112		([label, path]) =>113			html`<a class="repo-tab" href="/${name}/${path}" data-panel="${path}">${label}</a>`,114	)}</nav>115<div class="install-lines">116<button type="button" class="repo-steal-btn" data-copy="${steal}">${steal}</button>117${installLines.map(118	(line) =>119		html`<div class="install-line"><code>${line}</code>${html`<button type="button" class="copy-btn" data-copy="${line}" hidden>copy</button>`}</div>`,120)}121</div>122</div>123<div class="repo-panel" hidden></div>124</header>`;125}126127function sections(repo: PublicRepo, requestedPage: number, at: string | null): Html {128	const name = repo.settings.name;129	const saves = timeline(repo.mirror);130	// Past the last page, even at an unbindable number like Infinity, is page 1.131	const pageNumber = (requestedPage - 1) * PAGE_SIZE < saves.length ? requestedPage : 1;132	const rows = history(repo.mirror, PAGE_SIZE, (pageNumber - 1) * PAGE_SIZE);133	const pages: Pages = {134		at,135		newer: pageNumber > 1 ? pageNumber - 1 : null,136		older: pageNumber * PAGE_SIZE < saves.length ? pageNumber + 1 : null,137	};138	const atlas = atlasSection(repo, at ?? (headId(repo.mirror) as string), at, saves);139	return html`<section class="repo-section">140<details class="repo-section-toggle" open>141<summary class="section-head"><span class="section-label">timeline</span></summary>142${timelineSvg(name, saves)}143</details>144</section>145${atlas}146<section class="repo-section" id="history">147<details class="repo-section-toggle" open>148<summary class="section-head"><span class="section-label">history</span></summary>149${saveList(name, rows, pages)}150</details>151</section>`;152}153154type Pages = { at: string | null; newer: number | null; older: number | null };155156/**157 * One page of saves in a box that scrolls four rows at a time, as Kepr 2's; kepr.js fetches the158 * older pages into it as it scrolls, and without JavaScript the pager links page the whole view.159 */160function saveList(repo: string, rows: Save[], pages: Pages): Html {161	const pageHref = (n: number) => {162		const query = new URLSearchParams(pages.at ? { at: pages.at } : {});163		query.set("page", String(n));164		return `?${query}#history`;165	};166	return html`<div class="save-list" data-list="history">167${rows.map(168	(save) =>169		html`<a class="history-row" href="/${repo}/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>`,170)}171${172	(pages.newer || pages.older) &&173	html`<p class="pager mono">${pages.newer && html`<a href="${pageHref(pages.newer)}">← newer</a>`}${pages.older && html`<a class="pager-older" href="${pageHref(pages.older)}">older →</a>`}</p>`174}175</div>`;176}177178function details(readme: Html): Html {179	return html`<section class="repo-details">180<details class="repo-details-toggle" open>181<summary class="repo-details-head">details</summary>182<div class="repo-details-body markdown">${readme}</div>183</details>184</section>`;185}