Skip to content

Commit fec0584

Browse files
committed
add banner image helper and use it in the pages list
1 parent d2ff0c4 commit fec0584

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

helpers.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"slices"
88
"strings"
99
"time"
10+
11+
"github.com/yuin/goldmark/ast"
12+
gast "github.com/yuin/goldmark/ast"
1013
)
1114

1215
var helpers = template.FuncMap{
@@ -19,6 +22,7 @@ var helpers = template.FuncMap{
1922
"isFontAwesome": IsFontAwesome,
2023
"includeJS": includeJS,
2124
"scripts": scripts,
25+
"banner": Banner,
2226
}
2327

2428
var ErrHelperRegistered = errors.New("Helper already registered")
@@ -132,3 +136,27 @@ func scripts() template.HTML {
132136
func IsFontAwesome(i string) bool {
133137
return strings.HasPrefix(i, "fa")
134138
}
139+
140+
func Banner(p Page) string {
141+
_, a := p.AST()
142+
if a == nil {
143+
return ""
144+
}
145+
146+
paragraph := a.FirstChild()
147+
if paragraph == nil || paragraph.Kind() != gast.KindParagraph {
148+
return ""
149+
}
150+
151+
img := paragraph.FirstChild()
152+
if img == nil || img.Kind() != gast.KindImage {
153+
return ""
154+
}
155+
156+
image, ok := img.(*ast.Image)
157+
if !ok {
158+
return ""
159+
}
160+
161+
return string(image.Destination)
162+
}

templates/pages.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
<aside class="menu" dir="auto">
22
<div class="menu-list" dir="auto">
33
{{- range . -}}
4-
<a href="/{{.Name}}" dir="auto">
4+
5+
<a href="/{{.Name}}" dir="auto" class="is-clearfix">
6+
7+
{{ with banner . }}
8+
<img src="{{.}}" class="mr-3 is-pulled-left" style="display: block; border-radius: 0.5em; object-fit: cover; object-position: 0 50%; max-height: 3em; width: 6em;" />
9+
{{ end }}
10+
511
<span class="is-block">
612
{{ .Emoji }}
713
{{ $props := properties . }}
814
{{ with $props.title }} {{ .Value }} {{ else }} {{ .Name }} {{ end }}
915
</span>
1016
<time class="has-text-grey is-size-7" dir="auto">{{ ago .ModTime }}</time>
11-
</a>
17+
</a>
18+
1219
{{- end -}}
1320
</div>
1421
</aside>

0 commit comments

Comments
 (0)