feat(font): 系统管理里可设置正文字体(font 目录或链接)

- 新增 GET /api/fonts 列出二进制同级 font/ 目录里的字体文件,/fonts/<文件名> 对外提供(只认一级文件名,拒绝穿越)
- 站点设置新增 font.url / font.family,校验只接受 http(s):// 或 /fonts/ 开头的链接
- 前台按设置注入 @font-face 或 CSS 链接并覆盖 --su-font-sans;留空回到内置文楷
- 后台「系统管理」新增字体选择 + 链接 + 族名;撤销/保存按钮覆盖该页
- 撤掉上一轮的霞鹜新晰黑改动
This commit is contained in:
2026-11-05 18:12:29 +08:00
parent e243a20b69
commit 0bb4cc354a
13 changed files with 316 additions and 15 deletions
+36
View File
@@ -333,6 +333,7 @@ export default function AppNext() {
show_nav: true,
},
background: cachedBackground ?? { light: '', dark: '' },
font: { url: '', family: '' },
search: {
default: 'internal',
engines: DEFAULT_SEARCH_ENGINES.map((engine) => ({ ...engine })),
@@ -408,6 +409,41 @@ export default function AppNext() {
}
}, [scheme])
// 正文字体:空链接用内置文楷;否则注入 @font-face(字体文件)或 CSS 链接,并覆盖字体令牌
useEffect(() => {
const url = site.font.url.trim()
const family = site.font.family.trim()
const root = document.documentElement
document.getElementById('stash-font-link')?.remove()
document.getElementById('stash-font-face')?.remove()
if (url === '') {
root.style.removeProperty('--su-font-sans')
return
}
const familyName = family === '' ? 'Stash Font' : family
if (url.toLowerCase().endsWith('.css')) {
const link = document.createElement('link')
link.id = 'stash-font-link'
link.rel = 'stylesheet'
link.href = url
document.head.appendChild(link)
} else {
const style = document.createElement('style')
style.id = 'stash-font-face'
// 用 JSON.stringify 包成 CSS 字符串,引号 / 反斜杠不用自己转义
style.textContent = `@font-face{font-family:${JSON.stringify(familyName)};src:url(${JSON.stringify(url)});font-display:swap}`
document.head.appendChild(style)
}
root.style.setProperty(
'--su-font-sans',
`${JSON.stringify(familyName)}, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Hiragino Sans GB', 'Microsoft YaHei', 'Source Han Sans CN', sans-serif`,
)
}, [site.font.url, site.font.family])
/** 页头的明暗切换:只写本机偏好,不需要登录;站点默认值在后台改 */
const toggleTheme = () => {
const next: ThemeChoice = isDark ? 'light' : 'dark'