Allow more link names
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2020-05-13 22:36:03 +02:00
parent 7152f154ab
commit 191db91095
2 changed files with 19 additions and 8 deletions

15
main.go
View File

@ -274,7 +274,7 @@ func (h *historyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err = t.Execute(w, historyPage{ err = t.Execute(w, historyPage{
Session: sess, Session: sess,
Title: "History of " + strings.Replace(page, "_", " ", -1), Title: "History of " + cleanTitle(page),
Name: page, Name: page,
History: history, History: history,
}) })
@ -384,7 +384,7 @@ func (h *editHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
data := editPage{ data := editPage{
Session: sess, Session: sess,
Title: strings.Replace(page, "_", " ", -1), Title: cleanTitle(page),
Content: pageText, Content: pageText,
Editor: editor, Editor: editor,
Name: page, Name: page,
@ -456,7 +456,10 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
hrefRE := regexp.MustCompile(`#?\[\[\s*([\w.\- ]+)\s*\]\]`) hrefRE, err := regexp.Compile(`#?\[\[\s*([^\]]+)\s*\]\]`)
if err != nil {
log.Fatal(err)
}
pageText = hrefRE.ReplaceAllStringFunc(pageText, func(s string) string { pageText = hrefRE.ReplaceAllStringFunc(pageText, func(s string) string {
tag := false tag := false
@ -469,9 +472,9 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s = strings.TrimSuffix(s, "]]") s = strings.TrimSuffix(s, "]]")
s = strings.TrimSpace(s) s = strings.TrimSpace(s)
if tag { if tag {
return fmt.Sprintf(`<a href=%q class="tag">%s</a>`, strings.Replace(s, " ", "_", -1), s) return fmt.Sprintf(`<a href=%q class="tag">%s</a>`, cleanNameURL(s), s)
} }
return fmt.Sprintf("[%s](/%s)", s, strings.Replace(s, " ", "_", -1)) return fmt.Sprintf("[%s](/%s)", s, cleanNameURL(s))
}) })
md := markdown.New( md := markdown.New(
@ -482,7 +485,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
data := indexPage{ data := indexPage{
Session: sess, Session: sess,
Title: strings.Replace(page, "_", " ", -1), Title: cleanTitle(page),
Content: template.HTML(pageText), Content: template.HTML(pageText),
Name: page, Name: page,
Backrefs: mpPage.Refs, Backrefs: mpPage.Refs,

12
util.go
View File

@ -29,7 +29,7 @@ func RandStringBytes(n int) string {
} }
func ParseLinks(content string) ([]ParsedLink, error) { func ParseLinks(content string) ([]ParsedLink, error) {
hrefRE := regexp.MustCompile(`#?\[\[\s*([\w.\- ]+)\s*\]\]`) hrefRE := regexp.MustCompile(`#?\[\[\s*([^\]]+)\s*\]\]`)
links := hrefRE.FindAllStringSubmatch(content, -1) links := hrefRE.FindAllStringSubmatch(content, -1)
@ -46,9 +46,17 @@ func ParseLinks(content string) ([]ParsedLink, error) {
link = strings.TrimSuffix(link, "]]") link = strings.TrimSuffix(link, "]]")
link = strings.TrimSpace(link) link = strings.TrimSpace(link)
l := strings.Replace(link, " ", "_", -1) l := cleanNameURL(link)
result = append(result, ParsedLink{link, l}) result = append(result, ParsedLink{link, l})
} }
return result, nil return result, nil
} }
func cleanNameURL(name string) string {
return strings.Replace(name, " ", "_", -1)
}
func cleanTitle(name string) string {
return strings.Replace(name, "_", " ", -1)
}