From fa5ac4040e8e2fc57bef5c598e1ceda4c2fc0a73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Sun, 5 Oct 2025 15:16:20 +0200 Subject: feat(backend): add noreferer on external link and move target blank --- README.md | 2 +- backend/config.go | 6 ++++++ backend/templates/base.html | 6 ++---- frontend/index.ts | 6 ++---- markdown/ast_external.go | 12 +++++++++++- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 1b68cb4..f6b7a82 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Backend written in modern Go. Light CSS, light JS, runs everywhere. SSR first. -Maybe HTMX? +Optional HTMX to fluidify the navigation. Content written in markdown. diff --git a/backend/config.go b/backend/config.go index 8c111ae..d6cf25a 100644 --- a/backend/config.go +++ b/backend/config.go @@ -1,9 +1,11 @@ package backend import ( + "html/template" "log/slog" "os" + "git.anhgelus.world/anhgelus/small-world/markdown" "github.com/pelletier/go-toml/v2" ) @@ -12,6 +14,10 @@ type Link struct { URL string `toml:"url"` } +func (l *Link) Render() template.HTML { + return markdown.RenderLink(l.Name, l.URL) +} + type Logo struct { Header string `toml:"header"` Favicon string `toml:"favicon"` diff --git a/backend/templates/base.html b/backend/templates/base.html index cf0d834..3af37f9 100644 --- a/backend/templates/base.html +++ b/backend/templates/base.html @@ -27,15 +27,13 @@
Logo - +
{{ template "body" . }} {{ $script := asset "index.js" }} diff --git a/frontend/index.ts b/frontend/index.ts index 9babe4a..d4fbe2a 100644 --- a/frontend/index.ts +++ b/frontend/index.ts @@ -4,10 +4,8 @@ htmx.config.historyRestoreAsHxRequest = false; function setupAnchors() { document.querySelectorAll("a").forEach(e => { - if (!e.href.startsWith(window.location.origin) && /https?:\/\//.test(e.href)) { - e.target = "_blank"; - return - } + // stuff related to external links are already handled in the backend + if (!e.href.startsWith(window.location.origin) && /https?:\/\//.test(e.href)) return if (e.hasAttribute("hx-trigger")) return; e.setAttribute("hx-get", e.href) e.setAttribute("hx-trigger", "click") diff --git a/markdown/ast_external.go b/markdown/ast_external.go index f37a685..e648ad5 100644 --- a/markdown/ast_external.go +++ b/markdown/ast_external.go @@ -3,8 +3,11 @@ package markdown import ( "fmt" "html/template" + "regexp" ) +var externalLink = regexp.MustCompile(`https?://`) + type astLink struct { content block href block @@ -19,7 +22,14 @@ func (a *astLink) Eval(opt *Option) (template.HTML, *ParseError) { if err != nil { return "", err } - return template.HTML(fmt.Sprintf(`%s`, href, content)), nil + return RenderLink(string(content), string(href)), nil +} + +func RenderLink(content, href string) template.HTML { + if !externalLink.Match([]byte(href)) { + return template.HTML(fmt.Sprintf(`%s`, href, content)) + } + return template.HTML(fmt.Sprintf(`%s`, href, content)) } type astImage struct { -- cgit v1.2.3