Golang新版Discourse论坛如何启用暗黑模式
Golang新版Discourse论坛如何启用暗黑模式 大家好,
是否有可能将网站更新到支持深色模式的最新版 Discourse?
谢谢
1 回复
更多关于Golang新版Discourse论坛如何启用暗黑模式的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
在Discourse中启用暗色主题需要管理后台操作,以下是具体步骤:
- 创建暗色主题:
# 通过Discourse命令行创建主题
./launcher enter app
rake themes:install -- --url https://github.com/discourse/discourse-color-scheme-dark
- 后台配置:
- 访问
/admin/customize/themes - 点击"安装主题" → 选择"官方暗色配色方案"
- 创建新主题并选择暗色配色方案作为基础
- 设为默认主题:
// 在主题设置中添加自动检测代码
const darkModeQuery = window.matchMedia('(prefers-color-scheme: dark)');
if (darkModeQuery.matches) {
document.documentElement.setAttribute('data-theme', 'dark');
}
- 用户选择支持:
// 示例:记录用户主题偏好(Go后端处理)
type UserPreference struct {
UserID int64 `json:"user_id"`
Theme string `json:"theme"` // "light" 或 "dark"
}
func updateUserTheme(userID int64, theme string) error {
// 更新数据库中的用户主题设置
_, err := db.Exec(`
UPDATE user_options
SET theme_key = $1
WHERE user_id = $2
`, theme, userID)
return err
}
- 强制刷新缓存:
# 清理主题缓存
./launcher enter app
rails r "Theme.clear_cache!"
最新版Discourse(v3.2+)已内置暗色模式支持,用户可在个人设置中直接切换。管理员也可通过admin/site_settings/category/theme配置全局默认主题。

