diff --git a/plugins/web/internal/render/render.go b/plugins/web/internal/render/render.go index 84e3adc..26454b7 100644 --- a/plugins/web/internal/render/render.go +++ b/plugins/web/internal/render/render.go @@ -770,10 +770,30 @@ func headHTML(page map[string]any, defaultTitle string) string { return s.String() } +func cardHref(page map[string]any, href string) string { + href = strings.TrimSpace(href) + if href == "" { + return "" + } + if strings.HasPrefix(href, "http://") || strings.HasPrefix(href, "https://") || strings.HasPrefix(href, "//") { + return href + } + if strings.HasPrefix(href, "/") { + return href + } + prefix := "/post/" + if page != nil { + if p, ok := page["link_prefix"].(string); ok { + prefix = p + } + } + return prefix + href +} + func renderCard(page map[string]any, it map[string]any) string { title := text(it["title"]) body := text(it["body"]) - href := text(it["href"]) + href := cardHref(page, text(it["href"])) meta := text(it["meta"]) tag := text(it["tag"]) tc := classAttr(fieldCSS(it, "title")) @@ -781,11 +801,7 @@ func renderCard(page map[string]any, it map[string]any) string { var card strings.Builder card.WriteString(`
`) if href != "" { - prefix := "/post/" - if p, ok := page["link_prefix"].(string); ok { - prefix = p - } - card.WriteString(fmt.Sprintf(``, esc(prefix+href))) + card.WriteString(fmt.Sprintf(``, esc(href))) } if meta != "" { card.WriteString(fmt.Sprintf(`
%s
`, esc(meta))) diff --git a/plugins/web/internal/render/resolve_params_test.go b/plugins/web/internal/render/resolve_params_test.go index 4e4bbbe..ef87bf0 100644 --- a/plugins/web/internal/render/resolve_params_test.go +++ b/plugins/web/internal/render/resolve_params_test.go @@ -103,3 +103,20 @@ func TestMarkdownToHTML(t *testing.T) { t.Fatalf("%s", out) } } + +func TestCardHrefAbsoluteAndPrefixed(t *testing.T) { + page := map[string]any{"link_prefix": "/post/"} + if got := cardHref(page, "https://example.com/a"); got != "https://example.com/a" { + t.Fatalf("absolute: %q", got) + } + if got := cardHref(page, "/about"); got != "/about" { + t.Fatalf("root: %q", got) + } + if got := cardHref(page, "hello"); got != "/post/hello" { + t.Fatalf("prefixed: %q", got) + } + html := renderCard(page, map[string]any{"title": "T", "href": "https://ex.test/x", "body": "b"}) + if !strings.Contains(html, `href="https://ex.test/x"`) { + t.Fatalf("card absolute: %s", html) + } +}