Skip to content

Commit 8738697

Browse files
committed
allow frontmatter to be added to properties and use title in the layout
1 parent a52a616 commit 8738697

File tree

7 files changed

+73
-4
lines changed

7 files changed

+73
-4
lines changed

extensions/all/all.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
_ "github.com/emad-elsaid/xlog/extensions/editor"
1212
_ "github.com/emad-elsaid/xlog/extensions/embed"
1313
_ "github.com/emad-elsaid/xlog/extensions/file_operations"
14+
_ "github.com/emad-elsaid/xlog/extensions/frontmatter"
1415
_ "github.com/emad-elsaid/xlog/extensions/github"
1516
_ "github.com/emad-elsaid/xlog/extensions/gpg"
1617
_ "github.com/emad-elsaid/xlog/extensions/hashtags"
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package frontmatter
2+
3+
import (
4+
"github.com/emad-elsaid/xlog"
5+
meta "github.com/yuin/goldmark-meta"
6+
)
7+
8+
func init() {
9+
xlog.RegisterExtension(Frontmatter{})
10+
}
11+
12+
type Frontmatter struct{}
13+
14+
func (Frontmatter) Name() string { return "frontmatter" }
15+
func (Frontmatter) Init() {
16+
m := meta.New(
17+
meta.WithStoresInDocument(),
18+
)
19+
20+
m.Extend(xlog.MarkdownConverter())
21+
xlog.RegisterProperty(MetaProperties)
22+
}
23+
24+
type MetaProperty struct {
25+
NameVal string
26+
Val any
27+
}
28+
29+
func (m MetaProperty) Name() string { return m.NameVal }
30+
func (m MetaProperty) Icon() string { return "fa-solid fa-table-list" }
31+
func (m MetaProperty) Value() any { return m.Val }
32+
33+
func MetaProperties(p xlog.Page) []xlog.Property {
34+
_, ast := p.AST()
35+
if ast == nil {
36+
return nil
37+
}
38+
39+
metaData := ast.OwnerDocument().Meta()
40+
if len(metaData) == 0 {
41+
return nil
42+
}
43+
44+
ps := make([]xlog.Property, 0, len(metaData))
45+
for k, v := range metaData {
46+
ps = append(ps, MetaProperty{
47+
NameVal: k,
48+
Val: v,
49+
})
50+
}
51+
52+
return ps
53+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/hashicorp/golang-lru/v2 v2.0.7
2222
github.com/rjeczalik/notify v0.9.3
2323
github.com/stretchr/testify v1.10.0
24+
github.com/yuin/goldmark-meta v1.1.0
2425
gitlab.com/greyxor/slogor v1.5.2
2526
golang.org/x/sync v0.6.0
2627
gopkg.in/yaml.v3 v3.0.1
@@ -30,6 +31,7 @@ require (
3031
github.com/davecgh/go-spew v1.1.1 // indirect
3132
github.com/pmezard/go-difflib v1.0.0 // indirect
3233
golang.org/x/net v0.23.0 // indirect
34+
gopkg.in/yaml.v2 v2.3.0 // indirect
3335
)
3436

3537
require (

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ github.com/yuin/goldmark-emoji v1.0.2 h1:c/RgTShNgHTtc6xdz2KKI74jJr6rWi7FPgnP9GA
6666
github.com/yuin/goldmark-emoji v1.0.2/go.mod h1:RhP/RWpexdp+KHs7ghKnifRoIs/Bq4nDS7tRbCkOwKY=
6767
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc h1:+IAOyRda+RLrxa1WC7umKOZRsGq4QrFFMYApOeHzQwQ=
6868
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc/go.mod h1:ovIvrum6DQJA4QsJSovrkC4saKHQVs7TvcaeO8AIl5I=
69+
github.com/yuin/goldmark-meta v1.1.0 h1:pWw+JLHGZe8Rk0EGsMVssiNb/AaPMHfSRszZeUeiOUc=
70+
github.com/yuin/goldmark-meta v1.1.0/go.mod h1:U4spWENafuA7Zyg+Lj5RqK/MF+ovMYtBvXi1lBb2VP0=
6971
gitlab.com/greyxor/slogor v1.5.2 h1:ushBskVMbduRt3mWLpIct9mOGAkxnytVNoSn2IHBgXM=
7072
gitlab.com/greyxor/slogor v1.5.2/go.mod h1:P8dDYZxKsh/omu91z+vep/m78GxWEOc2KP2f0zaCasE=
7173
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -131,6 +133,8 @@ google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGm
131133
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
132134
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
133135
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
136+
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
137+
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
134138
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
135139
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
136140
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

property.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func Properties(p Page) map[string]Property {
3636
type lastUpdateProp struct{ page Page }
3737

3838
func (a lastUpdateProp) Icon() string { return "fa-solid fa-clock" }
39-
func (a lastUpdateProp) Name() string { return "Modified" }
39+
func (a lastUpdateProp) Name() string { return "modified" }
4040
func (a lastUpdateProp) Value() any { return ago(a.page.ModTime()) }
4141

4242
func defaultProps(p Page) []Property {

templates/layout.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
<link rel="icon" href="/public/logo.png">
88
<link rel="stylesheet" href="/public/style.css">
99
<title>
10-
{{- with .page }}{{ .Emoji }} {{ .Name }} | {{ end -}}
10+
{{ $props := properties .page }}
11+
{{- with .page -}}
12+
{{ .Emoji }}
13+
{{ with $props.title }} {{ .Value }} {{ else }} {{ .Name }} {{ end }}
14+
|
15+
{{- end -}}
1116
{{.config.Sitename}}
1217
</title>
1318
{{- with .page }} {{ widgets "head" . }} {{ end }}
@@ -21,7 +26,8 @@
2126
{{- if ne .config.Index .page.Name -}}
2227
<div dir="auto" class="title is-1 has-text-weight-bold">
2328
{{- with .page -}}
24-
{{- .Emoji }} {{ .Name -}}
29+
{{- .Emoji }}
30+
{{ with $props.title }} {{ .Value }} {{ else }} {{ .Name }} {{ end }}
2531
{{- end -}}
2632
</div>
2733
{{- end -}}

templates/pages.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<ul>
22
{{- range . -}}
33
<li>
4-
<a href="/{{.Name}}" dir="auto">{{ .Emoji }} {{ .Name }}</a>
4+
<a href="/{{.Name}}" dir="auto">{{ .Emoji }}
5+
{{ $props := properties . }}
6+
{{ with $props.title }} {{ .Value }} {{ else }} {{ .Name }} {{ end }}
7+
</a>
58
<time class="has-text-grey is-size-7">{{ ago .ModTime }}</time>
69
</li>
710
{{- end -}}

0 commit comments

Comments
 (0)