paths.ts
This file
Path
apps/web/src/features/code/paths.ts
114 lines · 3.1 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 1–24why, one step back1import type { ProjectName } from "@repo/protocol/ids";23import { revSegment } from "./rev.ts";45export const pathSegments = (path: string): string =>6 path.split("/").map(encodeURIComponent).join("/");78const under = (prefix: string, path: string | undefined) =>9 path === undefined || path === ""10 ? prefix11 : `${prefix}/${pathSegments(path)}`;1213export const treeHref = (project: ProjectName, rev: string, path?: string) =>14 under(`/${project}/tree/${revSegment(rev)}`, path);1516export const blobHref = (project: ProjectName, rev: string, path: string) =>17 under(`/${project}/blob/${revSegment(rev)}`, path);1819export const rawHref = (project: ProjectName, rev: string, path: string) =>20 under(`/${project}/raw/${revSegment(rev)}`, path);2122export const whyHref = (project: ProjectName, rev: string, path: string) =>23 under(`/${project}/why/${revSegment(rev)}`, path);24Merge fix/w12-reviewoscabrielbefore adoption7b8b393
line 25why, one step back25/**Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 26–27why, one step back26 * An `AppLink` takes the bare log as its `to` and the query as its `search`, or27 * the router reads the query as part of the revision.Merge fix/w12-reviewoscabrielbefore adoption7b8b393
line 28why, one step back28 */Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 29–31why, one step back29export const commitsHref = (30 project: ProjectName,31 rev: string,Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 32–40why, one step back32 {33 from,34 path,35 show,36 }: {37 readonly from?: number;38 readonly path?: string;39 readonly show?: "answers" | "openings";40 } = {}Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 41–49why, one step back41) => {42 const query = new URLSearchParams();4344 if (path !== undefined) {45 query.set("path", path);46 }4748 if (from !== undefined && from > 0) {49 query.set("from", String(from));Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 50–53why, one step back50 }5152 if (show !== undefined) {53 query.set("show", show);Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 54–60why, one step back54 }5556 const search = query.toString();5758 return `/${project}/commits/${revSegment(rev)}${search === "" ? "" : `?${search}`}`;59};60Merge redesign into mainoscabrielbefore adoptioncaedd21
line 61why, one step back61/** 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 62why, one step back62export const commitHref = (project: ProjectName, sha: string) =>Merge redesign into mainoscabrielbefore adoptioncaedd21
line 63why, one step back63 `/${project}/commit/${revSegment(sha)}`;Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 64–101why, one step back6465export const shortSha = (sha: string) => sha.slice(0, 7);6667export const dirOf = (path: string) => {68 const slash = path.lastIndexOf("/");6970 return slash === -1 ? "" : path.slice(0, slash);71};7273export const ancestorsOf = (path: string): readonly string[] =>74 path75 .split("/")76 .slice(0, -1)77 .map((_, index, parts) => parts.slice(0, index + 1).join("/"));7879export const sizeInWords = (bytes: number) => {80 const units = ["KiB", "MiB", "GiB"] as const;8182 if (bytes < 1024) {83 return `${bytes} ${bytes === 1 ? "byte" : "bytes"}`;84 }8586 const step = Math.min(87 units.length,88 Math.floor(Math.log(bytes) / Math.log(1024))89 );9091 return `${(bytes / 1024 ** step).toFixed(1)} ${units[step - 1] ?? "GiB"}`;92};9394const IMAGE = /\.(?:png|jpe?g|gif|webp|avif|ico|svg)$/iu;9596const MARKDOWN = /\.(?:md|markdown|mdown)$/iu;9798export const isImage = (path: string) => IMAGE.test(path);99100export const isMarkdown = (path: string) => MARKDOWN.test(path);101Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2
lines 102–113why, one step back102export const showsCode = (103 file: {104 readonly binary: boolean;105 readonly path: string;106 readonly text?: string | undefined;107 },108 plain: boolean109) =>110 !file.binary &&111 file.text !== undefined &&112 (plain || !(isImage(file.path) || isMarkdown(file.path)));113Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 114why, one step back114export const diffAnchor = (path: string) => `diff-${encodeURIComponent(path)}`;