Skip to content

Commit 9d5f56b

Browse files
tobynguyen27autofix-ci[bot]zyyv
authored
feat(preset-web-fonts): add CoolLabs fonts provider (#4649)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Chris <[email protected]>
1 parent ce4332c commit 9d5f56b

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

packages-presets/preset-web-fonts/src/preset.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Preset } from '@unocss/core'
22
import type { Provider, ResolvedWebFontMeta, WebFontMeta, WebFontsOptions, WebFontsProviders } from './types'
33
import { LAYER_IMPORTS, toArray } from '@unocss/core'
44
import { BunnyFontsProvider } from './providers/bunny'
5+
import { CoolLabsFontsProvider } from './providers/coollabs'
56
import { FontshareProvider } from './providers/fontshare'
67
import { FontSourceProvider } from './providers/fontsource'
78
import { GoogleFontsProvider } from './providers/google'
@@ -12,6 +13,7 @@ const builtinProviders = {
1213
bunny: BunnyFontsProvider,
1314
fontshare: FontshareProvider,
1415
fontsource: FontSourceProvider,
16+
coollabs: CoolLabsFontsProvider,
1517
none: NoneProvider,
1618
}
1719

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { Provider, WebFontsProviders } from '../types'
2+
3+
export function createCoolLabsCompatibleProvider(name: WebFontsProviders, host: string): Provider {
4+
return {
5+
name,
6+
getImportUrl(fonts) {
7+
const sort = (weights: string[][]) => {
8+
const firstW = weights.map(w => w[0])
9+
const lastW = weights.map(w => w[1])
10+
return `${firstW.join(';')};${lastW.join(';')}`
11+
}
12+
13+
const strings = fonts
14+
.map((i) => {
15+
let name = i.name.replace(/\s+/g, '+')
16+
if (i.weights?.length) {
17+
name += i.italic
18+
? `:ital,wght@${sort(i.weights.map(w => [`0,${w}`, `1,${w}`]))}`
19+
: `:wght@${i.weights.join(';')}`
20+
}
21+
return `family=${name}`
22+
})
23+
.join('&')
24+
return `${host}/css2?${strings}&display=swap`
25+
},
26+
}
27+
}
28+
29+
export const CoolLabsFontsProvider: Provider = createCoolLabsCompatibleProvider('coollabs', 'https://api.fonts.coollabs.io')

packages-presets/preset-web-fonts/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Arrayable, Awaitable } from '@unocss/core'
22

3-
export type WebFontsProviders = 'google' | 'bunny' | 'fontshare' | 'fontsource' | 'none' | Provider
3+
export type WebFontsProviders = 'google' | 'bunny' | 'fontshare' | 'fontsource' | 'coollabs' | 'none' | Provider
44

55
export interface WebFontMeta {
66
name: string

0 commit comments

Comments
 (0)