File tree Expand file tree Collapse file tree 1 file changed +11
-2
lines changed
Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Original file line number Diff line number Diff line change @@ -107,21 +107,30 @@ export interface GoogleFontFile {
107107 extension : string
108108}
109109
110+ const fontMimeMap : Record < string , string > = {
111+ truetype : "ttf" ,
112+ woff : "woff" ,
113+ woff2 : "woff2" ,
114+ opentype : "otf" ,
115+ }
116+
110117export async function processGoogleFonts (
111118 stylesheet : string ,
112119 baseUrl : string ,
113120) : Promise < {
114121 processedStylesheet : string
115122 fontFiles : GoogleFontFile [ ]
116123} > {
117- const fontSourceRegex = / u r l \( ( h t t p s : \/ \/ f o n t s .g s t a t i c .c o m \/ s \/ [ ^ ) ] + \. ( w o f f 2 | t t f ) ) \) / g
124+ const fontSourceRegex =
125+ / u r l \( ( h t t p s : \/ \/ f o n t s .g s t a t i c .c o m \/ .+ (?: \/ | (?: k i t = ) ) ( .+ ?) [ . & ] .+ ?) \) \s f o r m a t \( ' ( \w + ?) ' \) ; / g
118126 const fontFiles : GoogleFontFile [ ] = [ ]
119127 let processedStylesheet = stylesheet
120128
121129 let match
122130 while ( ( match = fontSourceRegex . exec ( stylesheet ) ) !== null ) {
123131 const url = match [ 1 ]
124- const [ filename , extension ] = url . split ( "/" ) . pop ( ) ! . split ( "." )
132+ const filename = match [ 2 ]
133+ const extension = fontMimeMap [ match [ 3 ] . toLowerCase ( ) ]
125134 const staticUrl = `https://${ baseUrl } /static/fonts/${ filename } .${ extension } `
126135
127136 processedStylesheet = processedStylesheet . replace ( url , staticUrl )
You can’t perform that action at this time.
0 commit comments