| 1 | import Link from "next/link"; |
| 2 | import { FileText } |
| 3 | |
| 4 | import { Markdown } |
| 5 | import { Skeleton } |
| 6 | import { cn } |
| 7 | import type { components |
| 8 | import { API_URL } |
| 9 | |
| 10 | type Readme |
| 11 | |
| 12 | export function RepositoryReadme({ |
| 13 | readme, |
| 14 | owner, |
| 15 | slug, |
| 16 | bare = false, |
| 17 | }: { |
| 18 | readme: Readme |
| 19 | owner: string |
| 20 | slug: string |
| 21 | /** Drops the card and its filename bar, for a profile where neither fits. */ |
| 22 | bare?: boolean |
| 23 | }) { |
| 24 | if (!readme.path |
| 25 | |
| 26 | const branch = |
| 27 | // a README in a subdirectory writes paths relative to that directory |
| 28 | const directory = |
| 29 | const rawHref = |
| 30 | `${API_URL}/api/repositories/${owner |
| 31 | |
| 32 | /** A README writes paths relative to itself, so a link out of it is either a file in this repository or nothing. Images go to the raw bytes; everything else goes to the page for that path. */ |
| 33 | const resolveUrl = |
| 34 | const [path, |
| 35 | if (!path) |
| 36 | return key === |
| 37 | ? rawHref(path) |
| 38 | : `/${owner} |
| 39 | .split("/") |
| 40 | .map(encodeURIComponent) |
| 41 | .join("/") |
| 42 | }; |
| 43 | |
| 44 | const body = |
| 45 | readme.content === |
| 46 | <p className="text-sm text-muted-foreground |
| 47 | This README is too large to render.{" "} |
| 48 | <Link href={ |
| 49 | View the raw file |
| 50 | </Link> |
| 51 | . |
| 52 | </p> |
| 53 | ) : ( |
| 54 | <Markdown resolveUrl={ |
| 55 | ); |
| 56 | |
| 57 | if (bare) return |
| 58 | |
| 59 | return ( |
| 60 | <div className="overflow-hidden rounded-lg border |
| 61 | <div className="flex items-center gap-2 border-b bg-muted/40 px-4 py-2.5 text-sm |
| 62 | <FileText className=" |
| 63 | <span className="font-medium |
| 64 | </div> |
| 65 | |
| 66 | <div className="px-4 py-4 md:px-6 |
| 67 | </div> |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** "#anchor" off the end of a path, so a link can carry both. */ |
| 72 | function splitHash(url: |
| 73 | const hash = |
| 74 | return hash === |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * A repository-relative path, resolved against the directory the README sits in: `./` drops, `../` pops, and a leading `/` is root-relative rather than a host path. |
| 79 | * |
| 80 | * `..` past the root is dropped rather than escaping the repository. |
| 81 | */ |
| 82 | export function resolve(url |
| 83 | // a root-relative link ignores where the README is |
| 84 | const base = |
| 85 | const segments: |
| 86 | |
| 87 | for (const segment |
| 88 | if (segment === |
| 89 | if (segment === |
| 90 | else segments.push( |
| 91 | } |
| 92 | |
| 93 | return segments.join |
| 94 | } |
| 95 | |
| 96 | /** biome-ignore-all lint/suspicious/noArrayIndexKey: skeleton rows have no id */ |
| 97 | export function RepositoryReadmeSkeleton({ |
| 98 | const lines = |
| 99 | <div className={cn |
| 100 | <Skeleton className=" |
| 101 | {Array.from({ |
| 102 | <Skeleton |
| 103 | key={index} |
| 104 | className="h-4" |
| 105 | style={{ width: |
| 106 | /> |
| 107 | ))} |
| 108 | </div> |
| 109 | ); |
| 110 | |
| 111 | if (bare) return |
| 112 | |
| 113 | return ( |
| 114 | <div className="overflow-hidden rounded-lg border |
| 115 | <div className="flex items-center gap-2 border-b bg-muted/40 px-4 py-2.5 |
| 116 | <Skeleton className=" |
| 117 | <Skeleton className=" |
| 118 | </div> |
| 119 | |
| 120 | {lines} |
| 121 | </div> |
| 122 | ); |
| 123 | } |