Skip to content

Commit 3f54c9a

Browse files
ras0qoscarotero
authored andcommitted
fix(google_fonts): handle unnamed @font-face subsets
1 parent 8764459 commit 3f54c9a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

plugins/google_fonts.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,24 @@ interface FontFace {
114114
}
115115

116116
function extractFontFaces(css: string, name: string): FontFace[] {
117-
const fontFaces = css.match(/\/\*[^*]+\*\/[\s]+@font-face {[^}]+}/g) || [];
117+
const fontFaces = css.match(/(\/\*[^*]+\*\/)?[\s]+@font-face {[^}]+}/g) || [];
118118

119+
let unnamedSubsetId = 1;
119120
return fontFaces.map((fontFace) => {
120-
const subset = fontFace.match(/\/\* ([^*]+) \*\//)?.[1];
121+
let subset = fontFace.match(/\/\* ([^*]+) \*\//)?.[1];
121122
let family = fontFace.match(/font-family: '([^']+)'/)?.[1];
122123
const style = fontFace.match(/font-style: ([^;]+);/)?.[1];
123124
const weight = fontFace.match(/font-weight: ([^;]+);/)?.[1];
124125
const stretch = fontFace.match(/font-stretch: ([^;]+);/)?.[1];
125126
const src = fontFace.match(/src: url\('?([^']+)'?\)/)?.[1];
126127
const range = fontFace.match(/unicode-range: ([^;]+);/)?.[1];
127128

128-
if (!family || !style || !weight || !src || !range || !subset) {
129+
if (!subset) {
130+
subset = `[${unnamedSubsetId}]`;
131+
unnamedSubsetId++;
132+
}
133+
134+
if (!family || !style || !weight || !src || !range) {
129135
throw new Error("Invalid font-face");
130136
}
131137

0 commit comments

Comments
 (0)