-
-
Notifications
You must be signed in to change notification settings - Fork 779
/
bunny.ts
33 lines (30 loc) · 1.16 KB
/
bunny.ts
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
import type { Provider, WebFontsProviders } from '../types'
export function createBunnyFontsProvider(
name: WebFontsProviders,
host: string,
): Provider {
return {
name,
getImportUrl(fonts): string {
const fontFamilies = fonts.map((font) => {
const { name, weights, italic } = font
const formattedName = name.toLowerCase().replace(/\s/g, '-')
if (!weights?.length) {
return `${formattedName}${italic ? ':i' : ''}`
}
let weightsAsString = weights.sort().map(weight => weight.toString())
// 1. if weights have at least one element that has 'i', ignore the `italic` flag.
// 2. if none of the weights have an 'i' and italic is true, append an 'i'
const weightsHaveItalic = weightsAsString.some(weight => weight.endsWith('i'))
if (!weightsHaveItalic && italic)
weightsAsString = weightsAsString.map(weight => weight += 'i')
return `${formattedName}:${weightsAsString.join(',')}`
})
return `${host}/css?family=${fontFamilies.join('|')}`
},
}
}
export const BunnyFontsProvider: Provider = createBunnyFontsProvider(
'bunny',
'https://fonts.bunny.net',
)