theme v2
This commit is contained in:
@@ -26,6 +26,7 @@ import (
|
||||
"github.com/tamakyi/TamaBox/internal/i18n"
|
||||
"github.com/tamakyi/TamaBox/internal/shared"
|
||||
templatepkg "github.com/tamakyi/TamaBox/internal/template"
|
||||
"github.com/tamakyi/TamaBox/internal/theme"
|
||||
)
|
||||
|
||||
type EndpointType string
|
||||
@@ -239,6 +240,13 @@ func Contexter() flamego.Handler {
|
||||
isUserPage := strings.HasPrefix(path, "/user") || strings.HasPrefix(path, "/_/")
|
||||
c.Data["IsUserPage"] = isUserPage
|
||||
|
||||
// 主题:/user 后台是箱主本人的页面,跟随本人配置;
|
||||
// /_/{domain} 的箱主主题由 question.Pager 覆盖(访客看到的是箱主的选择)。
|
||||
// 空串(未选且站点无默认主题)= 不加载主题 css。
|
||||
if isThemeScope(path) && c.IsLogged {
|
||||
c.Data["ThemeSlug"] = theme.Resolve(c.User.Theme)
|
||||
}
|
||||
|
||||
// SEO:仅公开页(首页/注册/登录/explore)允许被搜索引擎收录;
|
||||
// 提问内容(提问箱页、单题页)、密码页、私信、用户后台等隐私页一律 noindex。
|
||||
isPublicPage := path == "/" || path == "/register" || path == "/login" || strings.HasPrefix(path, "/explore")
|
||||
@@ -327,3 +335,10 @@ func (c *Context) Lang() string {
|
||||
}
|
||||
return i18n.Default
|
||||
}
|
||||
|
||||
// isThemeScope 判断该路径的主题是否由「当前登录用户自己的配置」决定。
|
||||
// 只覆盖 /user 后台(本人页面);提问箱相关页面由 question.Pager 按箱主注入,
|
||||
// 公开页(首页/探索/登录)保持站点默认外观,不跟随登录状态变化。
|
||||
func isThemeScope(path string) bool {
|
||||
return path == "/user" || strings.HasPrefix(path, "/user/")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package context
|
||||
|
||||
import "testing"
|
||||
|
||||
// 主题作用域:/user 后台必须命中(否则设置页、题目管理页拿不到自己的主题),
|
||||
// 其余路径不得命中(提问箱页面由 question.Pager 按箱主注入,公开页保持站点默认)。
|
||||
func TestIsThemeScope(t *testing.T) {
|
||||
cases := map[string]bool{
|
||||
"/user": true,
|
||||
"/user/": true,
|
||||
"/user/profile": true,
|
||||
"/user/questions": true,
|
||||
"/user/votes/1/results": true,
|
||||
"/username": false, // 前缀相近但不是用户后台
|
||||
"/_/admin6": false,
|
||||
"/_/admin6/share-card": false,
|
||||
"/": false,
|
||||
"/explore": false,
|
||||
"/vote/abc": false,
|
||||
"/api/v1/user/admin6": false,
|
||||
"/api/v1/user": false,
|
||||
"/deactivated": false,
|
||||
}
|
||||
|
||||
for path, want := range cases {
|
||||
if got := isThemeScope(path); got != want {
|
||||
t.Errorf("isThemeScope(%q) = %v, want %v", path, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user