Skip to content

Commit 7efb6ef

Browse files
authored
feat(github): add provider for github avatars (#1990)
1 parent 5718b3d commit 7efb6ef

File tree

8 files changed

+94
-0
lines changed

8 files changed

+94
-0
lines changed

docs/content/3.providers/github.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: GitHub
3+
description: Nuxt Image for easy GitHub avatars.
4+
links:
5+
- label: Source
6+
icon: i-simple-icons-github
7+
to: https://github.com/nuxt/image/blob/main/src/runtime/providers/github.ts
8+
size: xs
9+
---
10+
11+
Easily use GitHub avatars in your Nuxt app.
12+
13+
14+
Just pass the username as the `src` prop and set the width and height. Since GitHub avatars must always be square, the largest dimension is used as the size.
15+
16+
17+
```vue
18+
<!-- Width and Height -->
19+
<NuxtImg provider="github" src="nuxt" height="50" width="50" />
20+
21+
<!-- Width only -->
22+
<NuxtImg provider="github" src="unjs" width="512" />
23+
24+
<!-- Default size -->
25+
<NuxtImg provider="github" src="npm"/>
26+
```
27+

docs/public/providers/github.svg

Lines changed: 1 addition & 0 deletions
Loading

playground/app/providers.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,25 @@ export const providers: Provider[] = [
321321
},
322322
],
323323
},
324+
// GitHub
325+
{
326+
name: 'github',
327+
samples: [
328+
{
329+
src: 'npm',
330+
width: 50,
331+
height: 50,
332+
},
333+
{
334+
src: 'nuxt',
335+
width: 50,
336+
height: 100,
337+
},
338+
{
339+
src: 'unjs',
340+
},
341+
],
342+
},
324343
// gumlet
325344
{
326345
name: 'gumlet',

playground/nuxt.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export default defineNuxtConfig({
7171
filerobot: {
7272
baseURL: 'https://fesrinkeb.filerobot.com/',
7373
},
74+
github: {},
7475
glide: {
7576
baseURL: 'https://glide.herokuapp.com/1.0/',
7677
},

src/provider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const BuiltInProviders = [
2424
'directus',
2525
'fastly',
2626
'filerobot',
27+
'github',
2728
'glide',
2829
'gumlet',
2930
'hygraph',

src/runtime/providers/github.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { encodeQueryItem, joinURL } from 'ufo'
2+
import { createOperationsGenerator } from '../utils/index'
3+
import { defineProvider } from '../utils/provider'
4+
5+
const operationsGenerator = createOperationsGenerator({
6+
joinWith: '&',
7+
formatter: (key, value) => encodeQueryItem(key, value),
8+
})
9+
10+
interface GitHubOptions {
11+
baseURL?: string
12+
}
13+
14+
export default defineProvider<GitHubOptions>({
15+
getImage: (src, { modifiers, baseURL = 'https://avatars.githubusercontent.com/' }) => {
16+
let size = 460 // Default size
17+
// Calculate size based on width/height
18+
if (modifiers?.width || modifiers?.height) {
19+
size = Math.max(modifiers?.height ?? 0, modifiers?.width ?? 0)
20+
}
21+
22+
const operations = operationsGenerator({
23+
v: 4,
24+
s: size,
25+
})
26+
27+
return {
28+
url: joinURL(baseURL, src + '?' + operations),
29+
}
30+
},
31+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"requests": [
3+
"https://avatars.githubusercontent.com/npm?v=4&s=50",
4+
"https://avatars.githubusercontent.com/nuxt?v=4&s=100",
5+
"https://avatars.githubusercontent.com/unjs?v=4&s=460",
6+
],
7+
"sources": [
8+
"https://avatars.githubusercontent.com/npm?v=4&s=50",
9+
"https://avatars.githubusercontent.com/nuxt?v=4&s=100",
10+
"https://avatars.githubusercontent.com/unjs?v=4&s=460",
11+
],
12+
}

test/providers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const images = [
1111
fastly: { url: '/test.png' },
1212
filerobot: { url: '/monitoring3/boat-2023-03-06T15%3A03%3A12.6382894Z?vh=b3583b' },
1313
glide: { url: '/test.png' },
14+
github: { url: 'npm' },
1415
gumlet: { url: '/test.png' },
1516
imgix: { url: '/test.png' },
1617
imageengine: { url: '/test.png' },
@@ -47,6 +48,7 @@ export const images = [
4748
twicpics: { url: '/test.png?twic=v1/cover=200x-' },
4849
fastly: { url: '/test.png?width=200' },
4950
filerobot: { url: '/monitoring3/boat-2023-03-06T15%3A03%3A12.6382894Z?vh=b3583b?w=200' },
51+
github: { url: 'https://avatars.githubusercontent.com/npm?v=4' },
5052
glide: { url: '/test.png?w=200' },
5153
gumlet: { url: '/test.png?w=200' },
5254
imgix: { url: '/test.png?w=200' },

0 commit comments

Comments
 (0)