1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
package content
import (
"bytes"
"os"
"path/filepath"
"slices"
"strings"
"time"
"website/internal/log"
"github.com/adrg/frontmatter"
mapset "github.com/deckarep/golang-set/v2"
"github.com/pkg/errors"
fences "github.com/stefanfritsch/goldmark-fences"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
htmlrenderer "github.com/yuin/goldmark/renderer/html"
)
type PostMatter struct {
Date time.Time `toml:"date"`
Description string `toml:"description"`
Title string `toml:"title"`
Taxonomies struct {
Tags []string `toml:"tags"`
} `toml:"taxonomies"`
}
type PostVCSDates struct {
Created time.Time
LastUpdated time.Time
}
type Post struct {
Input string
Output string
Basename string
URL string
Content string
PostMatter
}
type Tags mapset.Set[string]
const DatesFilename = "dates.json"
var markdown = goldmark.New(
goldmark.WithRendererOptions(
htmlrenderer.WithUnsafe(),
),
goldmark.WithExtensions(
extension.GFM,
extension.Footnote,
extension.Typographer,
&fences.Extender{},
),
)
func GetPost(filename string) (*PostMatter, []byte, error) {
matter := PostMatter{}
content, err := os.Open(filename)
if err != nil {
return nil, nil, errors.WithMessagef(err, "could not open post %s", filename)
}
defer content.Close()
rest, err := frontmatter.MustParse(content, &matter)
if err != nil {
return nil, nil, errors.WithMessagef(
err,
"could not parse front matter of post %s",
filename,
)
}
return &matter, rest, nil
}
func RenderMarkdown(content []byte) (string, error) {
var buf bytes.Buffer
if err := markdown.Convert(content, &buf); err != nil {
return "", errors.WithMessage(err, "could not convert markdown content")
}
return buf.String(), nil
}
type Config struct {
Root string
InputDir string
OutputDir string
}
func GetPostFiles(root string) ([]string, error) {
return filepath.Glob(filepath.Join(root, "*.md"))
}
func ReadPosts(config *Config) ([]Post, Tags, error) {
tags := mapset.NewSet[string]()
posts := []Post{}
subdir := filepath.Join(config.Root, config.InputDir)
files, err := GetPostFiles(subdir)
if err != nil {
return nil, nil, errors.WithMessagef(err, "could not read post directory %s", subdir)
}
outputReplacer := strings.NewReplacer(config.Root, config.OutputDir, ".md", "/index.html")
urlReplacer := strings.NewReplacer(config.Root, "", ".md", "/")
for _, pathFromRoot := range files {
output := outputReplacer.Replace(pathFromRoot)
url := urlReplacer.Replace(pathFromRoot)
log.Debug("reading post", "post", pathFromRoot)
matter, content, err := GetPost(pathFromRoot)
if err != nil {
return nil, nil, err
}
// iter, err := repo.FileLog(pathFromRoot)
// if err != nil {
// return nil, nil, err
// }
// dates := getPostVCSDates(iter)
// log.Debug("post dates", "created", dates.Created, "updated", dates.LastUpdated)
for _, tag := range matter.Taxonomies.Tags {
tags.Add(strings.ToLower(tag))
}
log.Debug("rendering markdown in post", "post", pathFromRoot)
html, err := RenderMarkdown(content)
if err != nil {
return nil, nil, err
}
post := Post{
Input: pathFromRoot,
Output: output,
Basename: filepath.Base(url),
URL: url,
PostMatter: *matter,
Content: html,
}
posts = append(posts, post)
}
slices.SortFunc(posts, func(a, b Post) int {
return b.Date.Compare(a.Date)
})
return posts, tags, nil
}
|