diff options
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/config.go | 5 | ||||
| -rw-r--r-- | backend/data.go | 6 | ||||
| -rw-r--r-- | backend/home.go | 1 | ||||
| -rw-r--r-- | backend/parser.go | 22 | ||||
| -rw-r--r-- | backend/router.go | 11 | ||||
| -rw-r--r-- | backend/section.go | 1 | ||||
| -rw-r--r-- | backend/templates/base.html | 7 |
7 files changed, 28 insertions, 25 deletions
diff --git a/backend/config.go b/backend/config.go index cd24592..44b2033 100644 --- a/backend/config.go +++ b/backend/config.go @@ -5,7 +5,6 @@ import ( "log/slog" "os" - "git.anhgelus.world/anhgelus/small-web/markdown" "github.com/pelletier/go-toml/v2" ) @@ -14,8 +13,8 @@ type Link struct { URL string `toml:"url"` } -func (l *Link) Render() template.HTML { - return markdown.RenderLink(l.Name, l.URL) +func (l *Link) Render(url string) template.HTML { + return renderLink(l.Name, l.URL, url) } type Logo struct { diff --git a/backend/data.go b/backend/data.go index 1b40e73..5bc5391 100644 --- a/backend/data.go +++ b/backend/data.go @@ -11,7 +11,6 @@ import ( "log/slog" "math/rand" "net/http" - "net/url" "regexp" "strings" txt "text/template" @@ -103,11 +102,6 @@ func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string panic(err) } exec := "base.html" - if r.Context().Value(isUpdateKey).(bool) { - exec = "body" - w.Header().Set("Updated-Title", url.QueryEscape(d.Title())) - w.Header().Set("Updated-Quote", url.QueryEscape(d.Quote)) - } if custom == nil { err = t.ExecuteTemplate(w, exec, d) } else { diff --git a/backend/home.go b/backend/home.go index be01b9d..f824a69 100644 --- a/backend/home.go +++ b/backend/home.go @@ -83,6 +83,7 @@ func handleGenericRoot(w http.ResponseWriter, r *http.Request, name string) { } panic(err) } + d.URL = "/" + name d.Content, ok = parse(b, new(EntryInfo), d.data) if !ok { w.WriteHeader(http.StatusInternalServerError) diff --git a/backend/parser.go b/backend/parser.go index c7d6445..b8e730b 100644 --- a/backend/parser.go +++ b/backend/parser.go @@ -18,6 +18,23 @@ type EntryInfo struct { PubLocalDate toml.LocalDate `toml:"publication_date"` } +func renderLinkFunc(url string) func(string, string) template.HTML { + return func(content, href string) template.HTML { + b := "<a" + if href == url || (href != "/" && url != "/" && strings.HasPrefix(url, href)) { + b += ` class="target"` + } + if markdown.ExternalLink.MatchString(href) { + b += ` target="_blank"` + } + return template.HTML(fmt.Sprintf(`%s href="%s">%s</a>`, b, href, content)) + } +} + +func renderLink(content, href, url string) template.HTML { + return renderLinkFunc(url)(content, href) +} + func parse(b []byte, info *EntryInfo, d *data) (template.HTML, bool) { var dd string splits := strings.SplitN(string(b), "---", 2) @@ -31,7 +48,10 @@ func parse(b []byte, info *EntryInfo, d *data) (template.HTML, bool) { } else { dd = string(b) } - content, err := markdown.Parse(dd, &markdown.Option{ImageSource: getStatic}) + opt := new(markdown.Option) + opt.ImageSource = getStatic + opt.RenderLink = renderLinkFunc(d.URL) + content, err := markdown.Parse(dd, opt) var errMd *markdown.ParseError errors.As(err, &errMd) if errMd != nil { diff --git a/backend/router.go b/backend/router.go index 3e6a39a..3eede92 100644 --- a/backend/router.go +++ b/backend/router.go @@ -19,7 +19,6 @@ import ( const ( Version = "0.4.0" configKey = "config" - isUpdateKey = "is_update" assetsFSKey = "assets_fs" debugKey = "debug" ) @@ -95,16 +94,6 @@ func NewRouter(debug bool, cfg *Config, assets fs.FS) *chi.Mux { next.ServeHTTP(w, r.WithContext(ctx)) }) }) - r.Use(func(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - val := false - if r.Header.Get("HX-Request") == "true" { - val = true - } - ctx := context.WithValue(r.Context(), isUpdateKey, val) - next.ServeHTTP(w, r.WithContext(ctx)) - }) - }) return r } diff --git a/backend/section.go b/backend/section.go index e6e318a..ff2cf43 100644 --- a/backend/section.go +++ b/backend/section.go @@ -216,6 +216,7 @@ func (s *Section) parse(d *sectionData, mu *sync.Mutex, path, slug string) bool panic(err) } var ok bool + d.data.URL = fmt.Sprintf("/%s/%s", s.URI, slug) d.Content, ok = parse(b, &d.EntryInfo, d.data) if !ok { return false diff --git a/backend/templates/base.html b/backend/templates/base.html index 0089587..20abcd0 100644 --- a/backend/templates/base.html +++ b/backend/templates/base.html @@ -28,10 +28,11 @@ <meta name="twitter:description" content="{{ .PageDescription }}" /> <meta name="twitter:image" content="{{ fullStatic .Image }}" /> </head> - <body hx-push-url="true"> + <body> <header> <img src="{{ static .Logo.Header }}" alt="Logo" /> - <nav>{{ range .Links }}{{ .Render }}{{end}}</nav> + {{ $url := .URL }} + <nav>{{ range .Links }}{{ .Render $url }}{{end}}</nav> </header> {{ template "body" . }} <footer> @@ -42,7 +43,5 @@ <a href="https://git.anhgelus.world/anhgelus/small-web" target="_blank" rel="noreferrer">code source</a>. </p> </footer> - {{ $script := asset "index.js" }} - <script src="{{ $script.Src }}" integrity="{{ $script.Checksum }}" defer></script> </body> </html> |
