---
import { siteConfig } from "@/config";
const { font: fontConfig } = siteConfig;
// 获取选中的字体
const getSelectedFonts = () => {
if (!fontConfig.enable || !fontConfig.selected) return [];
const selectedIds = Array.isArray(fontConfig.selected)
? fontConfig.selected
: [fontConfig.selected];
return selectedIds
.map((id) => fontConfig.fonts[id])
.filter((font) => font?.src); // 过滤掉系统字体和不存在的字体
};
const selectedFonts = getSelectedFonts();
// 生成字体CSS类名
const generateFontClasses = () => {
if (!fontConfig.enable) return [];
const selectedIds = Array.isArray(fontConfig.selected)
? fontConfig.selected
: [fontConfig.selected];
return selectedIds.map((id) => `font-${id}-enabled`);
};
const fontClasses = generateFontClasses();
// 生成font-family回退样式
const generateFontFamilyStyle = () => {
if (!fontConfig.enable || !fontConfig.selected) return "";
const selectedIds = Array.isArray(fontConfig.selected)
? fontConfig.selected
: [fontConfig.selected];
const selectedFontFamilies = selectedIds
.map((id) => fontConfig.fonts[id])
.filter((font) => font)
.map((font) => `"${font.family}"`);
if (selectedFontFamilies.length === 0) return "";
const fallbacks = fontConfig.fallback || [];
const allFonts = [...selectedFontFamilies, ...fallbacks];
return `font-family: ${allFonts.join(", ")};`;
};
const fontFamilyStyle = generateFontFamilyStyle();
---
{
selectedFonts.map((font) => {
// 判断是否为外部链接
const isExternalUrl =
font.src.startsWith("http://") ||
font.src.startsWith("https://") ||
font.src.startsWith("//");
if (isExternalUrl) {
// 外部字体链接 (如 Google Fonts, CDN等)
return ;
} else {
// 本地字体文件
return (
);
}
})
}
{
fontConfig.enable &&
fontConfig.preload &&
selectedFonts
.filter((font) => !font.src.startsWith("http"))
.map((font) => (
))
}
{
fontConfig.enable && fontFamilyStyle && (