Render markdown and links in backrefs
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
0a187a49c3
commit
4840eca7eb
10
backref.go
10
backref.go
|
@ -2,9 +2,12 @@ package main
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitlab.com/golang-commonmark/markdown"
|
||||
)
|
||||
|
||||
type Reference struct {
|
||||
|
@ -82,6 +85,11 @@ func loadBackrefs(fp *FilePages, p string) (map[string][]Backref, error) {
|
|||
|
||||
result := make(map[string][]Backref)
|
||||
|
||||
md := markdown.New(
|
||||
markdown.HTML(true),
|
||||
markdown.XHTMLOutput(true),
|
||||
)
|
||||
|
||||
for _, ref := range refs[p] {
|
||||
title := strings.Replace(ref.Name, "_", " ", -1)
|
||||
if _, e := result[ref.Name]; !e {
|
||||
|
@ -91,7 +99,7 @@ func loadBackrefs(fp *FilePages, p string) (map[string][]Backref, error) {
|
|||
result[ref.Name] = append(result[ref.Name], Backref{
|
||||
Name: ref.Name,
|
||||
Title: title,
|
||||
Line: strings.TrimLeft(ref.Link.Line, " *"),
|
||||
Line: template.HTML(md.RenderToString([]byte(renderLinks(strings.TrimLeft(ref.Link.Line, " *"))))),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
50
main.go
50
main.go
|
@ -33,7 +33,7 @@ var (
|
|||
type Backref struct {
|
||||
Name string
|
||||
Title string
|
||||
Line string
|
||||
Line template.HTML
|
||||
}
|
||||
|
||||
// Page
|
||||
|
@ -480,26 +480,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
hrefRE, err := regexp.Compile(`#?\[\[\s*([^\]]+)\s*\]\]`)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
pageText = hrefRE.ReplaceAllStringFunc(pageText, func(s string) string {
|
||||
tag := false
|
||||
if s[0] == '#' {
|
||||
s = strings.TrimPrefix(s, "#[[")
|
||||
tag = true
|
||||
} else {
|
||||
s = strings.TrimPrefix(s, "[[")
|
||||
}
|
||||
s = strings.TrimSuffix(s, "]]")
|
||||
s = strings.TrimSpace(s)
|
||||
if tag {
|
||||
return fmt.Sprintf(`<a href=%q class="tag">%s</a>`, cleanNameURL(s), s)
|
||||
}
|
||||
return fmt.Sprintf("[%s](/%s)", s, cleanNameURL(s))
|
||||
})
|
||||
pageText = renderLinks(pageText)
|
||||
|
||||
md := markdown.New(
|
||||
markdown.HTML(true),
|
||||
|
@ -524,12 +505,37 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
err = t.Execute(w, data)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
log.Println(err)
|
||||
// http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func renderLinks(pageText string) string {
|
||||
hrefRE, err := regexp.Compile(`#?\[\[\s*([^\]]+)\s*\]\]`)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
pageText = hrefRE.ReplaceAllStringFunc(pageText, func(s string) string {
|
||||
tag := false
|
||||
if s[0] == '#' {
|
||||
s = strings.TrimPrefix(s, "#[[")
|
||||
tag = true
|
||||
} else {
|
||||
s = strings.TrimPrefix(s, "[[")
|
||||
}
|
||||
s = strings.TrimSuffix(s, "]]")
|
||||
s = strings.TrimSpace(s)
|
||||
if tag {
|
||||
return fmt.Sprintf(`<a href=%q class="tag">%s</a>`, cleanNameURL(s), s)
|
||||
}
|
||||
return fmt.Sprintf("[%s](/%s)", s, cleanNameURL(s))
|
||||
})
|
||||
return pageText
|
||||
}
|
||||
|
||||
func (h *recentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user