1import type { Hono } from "hono";2import { isSafePath } from "../bucket/repo.ts";3import { describeFile } from "../describe/describe.ts";4import { issueCounts } from "../issues/store.ts";5import {6 fileAt,7 filesAt,8 headId,9 history,10 objectBytes,11 repoStats,12 type Save,13 saveByPrefix,14 timeline,15} from "../mirror/queries.ts";16import { mirrorState } from "../mirror/store.ts";17import { visibleReleases } from "../releases/releases.ts";18import { setting } from "../server/db.ts";19import { atlasPanel, atlasPickerRows, atlasSection } from "./atlas.ts";20import { type Html, html } from "./html.ts";21import {22 browserPage,23 cacheFor,24 notFound,25 openBrowserRepo,26 type PageContext,27 type PublicRepo,28 plural,29 relativeTime,30} from "./layout.ts";31import { renderMarkdown, renderParagraph } from "./markdown.ts";32import { timelineSaves, timelineSvg } from "./timeline.ts";3334const PAGE_SIZE = 50;3536const PANELS = [37 ["issues", "issues"],38 ["builds", "releases"],39 ["archives", "archives"],40];41const HTTP_URL = /^https?:\/\//i;4243export function registerRepoPage(app: Hono, ctx: PageContext): void {44 app.get("/:repo", (c) => {45 const repo = openBrowserRepo(ctx, c, c.req.param("repo"));46 if (!repo) return notFound(c, ctx);47 48 const at = c.req.query("at");49 const atSave = at === undefined ? null : saveByPrefix(repo.mirror, at);50 if (at !== undefined && !atSave) return notFound(c, ctx);51 const pageNumber = Math.max(1, Number.parseInt(c.req.query("page") ?? "1", 10) || 1);52 const name = repo.settings.name;53 54 const readme =55 !repo.settings.description || !repo.settings.details ? readmeAtHead(repo.mirror) : null;56 const intro = repoIntro(repo, readme);57 cacheFor(c, false);58 return browserPage(c, ctx, {59 title: name,60 description: intro?.text ?? "",61 path: `/${name}`,62 body: html`${header(ctx, repo, intro?.html ?? null)}63${headId(repo.mirror) && sections(repo, pageNumber, atSave?.id ?? null)}64${details(repoDetails(repo, readme))}`,65 });66 });67 68 app.get("/:repo/timeline", (c) => {69 const repo = openBrowserRepo(ctx, c, c.req.param("repo"));70 if (!repo) return notFound(c, ctx);71 cacheFor(c, false);72 return c.json(timelineSaves(timeline(repo.mirror)));73 });74 75 app.get("/:repo/atlas/saves", (c) => {76 const repo = openBrowserRepo(ctx, c, c.req.param("repo"));77 const at = c.req.query("at");78 const save = repo && at !== undefined ? saveByPrefix(repo.mirror, at) : null;79 if (!repo || (at !== undefined && !save)) return notFound(c, ctx);80 cacheFor(c, false);81 const rows = atlasPickerRows(repo.settings.name, timeline(repo.mirror), save?.id ?? null);82 return c.html(rows.map(String).join(""));83 });84 85 app.get("/:repo/atlas/panel", (c) => {86 const repo = openBrowserRepo(ctx, c, c.req.param("repo"));87 const at = c.req.query("at");88 const path = c.req.query("path") ?? "";89 const save = repo && (at === undefined ? null : saveByPrefix(repo.mirror, at));90 const saveId = save?.id ?? (repo && headId(repo.mirror));91 const panel =92 repo &&93 saveId &&94 (at === undefined || save) &&95 isSafePath(path.replace(/\/$/, "")) &&96 atlasPanel(repo, saveId, save?.id ?? null, path);97 if (!panel) return notFound(c, ctx);98 cacheFor(c, save !== null && save !== undefined);99 return c.html(String(panel));100 });101}102103104105106107export function repoIntro(108 repo: PublicRepo,109 readme?: string | null,110): { html: Html; text: string } | null {111 const { name, description } = repo.settings;112 if (description)113 return renderParagraph(description, { repo: name, dir: "", at: null, own: true });114 const text = readme === undefined ? readmeAtHead(repo.mirror) : readme;115 const paragraph = text === null ? null : describeFile("README.md", text);116 return paragraph === null ? null : renderParagraph(paragraph, { repo: name, dir: "", at: null });117}118119120export function repoDetails(repo: PublicRepo, readme?: string | null): Html | null {121 const { name, details } = repo.settings;122 if (details) return renderMarkdown(details, { repo: name, dir: "", at: null, own: true });123 const text = readme === undefined ? readmeAtHead(repo.mirror) : readme;124 return text === null ? null : renderMarkdown(text, { repo: name, dir: "", at: null });125}126127export function readmeAtHead(mirror: PublicRepo["mirror"]): string | null {128 const head = headId(mirror);129 const path =130 head && filesAt(mirror, head).find((f) => f.path.toLowerCase() === "readme.md")?.path;131 const file = head && path ? fileAt(mirror, head, path) : null;132 const bytes = file && objectBytes(mirror, file.hash);133 return bytes ? new TextDecoder().decode(bytes) : null;134}135136function header(ctx: PageContext, repo: PublicRepo, intro: Html | null): Html {137 const { name, homepage, languages } = repo.settings;138 const steal = `koh steal ${ctx.domain}/${name}`;139 const stats = repoStats(repo.mirror);140 const handle = setting(ctx.kepr, "handle");141 const openIssues = issueCounts(ctx.kepr, name).open;142 143 144 const version =145 visibleReleases(ctx.kepr, repo.mirror, name)[0]?.version ||146 mirrorState(repo.mirror, "declared_version");147 const meta = [148 repo.private && "private",149 version && html`<span class="repo-version">v${version}</span>`,150 plural(stats.saves, "save"),151 stats.lines > 0 && plural(stats.lines, "line"),152 stats.updated !== null && `updated ${relativeTime(stats.updated)}`,153 ].filter(Boolean);154 return html`<header class="repo-head">155<h1 class="repo-name">${name}</h1>156${handle && html`<p class="repo-contributor mono"><a href="/@${handle}">@${handle}</a></p>`}157${html`<div class="repo-desc"${!intro && " hidden"}><p>${intro}</p></div>`}158${159 HTTP_URL.test(homepage) &&160 html`<p class="repo-homepage"><a href="${homepage}">${homepage}</a></p>`161}162${163 (languages.length > 0 || stats.language) &&164 html`<div class="lang-badges">${(languages.length > 0 ? languages : [stats.language]).map(165 (language) => html`<span class="lang-badge">${language}</span>`,166 )}</div>`167}168<p class="repo-meta">${meta.flatMap((part, i) => (i === 0 ? [part] : [" · ", part]))}</p>169${170 !repo.private &&171 html`<div class="repo-tabbar">172<nav class="repo-tabs" aria-label="Repository panels">${PANELS.map(173 ([label, path]) =>174 html`<a class="repo-tab" href="/${name}/${path}" data-panel="${path}">${label}${175 path === "issues" &&176 openIssues > 0 &&177 html`<span class="repo-issue-count" aria-label="${plural(178 openIssues,179 "open issue",180 )}">${openIssues}</span>`181 }</a>`,182 )}</nav>183<div class="install-lines">184<button type="button" class="repo-steal-btn" data-copy="${steal}">${steal}</button>185</div>186</div>187<div class="repo-panel" hidden></div>`188}189</header>`;190}191192function sections(repo: PublicRepo, requestedPage: number, at: string | null): Html {193 const name = repo.settings.name;194 const saves = timeline(repo.mirror);195 196 const pageNumber = (requestedPage - 1) * PAGE_SIZE < saves.length ? requestedPage : 1;197 const rows = history(repo.mirror, PAGE_SIZE, (pageNumber - 1) * PAGE_SIZE);198 const pages: Pages = {199 at,200 newer: pageNumber > 1 ? pageNumber - 1 : null,201 older: pageNumber * PAGE_SIZE < saves.length ? pageNumber + 1 : null,202 };203 const atlas = atlasSection(repo, at ?? (headId(repo.mirror) as string), at);204 return html`<section class="repo-section">205<details class="repo-section-toggle" open>206<summary class="section-head"><span class="section-label">timeline</span></summary>207${timelineSvg(name, saves)}208</details>209</section>210${atlas}211<section class="repo-section" id="history">212<details class="repo-section-toggle" open>213<summary class="section-head"><span class="section-label">history</span></summary>214${saveList(name, rows, pages)}215</details>216</section>`;217}218219type Pages = { at: string | null; newer: number | null; older: number | null };220221222223224225function saveList(repo: string, rows: Save[], pages: Pages): Html {226 const pageHref = (n: number) => {227 const query = new URLSearchParams(pages.at ? { at: pages.at } : {});228 query.set("page", String(n));229 return `?${query}#history`;230 };231 return html`<div class="save-list" data-list="history">232${rows.map(233 (save) =>234 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>`,235)}236${237 (pages.newer || pages.older) &&238 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>`239}240</div>`;241}242243244function details(body: Html | null): Html {245 return html`<section class="repo-details"${!body && " hidden"}>246<details class="repo-details-toggle" open>247<summary class="repo-details-head">details</summary>248<div class="repo-details-body markdown">${body}</div>249</details>250</section>`;251}