paths.ts
This file
Path
apps/web/src/features/code/paths.ts
115 lines · 3.1 KiB · TypeScript
Last changed by
Merge web/code-tab-and-fixes into main, keeping both the hero's concurrency line and its second actionoscabrielbefore adoption2eaa076
History of this fileTake it
Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 1–2why, one step back1import type { ProjectName } from "@repo/protocol/ids";2Merge web/code-tab-and-fixes into main, keeping both the hero's concurrency line and its second actionoscabrielbefore adoption2eaa076
line 3why, one step back3import type { LogShow } from "./log-page.ts";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 4–25why, one step back4import { revSegment } from "./rev.ts";56export const pathSegments = (path: string): string =>7 path.split("/").map(encodeURIComponent).join("/");89const under = (prefix: string, path: string | undefined) =>10 path === undefined || path === ""11 ? prefix12 : `${prefix}/${pathSegments(path)}`;1314export const treeHref = (project: ProjectName, rev: string, path?: string) =>15 under(`/${project}/tree/${revSegment(rev)}`, path);1617export const blobHref = (project: ProjectName, rev: string, path: string) =>18 under(`/${project}/blob/${revSegment(rev)}`, path);1920export const rawHref = (project: ProjectName, rev: string, path: string) =>21 under(`/${project}/raw/${revSegment(rev)}`, path);2223export const whyHref = (project: ProjectName, rev: string, path: string) =>24 under(`/${project}/why/${revSegment(rev)}`, path);25Merge fix/w12-reviewoscabrielbefore adoption7b8b393
line 26why, one step back26/**Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 27–28why, one step back27 * An `AppLink` takes the bare log as its `to` and the query as its `search`, or28 * the router reads the query as part of the revision.Merge fix/w12-reviewoscabrielbefore adoption7b8b393
line 29why, one step back29 */Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 30–32why, one step back30export const commitsHref = (31 project: ProjectName,32 rev: string,Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 33–39why, one step back33 {34 from,35 path,36 show,37 }: {38 readonly from?: number;39 readonly path?: string;Merge web/code-tab-and-fixes into main, keeping both the hero's concurrency line and its second actionoscabrielbefore adoption2eaa076
line 40why, one step back40 readonly show?: LogShow;Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 41why, one step back41 } = {}Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 42–50why, one step back42) => {43 const query = new URLSearchParams();4445 if (path !== undefined) {46 query.set("path", path);47 }4849 if (from !== undefined && from > 0) {50 query.set("from", String(from));Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 51–54why, one step back51 }5253 if (show !== undefined) {54 query.set("show", show);Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 55–61why, one step back55 }5657 const search = query.toString();5859 return `/${project}/commits/${revSegment(rev)}${search === "" ? "" : `?${search}`}`;60};61Merge redesign into mainoscabrielbefore adoptioncaedd21
line 62why, one step back62/** A commit's page: its id, or the id inside a fork (`@<handle>/<ask>:<sha>`), as one segment. */Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 63why, one step back63export const commitHref = (project: ProjectName, sha: string) =>Merge redesign into mainoscabrielbefore adoptioncaedd21
line 64why, one step back64 `/${project}/commit/${revSegment(sha)}`;Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 65–102why, one step back6566export const shortSha = (sha: string) => sha.slice(0, 7);6768export const dirOf = (path: string) => {69 const slash = path.lastIndexOf("/");7071 return slash === -1 ? "" : path.slice(0, slash);72};7374export const ancestorsOf = (path: string): readonly string[] =>75 path76 .split("/")77 .slice(0, -1)78 .map((_, index, parts) => parts.slice(0, index + 1).join("/"));7980export const sizeInWords = (bytes: number) => {81 const units = ["KiB", "MiB", "GiB"] as const;8283 if (bytes < 1024) {84 return `${bytes} ${bytes === 1 ? "byte" : "bytes"}`;85 }8687 const step = Math.min(88 units.length,89 Math.floor(Math.log(bytes) / Math.log(1024))90 );9192 return `${(bytes / 1024 ** step).toFixed(1)} ${units[step - 1] ?? "GiB"}`;93};9495const IMAGE = /\.(?:png|jpe?g|gif|webp|avif|ico|svg)$/iu;9697const MARKDOWN = /\.(?:md|markdown|mdown)$/iu;9899export const isImage = (path: string) => IMAGE.test(path);100101export const isMarkdown = (path: string) => MARKDOWN.test(path);102Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
lines 103–114why, one step back103export const showsCode = (104 file: {105 readonly binary: boolean;106 readonly path: string;107 readonly text?: string | undefined;108 },109 plain: boolean110) =>111 !file.binary &&112 file.text !== undefined &&113 (plain || !(isImage(file.path) || isMarkdown(file.path)));114Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 115why, one step back115export const diffAnchor = (path: string) => `diff-${encodeURIComponent(path)}`;