improve backrefs for view and edit
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2020-05-08 17:21:39 +02:00
parent ef5e074cb0
commit 6c04d9871f
5 changed files with 67 additions and 22 deletions

View File

@ -41,7 +41,7 @@ func processBackrefs(fp *FilePages) error {
return err
}
func loadBackrefs(fp *FilePages, p string) ([]string, error) {
func loadBackrefs(fp *FilePages, p string) ([]Backref, error) {
refs := make(Refs)
p = strings.Replace(p, " ", "_", -1)
@ -57,5 +57,15 @@ func loadBackrefs(fp *FilePages, p string) ([]string, error) {
return nil, err
}
return refs[p], nil
var result []Backref
for _, ref := range refs[p] {
title := strings.Replace(ref, "_", " ", -1)
result = append(result, Backref{
Name: ref,
Title: title,
})
}
return result, nil
}

26
file.go
View File

@ -29,18 +29,34 @@ func NewFilePages(dirname string) PagesRepository {
func (fp *FilePages) Get(title string) Page {
name := strings.Replace(title, " ", "_", -1)
refs, err := loadBackrefs(fp, name)
if err != nil {
return Page{
Title: title,
Name: name,
Content: "",
Refs: nil,
}
}
f, err := os.Open(filepath.Join(fp.dirname, name))
if err != nil {
return Page{}
return Page{
Title: title,
Name: name,
Content: "",
Refs: refs,
}
}
defer f.Close()
body, err := ioutil.ReadAll(f)
if err != nil {
return Page{}
return Page{
Title: title,
Name: name,
Content: "",
Refs: refs,
}
refs, err := loadBackrefs(fp, name)
if err != nil {
return Page{}
}
return Page{
Name: name,

14
main.go
View File

@ -27,6 +27,11 @@ const (
RedirectURI = "https://wiki.p83.nl/auth/callback"
)
type Backref struct {
Name string
Title string
}
// Page
type Page struct {
// Name is the filename of the page
@ -37,7 +42,7 @@ type Page struct {
Content string
Refs []string
Refs []Backref
}
type DiffPage struct {
@ -75,7 +80,7 @@ type indexPage struct {
Title string
Name string
Content template.HTML
Backrefs []string
Backrefs []Backref
}
type editPage struct {
@ -84,6 +89,7 @@ type editPage struct {
Content string
Name string
Editor template.HTML
Backrefs []Backref
}
type historyPage struct {
@ -339,7 +345,8 @@ func (h *editHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
page = "Home"
}
pageText := mp.Get(page).Content
mpPage := mp.Get(page)
pageText := mpPage.Content
jsonEditor := pageText != "" && pageText[0] == '{'
@ -371,6 +378,7 @@ func (h *editHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Content: pageText,
Editor: editor,
Name: page,
Backrefs: mpPage.Refs,
}
t, err := template.ParseFiles("templates/layout.html", "templates/edit.html")

View File

@ -8,6 +8,17 @@
<input type="hidden" name="p" value="{{ .Name }}" />
{{ .Editor }}
</form>
{{ if .Backrefs }}
<div class="backrefs">
<h3>Backrefs</h3>
<ul>
{{ range $ref := .Backrefs }}
<li><a href="/{{ $ref.Name }}">{{ $ref.Title }}</a></li>
{{ end }}
</ul>
</div>
{{ end }}
{{ end }}
{{ define "content_head" }}

View File

@ -8,7 +8,7 @@
<h3>Backrefs</h3>
<ul>
{{ range $ref := .Backrefs }}
<li><a href="/{{ $ref }}">{{ $ref }}</a></li>
<li><a href="/{{ $ref.Name }}">{{ $ref.Title }}</a></li>
{{ end }}
</ul>
</div>