Skip to content

Commit

Permalink
refactor(web-fonts): resue provider
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 4, 2022
1 parent 059c0db commit 829f5bd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 41 deletions.
2 changes: 2 additions & 0 deletions packages/preset-web-fonts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Preset } from '@unocss/core'
import { toArray } from '@unocss/core'
import { BunnyFontsProvider } from './providers/bunny'
import { GoogleFontsProvider } from './providers/google'
import { NoneProvider } from './providers/none'
import type { WebFontMeta, WebFontsOptions, WebFontsProviders } from './types'
Expand All @@ -22,6 +23,7 @@ export function normalizedFontMeta(meta: WebFontMeta | string, defaultProvider:

const providers = {
google: GoogleFontsProvider,
bunny: BunnyFontsProvider,
none: NoneProvider,
}

Expand Down
23 changes: 2 additions & 21 deletions packages/preset-web-fonts/src/providers/bunny.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
import type { Provider } from '../types'
import { createGoogleProvider } from './google'

export const BunnyFontsProvider: Provider = {
name: 'bunny',
getImportUrl(fonts) {
const strings = fonts
.filter(i => i.provider === 'bunny')
.map((i) => {
let name = i.name.replace(/\s+/g, '+')
if (i.weights?.length) {
name += i.italic
? `:ital,wght@${i.weights.flatMap(i => [`0,${i}`, `1,${i}`]).sort().join(';')}`
: `:wght@${i.weights.sort().join(';')}`
}
return `family=${name}`
}).join('&')
return `https://fonts.bunny.net/css2?${strings}&display=swap`
},
getFontName(font) {
return `"${font.name}"`
},
}
export const BunnyFontsProvider = createGoogleProvider('bunny', 'https://fonts.bunny.net')
44 changes: 24 additions & 20 deletions packages/preset-web-fonts/src/providers/google.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import type { Provider } from '../types'
import type { Provider, WebFontsProviders } from '../types'

export const GoogleFontsProvider: Provider = {
name: 'google',
getImportUrl(fonts) {
const strings = fonts
.filter(i => i.provider === 'google')
.map((i) => {
let name = i.name.replace(/\s+/g, '+')
if (i.weights?.length) {
name += i.italic
? `:ital,wght@${i.weights.flatMap(i => [`0,${i}`, `1,${i}`]).sort().join(';')}`
: `:wght@${i.weights.sort().join(';')}`
}
return `family=${name}`
}).join('&')
return `https://fonts.googleapis.com/css2?${strings}&display=swap`
},
getFontName(font) {
return `"${font.name}"`
},
export const GoogleFontsProvider: Provider = createGoogleProvider('google', 'https://fonts.googleapis.com')

export function createGoogleProvider(name: WebFontsProviders, host: string): Provider {
return {
name,
getImportUrl(fonts) {
const strings = fonts
.filter(i => i.provider === name)
.map((i) => {
let name = i.name.replace(/\s+/g, '+')
if (i.weights?.length) {
name += i.italic
? `:ital,wght@${i.weights.flatMap(i => [`0,${i}`, `1,${i}`]).sort().join(';')}`
: `:wght@${i.weights.sort().join(';')}`
}
return `family=${name}`
}).join('&')
return `${host}/css2?${strings}&display=swap`
},
getFontName(font) {
return `"${font.name}"`
},
}
}

0 comments on commit 829f5bd

Please sign in to comment.