Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | 161x 161x 161x 8x 8x 8x 8x 5x 5x 4x 1x 15x 3x 3x 12x 12x 12x 12x 12x 157x 157x 3x 3x 3x 3x | import type {
BrowserOverrideSelection,
ExamplesRef,
ExamplesRequestSelection,
RepositorySlug,
} from "@shared/examples";
import { examplesRefSchema, repositorySlugSchema } from "@shared/examples";
import { ExamplesError } from "./examples-error";
export type ResolvedExamplesSelection =
| { selection: "default"; mode: "builtin"; repository: null; ref: null }
| {
selection: "default" | "browser-override";
mode: "repository-ref";
repository: RepositorySlug;
ref: ExamplesRef;
};
export interface ExamplesDefaultConfig {
mode: "builtin" | "repository-ref";
ref: string;
repository: string | null;
}
export function normalizeRepositoryInput(
value: string,
options: { allowRawGithub?: boolean } = {},
): RepositorySlug | null {
const trimmed = value.trim();
Iif (!trimmed) return null;
if (!trimmed.includes("://")) return parseCanonicalCandidate(trimmed.replace(/\.git\/?$/i, ""));
let url: URL;
try { url = new URL(trimmed); }
catch { return null; }
Iif (url.protocol !== "https:" || url.username || url.password || url.search || url.hash) return null;
const host = url.hostname.toLowerCase();
if (host !== "github.com" && !(options.allowRawGithub && host === "raw.githubusercontent.com")) return null;
const segments = url.pathname.split("/").filter(Boolean);
if (segments.length !== 2) return null;
return parseCanonicalCandidate(`${segments[0]}/${segments[1].replace(/\.git$/i, "")}`);
}
export function toGithubRawRepositoryBase(repository: RepositorySlug): URL {
return new URL(`https://raw.githubusercontent.com/${repository}/`);
}
export function resolveExamplesSelection(
selection: ExamplesRequestSelection,
defaults: ExamplesDefaultConfig,
): ResolvedExamplesSelection {
if (selection.kind === "browser-override") {
const parsed = parseBrowserOverride(selection);
return { selection: "browser-override", mode: "repository-ref", ...parsed };
}
Iif (defaults.mode === "builtin") {
return { selection: "default", mode: "builtin", repository: null, ref: null };
}
const repository = repositorySlugSchema.safeParse(defaults.repository);
const ref = examplesRefSchema.safeParse(defaults.ref);
Iif (!repository.success || !ref.success) {
throw new ExamplesError("INVALID_SELECTION", "Invalid server examples selection");
}
return { selection: "default", mode: "repository-ref", repository: repository.data, ref: ref.data };
}
function parseCanonicalCandidate(value: string): RepositorySlug | null {
const parsed = repositorySlugSchema.safeParse(value.toLowerCase());
return parsed.success ? parsed.data : null;
}
function parseBrowserOverride(selection: BrowserOverrideSelection): BrowserOverrideSelection {
const repository = repositorySlugSchema.safeParse(selection.repository);
const ref = examplesRefSchema.safeParse(selection.ref);
Iif (!repository.success || !ref.success) {
throw new ExamplesError("INVALID_SELECTION", "Invalid external examples selection");
}
return { repository: repository.data, ref: ref.data };
}
|