Skip to content

Commit

Permalink
refactor(google): slight refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Feb 20, 2024
1 parent 306d2df commit e369485
Showing 1 changed file with 44 additions and 31 deletions.
75 changes: 44 additions & 31 deletions src/providers/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,36 @@ import { $fetch } from 'ofetch'

import type { FontProvider } from '../types'

export default {
async setup () {
await initialiseFontMeta()
},
async resolveFontFaces (fontFamily, defaults) {
if (!isGoogleFont(fontFamily)) { return }

const details = await getFontDetails(fontFamily, defaults.subsets)

return {
fonts: details.variants.map(variant => ({
style: variant.fontStyle,
weight: variant.fontWeight,
// TODO: handle subset unicode ranges
// TODO: download/proxy URLs locally
src: [
...variant.local?.map(name => ({ name })) || [fontFamily],
...variant.woff2 ? [{ url: variant.woff2, format: 'woff2' }] : [],
...variant.woff ? [{ url: variant.woff, format: 'woff' }] : [],
...variant.ttf ? [{ url: variant.ttf, format: 'truetype' }] : [],
...variant.eot ? [{ url: variant.eot, format: 'embedded-opentype' }] : [],
...variant.svg ? [{ url: variant.svg, format: 'svg' }] : [],
]
}))
}
},
} satisfies FontProvider

// https://github.com/majodev/google-webfonts-helper

interface FontIndexMeta {
category: string
defSubset: string
Expand Down Expand Up @@ -38,37 +68,20 @@ const fontAPI = $fetch.create({

let fonts: FontIndexMeta[]

export default {
async setup () {
// TODO: Fetch and cache possible Google fonts
fonts = await fontAPI<FontIndexMeta[]>('/')
},
async resolveFontFaces (fontFamily, defaults) {
const font = fonts.find(font => font.family === fontFamily)
if (!font) { return }

const subsets = defaults.subsets.filter(subset => font.subsets.includes(subset))
// TODO: Fetch and cache possible Google fonts
async function initialiseFontMeta () {
fonts = await fontAPI<FontIndexMeta[]>('/')
}

const details = await fontAPI<FontDetail>(font.id, {
query: subsets.length ? { subsets: subsets.join(',') } : {}
})
function isGoogleFont (family: string) {
return fonts.some(font => font.family === family)
}

return {
fonts: details.variants.map(variant => ({
style: variant.fontStyle,
weight: variant.fontWeight,
// TODO: handle subset unicode ranges
// TODO: download/proxy URLs locally
src: [
...variant.local?.map(name => ({ name })) || [fontFamily],
...variant.woff2 ? [{ url: variant.woff2, format: 'woff2' }] : [],
...variant.woff ? [{ url: variant.woff, format: 'woff' }] : [],
...variant.ttf ? [{ url: variant.ttf, format: 'truetype' }] : [],
...variant.eot ? [{ url: variant.eot, format: 'embedded-opentype' }] : [],
...variant.svg ? [{ url: variant.svg, format: 'svg' }] : [],
]
}))
}
},
} satisfies FontProvider
async function getFontDetails (family: string, defaultSubsets: string[]) {
const font = fonts.find(font => font.family === family)!
const subsets = defaultSubsets.filter(subset => font.subsets.includes(subset))

return await fontAPI<FontDetail>(font.id, {
query: subsets.length ? { subsets: subsets.join(',') } : {}
})
}

0 comments on commit e369485

Please sign in to comment.