mirror of
https://github.com/ChenQihan666/MyBlog-Next.git
synced 2026-08-14 07:33:07 +08:00
fix: iterate all weight matches in getFontPathByWeight (#645)
* fix: iterate all weight matches in getFontPathByWeight When fontData has multiple entries for the same weight with different format sets, Array.find() stops at the first match even if that entry does not contain the requested format. The fix uses a for...of loop that returns only when both weight AND format match. * fix: add fallback to first src entry and trailing newline - Use `?? font.src[0]` as fallback when no src entry matches the requested format, as suggested in code review - Add missing trailing newline at end of file Fixes #644
This commit is contained in:
@@ -11,7 +11,12 @@ export function getFontPathByWeight(
|
||||
const style = options?.style ?? "normal";
|
||||
const format = options?.format ?? "truetype";
|
||||
|
||||
return fonts
|
||||
.find(font => font.weight === String(weight) && font.style === style)
|
||||
?.src.find(file => file.format === format)?.url;
|
||||
for (const font of fonts) {
|
||||
if (font.weight === String(weight) && font.style === style) {
|
||||
const src = font.src.find(file => file.format === format) ?? font.src[0];
|
||||
if (src) return src.url;
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user