Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(preset-web-fonts): fix creation of import URL for
bunny
(#2766)
- Loading branch information
1 parent
077b942
commit 3c28607
Showing
1 changed file
with
32 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,33 @@ | ||
import { createGoogleCompatibleProvider } from './google' | ||
import type { Provider, WebFontsProviders } from '../types' | ||
|
||
export const BunnyFontsProvider = createGoogleCompatibleProvider('bunny', 'https://fonts.bunny.net') | ||
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', | ||
) |