Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/generate/__tests__/format-contributor.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,16 @@ test('format contributor with quotes in name', () => {
'<a href="http://github.com/namelastname"><img src="https://avatars1.githubusercontent.com/u/1500684?size=150" width="150px" height="150px" alt="Name &quot;Nickname&quot; Lastname"/><br /><sub><b>Name &quot;Nickname&quot; Lastname</b></sub></a><br /><a href="https://github.com/all-contributors/all-contributors-cli/commits?author=namelastname" title="Documentation">📖</a>'
expect(formatContributor(options, contributor)).toBe(expected)
})

test('format contributor with a relative avatar url', () => {
const contributor = {
...contributors.nologin,
avatar_url: './assets/avatar.png',
}
const {options} = fixtures()

const expected =
'<img src="./assets/avatar.png" width="150px" height="150px" alt="No Github Account"/><br /><sub><b>No Github Account</b></sub><br /><a href="#translation" title="Translation">🌍</a>'

expect(formatContributor(options, contributor)).toBe(expected)
})
24 changes: 16 additions & 8 deletions src/generate/format-contributor.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,31 @@ function escapeName(name) {
.replace(new RegExp('\\"', 'g'), '&quot;')
}

function formatAvatarUrl(avatarUrl, imageSize) {
if (!URL.canParse(avatarUrl)) {
return avatarUrl
}

const url = new URL(avatarUrl)

url.searchParams.append('size', imageSize)

return url.toString()
}

export function formatContributor(options, contributor) {
const formatter = contribution =>
formatContributionType(options, contributor, contribution)
const contributions = contributor.contributions.map(formatter).join(' ')

const contributorAvatarUrl = new URL(contributor.avatar_url)

contributorAvatarUrl.searchParams.append(
'size',
options.imageSize ?? defaultImageSize,
)

const templateData = {
contributions,
contributor: {
...contributor,
avatar_url: contributorAvatarUrl.toString(),
avatar_url: formatAvatarUrl(
contributor.avatar_url,
options.imageSize ?? defaultImageSize,
),
},
options: {imageSize: defaultImageSize, ...options},
}
Expand Down
Loading