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
+52 -2
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