Skip to content

Commit

Permalink
feat(git changelog): resolve profile picture for GitHub emails
Browse files Browse the repository at this point in the history
  • Loading branch information
giladgd committed Aug 2, 2024
1 parent 31dd251 commit 86aa24c
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/vitepress-plugin-git-changelog/src/vite/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,29 @@ export function findMapAuthorI18n(mappedAuthor: Contributor): Record<string, str
return undefined
}

function getAvatarFromGithubNoreplyAddress(email: string | undefined, size: number = 80): string | undefined {
if (!email)
return undefined

const match = email.match(/^(?:(?<userId>\d+)\+)?(?<userName>[a-zA-Z\d-]{1,39})@users.noreply.github.com$/)
if (!match || !match.groups)
return undefined

const { userName, userId } = match.groups
return `https://avatars.githubusercontent.com/${userId ? `u/${userId}` : userName}?size=${size}`
}

export async function newAvatarForAuthor(mappedAuthor: Contributor | undefined, email: string): Promise<string> {
if (mappedAuthor) {
if (mappedAuthor.avatar)
return mappedAuthor.avatar
if (mappedAuthor.username)
return `https://github.com/${mappedAuthor.username}.png`
}

const githubProfilePicture = getAvatarFromGithubNoreplyAddress(email)
if (githubProfilePicture != null)
return githubProfilePicture

return `https://gravatar.com/avatar/${await digestStringAsSHA256(email)}?d=retro`
}

0 comments on commit 86aa24c

Please sign in to comment.