-
Notifications
You must be signed in to change notification settings - Fork 2
/
blog.go
180 lines (149 loc) · 3.61 KB
/
blog.go
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"fmt"
"html/template"
"io/ioutil"
"log"
"os"
"path/filepath"
"slices"
"strings"
"time"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"github.com/tj/front"
stripmd "github.com/writeas/go-strip-markdown"
)
const (
wordsPerMinute = 200
)
type Post struct {
SmallContainer bool
Slug string
WordCount int
ReadingTime int // in minutes
Title string `yaml:"title"`
AuthorHandles string `yaml:"authors"`
Authors []TeamMember `yaml:"ignore"`
IsDraft bool `yaml:"draft"`
Date string `yaml:"date"`
DateTime time.Time
Description string `yaml:"desc"`
DescBody template.HTML
HTMLBody template.HTML // html content
}
func (p *Post) FormatDate() string {
return p.DateTime.Format("January 2, 2006")
}
func createPage(p *Post) error {
fp := filepath.Join(buildDir, "blog", p.Slug+".html")
os.Remove(fp)
f, err := os.Create(fp)
if err != nil {
return err
}
defer f.Close()
err = postTmpl.ExecuteTemplate(f, "base", p)
if err != nil {
return err
}
fmt.Printf("📔 blog/%s.html sucessfully generated.\n", p.Slug)
return nil
}
func createBlogIndex(posts []*Post) error {
fp := filepath.Join(buildDir, "blog", "index.html")
os.Remove(fp)
f, err := os.Create(fp)
if err != nil {
return err
}
defer f.Close()
err = blogTmpl.ExecuteTemplate(f, "base", Page{
Title: "Blog",
Posts: posts,
})
if err != nil {
return err
}
fmt.Printf("📚 blog/index.html sucessfully generated.\n")
return nil
}
func newPost(fn string) (*Post, error) {
var p Post
p.SmallContainer = true
p.Slug = strings.Replace(fn, ".md", "", 1)
path := filepath.Join(blogDir, fn)
md, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
content, err := front.Unmarshal(md, &p)
if err != nil {
return nil, err
}
p.DateTime, err = time.Parse("02/01/2006", p.Date)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
// Get author by handle
authors := []TeamMember{}
handles := strings.Split(p.AuthorHandles, ",")
for _, member := range Members {
if slices.Contains(handles, member.Handle) {
authors = append(authors, member)
}
}
sortTeamMembers(authors)
p.Authors = authors
p.DescBody = template.HTML(p.Description)
// parser
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.MathJax | parser.Footnotes
parser := parser.NewWithExtensions(extensions)
// renderer
htmlFlags := html.CommonFlags | html.HrefTargetBlank | html.FootnoteNoHRTag
opts := html.RendererOptions{Flags: htmlFlags}
renderer := html.NewRenderer(opts)
p.HTMLBody = template.HTML(markdown.ToHTML(content, parser, renderer))
// Word count and reading time
plaintext := stripmd.Strip(string(content))
words := strings.Fields(plaintext)
p.WordCount = len(words)
p.ReadingTime = p.WordCount/wordsPerMinute + 1
return &p, nil
}
func genBlog() {
var posts []*Post
// Read markdown files
mdfiles, err := ioutil.ReadDir(blogDir)
if err != nil {
log.Fatal("Can't read the posts directory")
}
for _, file := range mdfiles {
fn := file.Name()
// Create post for each markdown file
p, err := newPost(fn)
if err != nil {
log.Fatal(err)
}
// Add it to the list
posts = append(posts, p)
}
subdir := filepath.Join(buildDir, "blog")
_ = os.Mkdir(subdir, os.ModePerm)
// Build each post page
for _, post := range posts {
err := createPage(post)
if err != nil {
log.Fatal(err)
}
}
// Create blog index
err = createBlogIndex(posts)
if err != nil {
log.Fatal(err)
}
}