| 1da9874 | | | 1 | import type { Metadata } from "next"; |
| 3e3af55 | | | 2 | import Link from "next/link"; |
| bf5fc33 | | | 3 | import { createHighlighter, type Highlighter } from "shiki"; |
| bc1b2ba | | | 4 | import { formatSize, encodePath } from "@/lib/utils"; |
| bc1b2ba | | | 5 | import { getRepoBlob } from "@/lib/grove-api"; |
| bf5fc33 | | | 6 | |
| bf5fc33 | | | 7 | let highlighter: Highlighter | null = null; |
| bf5fc33 | | | 8 | |
| bf5fc33 | | | 9 | async function getHighlighter() { |
| bf5fc33 | | | 10 | if (!highlighter) { |
| bf5fc33 | | | 11 | highlighter = await createHighlighter({ |
| bf5fc33 | | | 12 | themes: ["vitesse-light", "vitesse-dark"], |
| bf5fc33 | | | 13 | langs: [ |
| bf5fc33 | | | 14 | "typescript", "tsx", "javascript", "jsx", "json", "markdown", |
| bf5fc33 | | | 15 | "css", "html", "python", "rust", "go", "ruby", "yaml", "toml", |
| bf5fc33 | | | 16 | "bash", "sql", "graphql", "xml", "c", "cpp", "java", "kotlin", |
| bf5fc33 | | | 17 | "swift", "lua", "diff", "dockerfile", "makefile", "ini", |
| bf5fc33 | | | 18 | ], |
| bf5fc33 | | | 19 | }); |
| bf5fc33 | | | 20 | } |
| bf5fc33 | | | 21 | return highlighter; |
| bf5fc33 | | | 22 | } |
| 3e3af55 | | | 23 | |
| 3e3af55 | | | 24 | interface Props { |
| 3e3af55 | | | 25 | params: Promise<{ owner: string; repo: string; path: string[] }>; |
| 3e3af55 | | | 26 | } |
| 3e3af55 | | | 27 | |
| 1da9874 | | | 28 | export async function generateMetadata({ params }: Props): Promise<Metadata> { |
| 86450dc | | | 29 | const { repo, path: pathParts } = await params; |
| 1da9874 | | | 30 | const filePath = pathParts.slice(1).join("/"); |
| 1da9874 | | | 31 | const fileName = filePath.split("/").pop() ?? filePath; |
| 86450dc | | | 32 | return { title: `${fileName} · ${repo}` }; |
| 1da9874 | | | 33 | } |
| 1da9874 | | | 34 | |
| 3e3af55 | | | 35 | |
| bf5fc33 | | | 36 | function getLang(filename: string): string { |
| bf5fc33 | | | 37 | const ext = filename.split(".").pop()?.toLowerCase() ?? ""; |
| bf5fc33 | | | 38 | const map: Record<string, string> = { |
| bf5fc33 | | | 39 | ts: "typescript", |
| bf5fc33 | | | 40 | tsx: "tsx", |
| bf5fc33 | | | 41 | js: "javascript", |
| bf5fc33 | | | 42 | jsx: "jsx", |
| bf5fc33 | | | 43 | json: "json", |
| bf5fc33 | | | 44 | md: "markdown", |
| bf5fc33 | | | 45 | css: "css", |
| bf5fc33 | | | 46 | html: "html", |
| bf5fc33 | | | 47 | py: "python", |
| bf5fc33 | | | 48 | rs: "rust", |
| bf5fc33 | | | 49 | go: "go", |
| bf5fc33 | | | 50 | rb: "ruby", |
| bf5fc33 | | | 51 | yml: "yaml", |
| bf5fc33 | | | 52 | yaml: "yaml", |
| bf5fc33 | | | 53 | toml: "toml", |
| bf5fc33 | | | 54 | sh: "bash", |
| bf5fc33 | | | 55 | bash: "bash", |
| bf5fc33 | | | 56 | zsh: "bash", |
| bf5fc33 | | | 57 | sql: "sql", |
| bf5fc33 | | | 58 | graphql: "graphql", |
| bf5fc33 | | | 59 | svg: "xml", |
| bf5fc33 | | | 60 | xml: "xml", |
| bf5fc33 | | | 61 | c: "c", |
| bf5fc33 | | | 62 | cpp: "cpp", |
| bf5fc33 | | | 63 | h: "c", |
| bf5fc33 | | | 64 | hpp: "cpp", |
| bf5fc33 | | | 65 | java: "java", |
| bf5fc33 | | | 66 | kt: "kotlin", |
| bf5fc33 | | | 67 | swift: "swift", |
| bf5fc33 | | | 68 | lua: "lua", |
| bf5fc33 | | | 69 | diff: "diff", |
| bf5fc33 | | | 70 | }; |
| bf5fc33 | | | 71 | const nameMap: Record<string, string> = { |
| bf5fc33 | | | 72 | Dockerfile: "dockerfile", |
| bf5fc33 | | | 73 | Makefile: "makefile", |
| bf5fc33 | | | 74 | ".gitignore": "gitignore", |
| bf5fc33 | | | 75 | ".gitmodules": "ini", |
| bf5fc33 | | | 76 | }; |
| 818dc90 | | | 77 | if (filename.startsWith("Dockerfile")) return "dockerfile"; |
| 818dc90 | | | 78 | if (filename.startsWith("Makefile")) return "makefile"; |
| bf5fc33 | | | 79 | return nameMap[filename] ?? map[ext] ?? "text"; |
| 3e3af55 | | | 80 | } |
| 3e3af55 | | | 81 | |
| 12ffdd4 | | | 82 | export default async function BlobPage({ params }: Props) { |
| 3e3af55 | | | 83 | const { owner, repo, path: pathParts } = await params; |
| 12ffdd4 | | | 84 | const ref = pathParts[0] ?? "main"; |
| 12ffdd4 | | | 85 | const path = pathParts.slice(1).join("/"); |
| 3e3af55 | | | 86 | const filename = pathParts[pathParts.length - 1]; |
| 3e3af55 | | | 87 | |
| bc1b2ba | | | 88 | const blob = await getRepoBlob(owner, repo, ref, path); |
| 3e3af55 | | | 89 | |
| 3e3af55 | | | 90 | if (!blob) { |
| 3e3af55 | | | 91 | return ( |
| 135dfe5 | | | 92 | <div className="max-w-3xl mx-auto px-4 py-16"> |
| cf89d3c | | | 93 | <h1 className="text-lg" style={{ color: "var(--text-secondary)" }}> |
| 135dfe5 | | | 94 | File not found |
| 135dfe5 | | | 95 | </h1> |
| 3e3af55 | | | 96 | </div> |
| 3e3af55 | | | 97 | ); |
| 3e3af55 | | | 98 | } |
| 3e3af55 | | | 99 | |
| 3e3af55 | | | 100 | const lines = blob.content.split("\n"); |
| bf5fc33 | | | 101 | const lang = getLang(filename); |
| bf5fc33 | | | 102 | |
| bf5fc33 | | | 103 | let highlighted: string | null = null; |
| bf5fc33 | | | 104 | try { |
| bf5fc33 | | | 105 | const hl = await getHighlighter(); |
| bf5fc33 | | | 106 | const loadedLangs = hl.getLoadedLanguages(); |
| bf5fc33 | | | 107 | const effectiveLang = loadedLangs.includes(lang) ? lang : "text"; |
| bf5fc33 | | | 108 | highlighted = hl.codeToHtml(blob.content, { |
| bf5fc33 | | | 109 | lang: effectiveLang, |
| bf5fc33 | | | 110 | themes: { |
| bf5fc33 | | | 111 | light: "vitesse-light", |
| bf5fc33 | | | 112 | dark: "vitesse-dark", |
| bf5fc33 | | | 113 | }, |
| bf5fc33 | | | 114 | defaultColor: false, |
| bf5fc33 | | | 115 | }); |
| bf5fc33 | | | 116 | } catch (e) { |
| bf5fc33 | | | 117 | console.error(`[shiki] Failed to highlight ${filename} as ${lang}:`, e); |
| bf5fc33 | | | 118 | } |
| bf5fc33 | | | 119 | |
| bf5fc33 | | | 120 | let htmlLines: string[] | null = null; |
| bf5fc33 | | | 121 | if (highlighted) { |
| bf5fc33 | | | 122 | const codeMatch = highlighted.match(/<code[^>]*>([\s\S]*)<\/code>/); |
| bf5fc33 | | | 123 | if (codeMatch) { |
| bf5fc33 | | | 124 | htmlLines = codeMatch[1].split("\n"); |
| bf5fc33 | | | 125 | } |
| bf5fc33 | | | 126 | } |
| 3e3af55 | | | 127 | |
| 3e3af55 | | | 128 | return ( |
| 4dfd09b | | | 129 | <div className="px-4 py-6"> |
| bf5fc33 | | | 130 | <div |
| bf5fc33 | | | 131 | style={{ |
| bf5fc33 | | | 132 | border: "1px solid var(--border-subtle)", |
| bf5fc33 | | | 133 | }} |
| bf5fc33 | | | 134 | > |
| bf5fc33 | | | 135 | <div |
| bf5fc33 | | | 136 | className="flex items-center justify-between px-3 py-2 text-xs" |
| bf5fc33 | | | 137 | style={{ |
| bf5fc33 | | | 138 | color: "var(--text-muted)", |
| bf5fc33 | | | 139 | backgroundColor: "var(--bg-inset)", |
| bf5fc33 | | | 140 | borderBottom: "1px solid var(--border-subtle)", |
| bf5fc33 | | | 141 | }} |
| 135dfe5 | | | 142 | > |
| bf5fc33 | | | 143 | <div className="flex items-center gap-4"> |
| bf5fc33 | | | 144 | <span>{formatSize(blob.size)}</span> |
| bf5fc33 | | | 145 | <span>{lines.length} lines</span> |
| bf5fc33 | | | 146 | </div> |
| bf5fc33 | | | 147 | <div className="flex items-center gap-3"> |
| bf5fc33 | | | 148 | <Link |
| d744b82 | | | 149 | href={`/${owner}/${repo}/blame/${ref}/${encodePath(path)}`} |
| bf5fc33 | | | 150 | style={{ color: "var(--accent)" }} |
| bf5fc33 | | | 151 | className="hover:underline" |
| bf5fc33 | | | 152 | > |
| bf5fc33 | | | 153 | Blame |
| bf5fc33 | | | 154 | </Link> |
| bf5fc33 | | | 155 | </div> |
| bf5fc33 | | | 156 | </div> |
| 135dfe5 | | | 157 | |
| bf5fc33 | | | 158 | <div className="overflow-x-auto shiki"> |
| bf5fc33 | | | 159 | <table className="w-full text-sm font-mono"> |
| bf5fc33 | | | 160 | <tbody> |
| bf5fc33 | | | 161 | {htmlLines |
| bf5fc33 | | | 162 | ? htmlLines.map((html, i) => ( |
| bf5fc33 | | | 163 | <tr key={i}> |
| bf5fc33 | | | 164 | <td |
| bf5fc33 | | | 165 | className="text-right select-none pr-4 pl-3 py-0 w-10" |
| bf5fc33 | | | 166 | style={{ |
| bf5fc33 | | | 167 | color: "var(--text-faint)", |
| bf5fc33 | | | 168 | borderRight: "1px solid var(--divide)", |
| bf5fc33 | | | 169 | }} |
| bf5fc33 | | | 170 | > |
| bf5fc33 | | | 171 | {i + 1} |
| bf5fc33 | | | 172 | </td> |
| bf5fc33 | | | 173 | <td |
| bf5fc33 | | | 174 | className="pl-4 py-0 whitespace-pre" |
| bf5fc33 | | | 175 | dangerouslySetInnerHTML={{ __html: html || " " }} |
| bf5fc33 | | | 176 | /> |
| bf5fc33 | | | 177 | </tr> |
| bf5fc33 | | | 178 | )) |
| bf5fc33 | | | 179 | : lines.map((line: string, i: number) => ( |
| bf5fc33 | | | 180 | <tr key={i}> |
| bf5fc33 | | | 181 | <td |
| bf5fc33 | | | 182 | className="text-right select-none pr-4 pl-3 py-0 w-10" |
| bf5fc33 | | | 183 | style={{ |
| bf5fc33 | | | 184 | color: "var(--text-faint)", |
| bf5fc33 | | | 185 | borderRight: "1px solid var(--divide)", |
| bf5fc33 | | | 186 | }} |
| bf5fc33 | | | 187 | > |
| bf5fc33 | | | 188 | {i + 1} |
| bf5fc33 | | | 189 | </td> |
| bf5fc33 | | | 190 | <td |
| bf5fc33 | | | 191 | className="pl-4 py-0 whitespace-pre" |
| bf5fc33 | | | 192 | style={{ color: "var(--text-secondary)" }} |
| bf5fc33 | | | 193 | > |
| bf5fc33 | | | 194 | {line} |
| bf5fc33 | | | 195 | </td> |
| bf5fc33 | | | 196 | </tr> |
| bf5fc33 | | | 197 | ))} |
| bf5fc33 | | | 198 | </tbody> |
| bf5fc33 | | | 199 | </table> |
| bf5fc33 | | | 200 | </div> |
| 3e3af55 | | | 201 | </div> |
| 3e3af55 | | | 202 | </div> |
| 3e3af55 | | | 203 | ); |
| 3e3af55 | | | 204 | } |