feat(config): supports external link for images
This commit is contained in:
parent
b7829c5268
commit
b87964e81f
3 changed files with 20 additions and 3 deletions
16
data.go
16
data.go
|
@ -7,10 +7,15 @@ import (
|
||||||
"github.com/anhgelus/golatt"
|
"github.com/anhgelus/golatt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
regexExternalLink = regexp.MustCompile(`https?://`)
|
||||||
|
)
|
||||||
|
|
||||||
type ConfigData interface {
|
type ConfigData interface {
|
||||||
GetTextColor() template.CSS
|
GetTextColor() template.CSS
|
||||||
GetBackground() template.CSS
|
GetBackground() template.CSS
|
||||||
|
@ -70,12 +75,19 @@ type Link struct {
|
||||||
Content string `json:"content" toml:"content"`
|
Content string `json:"content" toml:"content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getImage(s string) string {
|
||||||
|
if regexExternalLink.MatchString(s) {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
return golatt.GetStaticPath(s)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Config) GetBackground() template.CSS {
|
func (c *Config) GetBackground() template.CSS {
|
||||||
return c.Color.GetBackground()
|
return c.Color.GetBackground()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) GetBackgroundImage() template.CSS {
|
func (c *Config) GetBackgroundImage() template.CSS {
|
||||||
return template.CSS("--background-image: url(" + golatt.GetStaticPath(c.Image) + ");")
|
return template.CSS("--background-image: url(" + getImage(c.Image) + ");")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) GetTextColor() template.CSS {
|
func (c *Config) GetTextColor() template.CSS {
|
||||||
|
@ -168,7 +180,7 @@ func (p *CustomPage) GetTextColor() template.CSS {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *CustomPage) GetBackgroundImage() template.CSS {
|
func (p *CustomPage) GetBackgroundImage() template.CSS {
|
||||||
return template.CSS("--background-image: url(" + golatt.GetStaticPath(p.Image) + ");")
|
return template.CSS("--background-image: url(" + getImage(p.Image) + ");")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *CustomPage) GetBackground() template.CSS {
|
func (p *CustomPage) GetBackground() template.CSS {
|
||||||
|
|
5
main.go
5
main.go
|
@ -7,6 +7,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
"github.com/anhgelus/golatt"
|
"github.com/anhgelus/golatt"
|
||||||
|
"html/template"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -127,6 +128,10 @@ func main() {
|
||||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g.TemplateFuncMap = template.FuncMap{
|
||||||
|
"getImage": getImage,
|
||||||
|
}
|
||||||
|
|
||||||
host := fmt.Sprintf(":%d", port)
|
host := fmt.Sprintf(":%d", port)
|
||||||
if dev {
|
if dev {
|
||||||
if port != 80 {
|
if port != 80 {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
<main style="{{ .GetBackground }}">
|
<main style="{{ .GetBackground }}">
|
||||||
<div class="presentation">
|
<div class="presentation">
|
||||||
<figure>
|
<figure>
|
||||||
<img src="{{ getStaticPath .Person.Image }}" alt="{{ .Person.Name }}'s image">
|
<img src="{{ getImage .Person.Image }}" alt="{{ .Person.Name }}'s image">
|
||||||
</figure>
|
</figure>
|
||||||
<div class="presentation__information">
|
<div class="presentation__information">
|
||||||
<h2>{{ .Person.Name }}</h2>
|
<h2>{{ .Person.Name }}</h2>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue