Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586import type { CommitSummary } from "$lib/api/repo";import { isMarkdownFile } from "$lib/markup";
export type BlobKind = "code" | "markup" | "svg" | "image" | "video" | "submodule" | "other";
export type BlobViewMode = "code" | "rendered";
export interface RepoBlobView { ref: string; path: string; kind: BlobKind; sizeLabel: string | null; lines: number | null; contents: string | null; renderedHtml: string | null; fileTooLarge: boolean; defaultView: BlobViewMode; submodule: { name: string; url: string } | null; lastCommit: CommitSummary | null;}
// same 1MiB inline cap as the appview's maxBlobSizeexport const MAX_INLINE_SIZE = 1 << 20;
// go-humanize's Bytesexport const formatBytes = (bytes: number): string => { if (bytes < 1000) return `${bytes} B`; const units = ["kB", "MB", "GB", "TB", "PB", "EB"]; let value = bytes; let unit = "B"; for (const next of units) { if (value < 1000) break; value /= 1000; unit = next; } let rendered = value < 10 ? value.toFixed(1) : String(Math.round(value)); // rounding can spill over the boundary ("1000 kB"), promote to the // next unit and render again ("1.0 MB") if (Number.parseFloat(rendered) >= 1000 && unit !== "EB") { value /= 1000; unit = units[units.indexOf(unit) + 1]; rendered = value < 10 ? value.toFixed(1) : String(Math.round(value)); } return `${rendered} ${unit}`;};
// textual application/* types (json, toml, ...) arrive with isBinary=false// instead of a text/* mimeexport const classifyBlob = (blob: { path: string; mimeType?: string; isBinary?: boolean; submodule?: unknown;}): BlobKind => { if (blob.submodule) return "submodule"; const mediaType = (blob.mimeType ?? "").split(";")[0].trim().toLowerCase(); if (mediaType === "image/svg+xml") return "svg"; if (mediaType.startsWith("image/")) return "image"; if (mediaType.startsWith("video/")) return "video"; if (blob.isBinary === false || mediaType.startsWith("text/")) { return isMarkdownFile(blob.path) ? "markup" : "code"; } return "other";};
export const hasTextView = (kind: BlobKind): boolean => kind === "code" || kind === "markup" || kind === "svg";
export const hasRenderedView = (kind: BlobKind): boolean => kind === "markup" || kind === "svg";
// a trailing newline terminates the last line instead of starting a new oneexport const countLines = (text: string): number => { if (text === "") return 0; const lines = text.split("\n").length; return text.endsWith("\n") ? lines - 1 : lines;};
const textEncoder = new TextEncoder();
export const contentStats = ( contents: string): { lines: number; bytes: number; sizeLabel: string } => { const bytes = textEncoder.encode(contents).length; return { lines: countLines(contents), bytes, sizeLabel: formatBytes(bytes) };};