--- // fetch all commits for just this page's path type Props = { path: string; }; const { path } = Astro.props as Props; const resolvedPath = `examples/docs/${path}`; const url = `https://api.github.com/repos/withastro/astro/commits?path=${resolvedPath}`; const commitsURL = `https://github.com/withastro/astro/commits/main/${resolvedPath}`; type Commit = { author: { id: string; login: string; }; }; async function getCommits(url: string) { try { const token = import.meta.env.SNOWPACK_PUBLIC_GITHUB_TOKEN ?? 'hello'; if (!token) { throw new Error( 'Cannot find "SNOWPACK_PUBLIC_GITHUB_TOKEN" used for escaping rate-limiting.' ); } const auth = `Basic ${Buffer.from(token, 'binary').toString('base64')}`; const res = await fetch(url, { method: 'GET', headers: { Authorization: auth, 'User-Agent': 'astro-docs/1.0', }, }); const data = await res.json(); if (!res.ok) { throw new Error( `Request to fetch commits failed. Reason: ${res.statusText} Message: ${data.message}` ); } return data as Commit[]; } catch (e) { console.warn(`[error] /src/components/AvatarList.astro ${(e as any)?.message ?? e}`); return [] as Commit[]; } } function removeDups(arr: Commit[]) { const map = new Map(); for (let item of arr) { const author = item.author; // Deduplicate based on author.id map.set(author.id, { login: author.login, id: author.id }); } return [...map.values()]; } const data = await getCommits(url); const unique = removeDups(data); const recentContributors = unique.slice(0, 3); // only show avatars for the 3 most recent contributors const additionalContributors = unique.length - recentContributors.length; // list the rest of them as # of extra contributors ---
{ additionalContributors > 0 && ( {`and ${additionalContributors} additional contributor${ additionalContributors > 1 ? 's' : '' }.`} ) } {unique.length === 0 && Contributors}