Skip to content
Myr

paths.ts

This file

Path

apps/web/src/features/code/paths.ts

140 lines · 4.4 KiB · TypeScript

Last changed by

Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2

History of this file

Take it

Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229

lines 1–32why, one step back
1/**
2 * The code browser's addresses, one function per page, so every link to a3 * folder, a file, its bytes, its history or a commit is spelled one way.4 * A revision is one URL segment (`revSegment`: a tag's `/` and `@` are5 * encoded); a repo path keeps its `/` and encodes each segment.6 */7import type { ProjectName } from "@repo/protocol/ids";89import { revSegment } from "./rev.ts";1011/** `path` in a URL: each segment encoded, the slashes kept. */12export const pathSegments = (path: string): string =>13 path.split("/").map(encodeURIComponent).join("/");1415/** `/<prefix>` followed by `/<path>` when there is one. */16const under = (prefix: string, path: string | undefined) =>17 path === undefined || path === ""18 ? prefix19 : `${prefix}/${pathSegments(path)}`;2021/** A folder at `rev`; the root when `path` is absent. */22export const treeHref = (project: ProjectName, rev: string, path?: string) =>23 under(`/${project}/tree/${revSegment(rev)}`, path);2425/** A file at `rev`. */26export const blobHref = (project: ProjectName, rev: string, path: string) =>27 under(`/${project}/blob/${revSegment(rev)}`, path);2829/** A file's bytes at `rev`, as the platform serves them. */30export const rawHref = (project: ProjectName, rev: string, path: string) =>31 under(`/${project}/raw/${revSegment(rev)}`, path);32

Close the code browser and drafting wave: its decisions in two ADRs, every rule it added held, and its leftovers folded inoscabrielbefore adoption009640c

line 33why, one step back
33/** Why each line of a file at `rev` is there (the why page). */

Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229

lines 34–36why, one step back
34export const whyHref = (project: ProjectName, rev: string, path: string) =>
35 under(`/${project}/why/${revSegment(rev)}`, path);36

Merge fix/w12-reviewoscabrielbefore adoption7b8b393

lines 37–41why, one step back
37/**
38 * The log from `rev`: only the commits that changed `path` when given, from39 * the `from`th. An `AppLink` takes the bare log as its `to` and the query40 * as its `search`, or the router reads the query as part of the revision.41 */

Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229

lines 42–44why, one step back
42export const commitsHref = (
43 project: ProjectName,44 rev: string,

Merge build/1211-code-browseroscabrielbefore adoptiona6808d3

lines 45–54why, one step back
45 {
46 from,47 path,48 show,49 }: {50 readonly from?: number;51 readonly path?: string;52 /** Only the answers or the asks' openings; every commit when absent. */53 readonly show?: "answers" | "openings";54 } = {}

Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229

lines 55–63why, one step back
55) => {
56 const query = new URLSearchParams();5758 if (path !== undefined) {59 query.set("path", path);60 }6162 if (from !== undefined && from > 0) {63 query.set("from", String(from));

Merge build/1211-code-browseroscabrielbefore adoptiona6808d3

lines 64–67why, one step back
64 }
6566 if (show !== undefined) {67 query.set("show", show);

Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229

lines 68–121why, one step back
68 }
6970 const search = query.toString();7172 return `/${project}/commits/${revSegment(rev)}${search === "" ? "" : `?${search}`}`;73};7475/** One commit. */76export const commitHref = (project: ProjectName, sha: string) =>77 `/${project}/commit/${sha}`;7879/** A commit's id as a person reads it. */80export const shortSha = (sha: string) => sha.slice(0, 7);8182/** The folder holding `path`: `""` for a file at the root. */83export const dirOf = (path: string) => {84 const slash = path.lastIndexOf("/");8586 return slash === -1 ? "" : path.slice(0, slash);87};8889/** Each folder above `path`, outermost first: `a/b/c` gives `a` and `a/b`. */90export const ancestorsOf = (path: string): readonly string[] =>91 path92 .split("/")93 .slice(0, -1)94 .map((_, index, parts) => parts.slice(0, index + 1).join("/"));9596/** A size as a person reads it: bytes, then KiB, MiB and GiB to one decimal. */97export const sizeInWords = (bytes: number) => {98 const units = ["KiB", "MiB", "GiB"] as const;99100 if (bytes < 1024) {101 return `${bytes} ${bytes === 1 ? "byte" : "bytes"}`;102 }103104 const step = Math.min(105 units.length,106 Math.floor(Math.log(bytes) / Math.log(1024))107 );108109 return `${(bytes / 1024 ** step).toFixed(1)} ${units[step - 1] ?? "GiB"}`;110};111112const IMAGE = /\.(?:png|jpe?g|gif|webp|avif|ico|svg)$/iu;113114const MARKDOWN = /\.(?:md|markdown|mdown)$/iu;115116/** Whether the platform serves `path` as an image a page may show inline. */117export const isImage = (path: string) => IMAGE.test(path);118119/** Whether `path` is Markdown, rendered on its page. */120export const isMarkdown = (path: string) => MARKDOWN.test(path);121

Merge perf/ssr-highlight into mainoscabrielbefore adoption5881ff2

lines 122–138why, one step back
122/**
123 * Whether a file's page shows it as code: text it has, and not an image124 * or Markdown shown as itself (`?plain=1` shows their source). The server125 * highlights a file ahead only then.126 */127export const showsCode = (128 file: {129 readonly binary: boolean;130 readonly path: string;131 readonly text?: string | undefined;132 },133 plain: boolean134) =>135 !file.binary &&136 file.text !== undefined &&137 (plain || !(isImage(file.path) || isMarkdown(file.path)));138

Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229

lines 139–140why, one step back
139/** The id of a changed file's diff on its commit's page. */
140export const diffAnchor = (path: string) => `diff-${encodeURIComponent(path)}`;