Skip to content

Commit 41bea55

Browse files
authored
✨ Add support for allowing specific fonts when preloading (#46)
2 parents 8d249f7 + abf41e7 commit 41bea55

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,15 @@ $fonts = Webfonts::fonts();
113113
$html = Webfonts::preload()->build();
114114
```
115115

116-
Excluding certain fonts from being preloaded can be done inside `register()` of a service provider:
116+
Allowing/excluding certain fonts from being preloaded can be done inside `register()` of a service provider:
117117

118118
```php
119119
use Log1x\LaravelWebfonts\Webfonts;
120120

121+
// Allow specific fonts.
122+
Webfonts::only(['inter-v13-latin-regular']);
123+
124+
// Exclude specific fonts.
121125
Webfonts::except(['inter-v13-latin-500']);
122126
```
123127

src/Webfonts.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ class Webfonts
2727
protected bool $wordpress = false;
2828

2929
/**
30-
* The exception list.
30+
* The fonts to allow.
31+
*/
32+
public static array $only = [];
33+
34+
/**
35+
* The fonts to exclude.
3136
*/
3237
public static array $except = [];
3338

@@ -74,19 +79,29 @@ public static function except(array $except): void
7479
static::$except = [...static::$except, ...$except];
7580
}
7681

82+
/**
83+
* Set the fonts to allow.
84+
*/
85+
public static function only(array $only): void
86+
{
87+
static::$only = [...static::$only, ...$only];
88+
}
89+
7790
/**
7891
* Retrieve the fonts from the manifest.
7992
*/
80-
public function fonts(array $except = []): array
93+
public function fonts(array $except = [], array $only = []): array
8194
{
8295
if ($this->fonts) {
8396
return $this->fonts;
8497
}
8598

8699
$except = [...static::$except, ...$except];
100+
$only = [...static::$only, ...$only];
87101

88102
return collect($this->manifest())
89103
->filter(fn ($value, $key) => Str::endsWith($key, '.woff2'))
104+
->filter(fn ($value, $key) => blank($only) || in_array(basename($key), $only) || in_array(basename($key, '.woff2'), $only))
90105
->reject(fn ($value, $key) => in_array(basename($key), $except) || in_array(basename($key, '.woff2'), $except))
91106
->all();
92107
}

0 commit comments

Comments
 (0)