Clean up AppURL, remove legacy origin-url webcomponent (#37090)

1. `origin-url` was introduced in the past when there was no good
framework support to detect current host url
    * It is not needed anymore
    * Removing it makes the code clearer
2. Separate template helper functions for different templates (web
page/mail)
3. The "AppURL" info is removed from admin config page: it doesn't
really help.
    * We already have various app url checks at many places
This commit is contained in:
wxiaoguang
2026-04-04 01:56:31 +08:00
committed by GitHub
parent d80640fa5d
commit f9f9876f2c
35 changed files with 108 additions and 80 deletions

View File

@@ -25,8 +25,7 @@ import (
"code.gitea.io/gitea/services/gitdiff"
)
// NewFuncMap returns functions for injecting to templates
func NewFuncMap() template.FuncMap {
func newFuncMapWebPage() template.FuncMap {
return map[string]any{
"DumpVar": dumpVar,
"NIL": func() any { return nil },
@@ -40,7 +39,6 @@ func NewFuncMap() template.FuncMap {
"QueryEscape": queryEscape,
"QueryBuild": QueryBuild,
"SanitizeHTML": SanitizeHTML,
"DotEscape": dotEscape,
"PathEscape": url.PathEscape,
"PathEscapeSegments": util.PathEscapeSegments,
@@ -61,6 +59,7 @@ func NewFuncMap() template.FuncMap {
// -----------------------------------------------------------------
// time / number / format
"ShortSha": base.ShortSha,
"FileSize": base.FileSize,
"CountFmt": countFmt,
"Sec2Hour": util.SecToHours,
@@ -73,6 +72,7 @@ func NewFuncMap() template.FuncMap {
"AssetURI": public.AssetURI,
"ScriptImport": scriptImport,
// -----------------------------------------------------------------
// setting
"AppName": func() string {
@@ -84,17 +84,10 @@ func NewFuncMap() template.FuncMap {
"AssetUrlPrefix": func() string {
return setting.StaticURLPrefix + "/assets"
},
"AppUrl": func() string {
// The usage of AppUrl should be avoided as much as possible,
// because the AppURL(ROOT_URL) may not match user's visiting site and the ROOT_URL in app.ini may be incorrect.
// And it's difficult for Gitea to guess absolute URL correctly with zero configuration,
// because Gitea doesn't know whether the scheme is HTTP or HTTPS unless the reverse proxy could tell Gitea.
return setting.AppURL
},
"AppVer": func() string {
return setting.AppVer
},
"AppDomain": func() string { // documented in mail-templates.md
"AppDomain": func() string { // TODO: helm registry still uses it, need to use current request host in the future
return setting.Domain
},
"ShowFooterTemplateLoadTime": func() bool {
@@ -143,7 +136,6 @@ func NewFuncMap() template.FuncMap {
// -----------------------------------------------------------------
// misc (TODO: move them to MiscUtils to avoid bloating the main func map)
"ShortSha": base.ShortSha,
"ActionContent2Commits": ActionContent2Commits,
"IsMultilineCommitMessage": isMultilineCommitMessage,
"CommentMustAsDiff": gitdiff.CommentMustAsDiff,
@@ -177,11 +169,6 @@ func queryEscape(s string) template.URL {
return template.URL(url.QueryEscape(s))
}
// dotEscape wraps a dots in names with ZWJ [U+200D] in order to prevent auto-linkers from detecting these as urls
func dotEscape(raw string) string {
return strings.ReplaceAll(raw, ".", "\u200d.\u200d")
}
// iif is an "inline-if", similar util.Iif[T] but templates need the non-generic version,
// and it could be simply used as "{{iif expr trueVal}}" (omit the falseVal).
func iif(condition any, vals ...any) any {

View File

@@ -6,12 +6,14 @@ package templates
import (
"html/template"
"io"
"net/url"
"regexp"
"slices"
"strings"
"sync"
texttmpl "text/template"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
@@ -34,6 +36,11 @@ type MailRender struct {
mockedBodyTemplates map[string]*template.Template
}
// dotEscape wraps a dots in names with ZWJ [U+200D] in order to prevent auto-linkers from detecting these as urls
func dotEscape(raw string) string {
return strings.ReplaceAll(raw, ".", "\u200d.\u200d")
}
// mailSubjectTextFuncMap returns functions for injecting to text templates, it's only used for mail subject
func mailSubjectTextFuncMap() texttmpl.FuncMap {
return texttmpl.FuncMap{
@@ -41,6 +48,7 @@ func mailSubjectTextFuncMap() texttmpl.FuncMap {
"Eval": evalTokens,
"EllipsisString": util.EllipsisDisplayString,
"AppName": func() string {
return setting.AppName
},
@@ -50,6 +58,48 @@ func mailSubjectTextFuncMap() texttmpl.FuncMap {
}
}
func mailBodyFuncMap() template.FuncMap {
// Some of them are documented in mail-templates.md
return template.FuncMap{
"DumpVar": dumpVar,
"NIL": func() any { return nil },
// html/template related functions
"dict": dict,
"Iif": iif,
"Eval": evalTokens,
"HTMLFormat": htmlFormat,
"QueryEscape": queryEscape,
"QueryBuild": QueryBuild,
"SanitizeHTML": SanitizeHTML,
"PathEscape": url.PathEscape,
"PathEscapeSegments": util.PathEscapeSegments,
"DotEscape": dotEscape,
// utils
"StringUtils": NewStringUtils,
"SliceUtils": NewSliceUtils,
"JsonUtils": NewJsonUtils,
// time / number / format
"ShortSha": base.ShortSha,
"FileSize": base.FileSize,
// setting
"AppName": func() string {
return setting.AppName
},
"AppUrl": func() string {
return setting.AppURL
},
"AppDomain": func() string {
return setting.Domain
},
}
}
var mailSubjectSplit = regexp.MustCompile(`(?m)^-{3,}\s*$`)
func newMailRenderer() (*MailRender, error) {
@@ -103,7 +153,7 @@ func newMailRenderer() (*MailRender, error) {
return renderer.tmplRenderer.Templates().HasTemplate(name)
}
staticFuncMap := NewFuncMap()
staticFuncMap := mailBodyFuncMap()
renderer.BodyTemplates.ExecuteTemplate = func(w io.Writer, name string, data any) error {
if t, ok := renderer.mockedBodyTemplates[name]; ok {
return t.Execute(w, data)
@@ -131,7 +181,7 @@ func (r *MailRender) MockTemplate(name, subject, body string) func() {
texttmpl.Must(r.SubjectTemplates.New(name).Parse(subject))
oldBody, hasOldBody := r.mockedBodyTemplates[name]
mockFuncMap := NewFuncMap()
mockFuncMap := mailBodyFuncMap()
r.mockedBodyTemplates[name] = template.Must(template.New(name).Funcs(mockFuncMap).Parse(body))
return func() {
r.SubjectTemplates = oldSubject

View File

@@ -24,13 +24,13 @@ type pageRenderer struct {
}
func (r *pageRenderer) funcMap(ctx context.Context) template.FuncMap {
pageFuncMap := NewFuncMap()
pageFuncMap := newFuncMapWebPage()
pageFuncMap["ctx"] = func() any { return ctx }
return pageFuncMap
}
func (r *pageRenderer) funcMapDummy() template.FuncMap {
dummyFuncMap := NewFuncMap()
dummyFuncMap := newFuncMapWebPage()
dummyFuncMap["ctx"] = func() any { return nil } // for template compilation only, no context available
return dummyFuncMap
}