aboutsummaryrefslogtreecommitdiff
path: root/backend/parser.go
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2025-12-12 19:21:22 +0100
committerAnhgelus Morhtuuzh <william@herges.fr>2025-12-12 19:21:22 +0100
commita815c291074b454d4bb1a31067cd363c84df1360 (patch)
treeb04e3e048fdd15caff2c9127d7598b04b47178a7 /backend/parser.go
parentd56090d43c925dbbd22a3e0c3f6d541674a09697 (diff)
refactor(frontend): remove JS files
Diffstat (limited to 'backend/parser.go')
-rw-r--r--backend/parser.go22
1 files changed, 21 insertions, 1 deletions
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 {