From 3654897f60d1bf96c481d2bb8221b6bba75d2ba9 Mon Sep 17 00:00:00 2001 From: Michael Barz Date: Wed, 14 Jan 2026 16:19:32 +0100 Subject: [PATCH] fix: markdown links formatting (#2143) --- pkg/markdown/markdown.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/markdown/markdown.go b/pkg/markdown/markdown.go index 66b3dd1c8f..19a26fe19e 100644 --- a/pkg/markdown/markdown.go +++ b/pkg/markdown/markdown.go @@ -5,6 +5,7 @@ import ( "bytes" "fmt" "io" + "regexp" "strings" ) @@ -77,7 +78,7 @@ func (md MD) WriteToc(w io.Writer) (int64, error) { // main title not in toc continue } - link := fmt.Sprintf("#%s", strings.ToLower(strings.Replace(h.Header, " ", "-", -1))) + link := fmt.Sprintf("#%s", toAnchor(h.Header)) s := fmt.Sprintf("%s* [%s](%s)\n", strings.Repeat(" ", h.Level-2), h.Header, link) n, err := w.Write([]byte(s)) if err != nil { @@ -137,3 +138,12 @@ func headingFromString(s string) Heading { Header: strings.TrimPrefix(con, " "), } } + +func toAnchor(header string) string { + // Remove everything except letters, numbers, and spaces + reg := regexp.MustCompile(`[^a-zA-Z0-9 ]+`) + anchor := reg.ReplaceAllString(header, "") + // Replace spaces with hyphens and convert to lowercase + anchor = strings.ReplaceAll(anchor, " ", "-") + return strings.ToLower(anchor) +}