mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-06 22:40:31 -05:00
templates: Add pathEscape
template function and use it in file browser (#6278)
* use url.PathEscape in file-server browse template - add `pathEscape` to c.tpl.Funcs, using `url.PathEscape` - use `pathEscape` in browse.html in place of `replace` * document `pathEscape` * Remove unnecessary pipe of img src to `html`
This commit is contained in:
parent
e66040a6f0
commit
f98f449f05
3 changed files with 15 additions and 1 deletions
|
@ -21,7 +21,7 @@
|
||||||
</svg>
|
</svg>
|
||||||
{{- else if .HasExt ".jpg" ".jpeg" ".png" ".gif" ".webp" ".tiff" ".bmp" ".heif" ".heic" ".svg"}}
|
{{- else if .HasExt ".jpg" ".jpeg" ".png" ".gif" ".webp" ".tiff" ".bmp" ".heif" ".heic" ".svg"}}
|
||||||
{{- if eq .Tpl.Layout "grid"}}
|
{{- if eq .Tpl.Layout "grid"}}
|
||||||
<img loading="lazy" src="{{.Name | replace "#" "%23" | replace "?" "%3f" | html}}">
|
<img loading="lazy" src="{{.Name | pathEscape}}">
|
||||||
{{- else}}
|
{{- else}}
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-photo" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-photo" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||||||
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
|
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
|
||||||
|
|
|
@ -300,6 +300,18 @@ func init() {
|
||||||
// find the documentation on time layouts [in Go's docs](https://pkg.go.dev/time#pkg-constants).
|
// find the documentation on time layouts [in Go's docs](https://pkg.go.dev/time#pkg-constants).
|
||||||
// The default time layout is `RFC1123Z`, i.e. `Mon, 02 Jan 2006 15:04:05 -0700`.
|
// The default time layout is `RFC1123Z`, i.e. `Mon, 02 Jan 2006 15:04:05 -0700`.
|
||||||
//
|
//
|
||||||
|
// ##### `pathEscape`
|
||||||
|
//
|
||||||
|
// Passes a string through `url.PathEscape`, replacing characters that have
|
||||||
|
// special meaning in URL path parameters (`?`, `&`, `%`).
|
||||||
|
//
|
||||||
|
// Useful e.g. to include filenames containing these characters in URL path
|
||||||
|
// parameters, or use them as an `img` element's `src` attribute.
|
||||||
|
//
|
||||||
|
// ```
|
||||||
|
// {{pathEscape "50%_valid_filename?.jpg"}}
|
||||||
|
// ```
|
||||||
|
//
|
||||||
// ```
|
// ```
|
||||||
// {{humanize "size" "2048000"}}
|
// {{humanize "size" "2048000"}}
|
||||||
// {{placeholder "http.response.header.Content-Length" | humanize "size"}}
|
// {{placeholder "http.response.header.Content-Length" | humanize "size"}}
|
||||||
|
|
|
@ -21,6 +21,7 @@ import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
@ -91,6 +92,7 @@ func (c *TemplateContext) NewTemplate(tplName string) *template.Template {
|
||||||
"httpError": c.funcHTTPError,
|
"httpError": c.funcHTTPError,
|
||||||
"humanize": c.funcHumanize,
|
"humanize": c.funcHumanize,
|
||||||
"maybe": c.funcMaybe,
|
"maybe": c.funcMaybe,
|
||||||
|
"pathEscape": url.PathEscape,
|
||||||
})
|
})
|
||||||
return c.tpl
|
return c.tpl
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue