mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-24 05:30:06 -06:00
fix: markdown links formatting (#2143)
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user