Skip to content

Commit 8d5b13e

Browse files
authored
fix(fonts): Fixed page title fonts not downloadable to local (#1898)
* Fixed url parser regex not working for Google Fonts subset API * Prettier no
1 parent 4d07ac9 commit 8d5b13e

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

quartz/util/theme.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff 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+
110117
export async function processGoogleFonts(
111118
stylesheet: string,
112119
baseUrl: string,
113120
): Promise<{
114121
processedStylesheet: string
115122
fontFiles: GoogleFontFile[]
116123
}> {
117-
const fontSourceRegex = /url\((https:\/\/fonts.gstatic.com\/s\/[^)]+\.(woff2|ttf))\)/g
124+
const fontSourceRegex =
125+
/url\((https:\/\/fonts.gstatic.com\/.+(?:\/|(?:kit=))(.+?)[.&].+?)\)\sformat\('(\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)

0 commit comments

Comments
 (0)