-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.go
208 lines (179 loc) · 5.02 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"encoding/xml"
"fmt"
"io"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"unicode"
)
type BlogPost struct {
Filename string
Content string
Metadata Metadata
mdParser MarkdownParser
}
type Metadata struct {
Title string `xml:"title"` // Not used
Slug string `xml:"slug"` // Used when calculating redirects for legacy posts
Summary string `xml:"summary"` // Not used
CreatedOn string `xml:"createdOn"` // Used when file name does not have a date
UpdatedOn string `xml:"updatedOn"` // Not used
PostedOn string `xml:"postedOn"` // Not used
OldSequence string `xml:"oldSequence"` // Used when calculating redirects for legacy posts
Fields []Field `xml:"fields"` // Used when calculating redirects for legacy posts
}
type Field struct {
Name string `xml:"name"`
Value string `xml:"value"`
}
func LoadBlogPost(filename string) BlogPost {
content := readFile(filename)
blog := BlogPost{Filename: filename, Content: content}
blog.Metadata = blog.fetchMetadata()
blog.mdParser = NewMarkdownParser(content)
return blog
}
func (b BlogPost) DateCreated() string {
if len(b.Metadata.CreatedOn) >= 10 {
// Use the date part (YYYY-MM-DD) from the metadata
return b.Metadata.CreatedOn[0:10]
}
date := dateFromFilename(b.Filename)
if date != "" {
// Use the date part from the filename
return date
}
return "1970-01-01"
}
func (b BlogPost) DatePosted() string {
if len(b.Metadata.PostedOn) >= 10 {
// Use the date part (YYYY-MM-DD) from the metadata
return b.Metadata.PostedOn[0:10]
}
date := dateFromFilename(b.Filename)
if date != "" {
// Use the date part from the filename
return date
}
return "1970-01-01"
}
func (b BlogPost) YearCreated() int {
yearString := b.DateCreated()[0:4]
year, _ := strconv.Atoi(yearString)
return year
}
func (b BlogPost) YearPosted() int {
yearString := b.DatePosted()[0:4]
year, _ := strconv.Atoi(yearString)
return year
}
func (b BlogPost) Summary() string {
return b.mdParser.Description()
}
func (b BlogPost) DefaultTitle() string {
return strings.TrimSuffix(filepath.Base(b.Filename), ".md")
}
func (b BlogPost) Title() string {
if b.mdParser.Title() == "" {
return b.DefaultTitle()
}
return b.mdParser.Title()
}
func (b BlogPost) LinkUrl() string {
return "/" + strings.TrimSuffix(b.Filename, ".md")
}
func (b BlogPost) LinkMarkdown() string {
return fmt.Sprintf("[%s](%s)", b.Title(), b.LinkUrl())
}
func (b BlogPost) MetadataFile() string {
return strings.TrimSuffix(b.Filename, ".md") + ".xml"
}
func (b BlogPost) ToSearchEntry() string {
entry := fmt.Sprintf(`{
"id": "%s",
"name": "%s",
"text": "%s"
}`, b.LinkUrl(), b.Title(), b.SearchText())
return entry
}
func (b BlogPost) SearchText() string {
// Extract all alphanumeric words and a few selected special characters
plainText := ""
var prevChar rune
for _, c := range strings.ToLower(b.Content) {
if unicode.IsLetter(c) || unicode.IsDigit(c) {
plainText += string(c)
} else if c == '.' || c == '/' || c == '@' || c == ':' || c == '-' {
plainText += string(c)
} else if c == '#' && (prevChar == 'C' || prevChar == 'c') {
plainText += string(c)
} else {
plainText += " "
}
prevChar = c
}
// Remove duplicates
tokens := []string{}
for _, token := range strings.Split(plainText, " ") {
token = strings.TrimSpace(token)
if token == "" {
continue
}
if !slices.Contains(tokens, token) {
tokens = append(tokens, token)
}
}
return strings.Join(tokens, " ")
}
func (b BlogPost) fetchMetadata() Metadata {
reader, err := os.Open(b.MetadataFile())
if err != nil {
return Metadata{}
}
defer reader.Close()
byteValue, _ := io.ReadAll(reader)
var metadata Metadata
xml.Unmarshal(byteValue, &metadata)
return metadata
}
func (b BlogPost) OldId() int {
for _, field := range b.Metadata.Fields {
if field.Name == "oldId" {
oldId, _ := strconv.Atoi(field.Value)
return oldId
}
}
return 0
}
// Creates the redirect files required to support legacy URLs indicated in the metadata file
// Redirect legacy URLs
//
// ./blog/slug/index.html
// ./blog/slug/10 (old id)
// ./blog/slug/2008-11-25-00001 (date created + sequence)
//
// to new format
//
// ./blog/2008-11-30/slug (date posted)
func (b BlogPost) createRedirectFiles() bool {
newUrl := fmt.Sprintf("/blog/%s/%s", b.DatePosted(), b.Metadata.Slug)
content := `<head><meta http-equiv="Refresh" content="0; URL=URL-GOES-HERE" /></head>`
content = strings.Replace(content, "URL-GOES-HERE", newUrl, 1)
redirectFile1 := fmt.Sprintf("./blog/%s/index.html", b.Metadata.Slug)
redirectFolder := fmt.Sprintf("./blog/%s", b.Metadata.Slug)
createDir(redirectFolder)
saveFile(redirectFile1, content)
if b.OldId() != 0 {
redirectFile2 := fmt.Sprintf("./blog/%s/%d.html", b.Metadata.Slug, b.OldId())
saveFile(redirectFile2, content)
}
if b.Metadata.OldSequence != "" {
redirectFile3 := fmt.Sprintf("./blog/%s/%s-%s.html", b.Metadata.Slug, b.DateCreated(), b.Metadata.OldSequence)
saveFile(redirectFile3, content)
}
return true
}