/
article.go
277 lines (245 loc) · 6.61 KB
/
article.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package model
import (
"bufio"
"bytes"
"html/template"
"io/fs"
"log"
"os"
"path"
"path/filepath"
"strings"
"sync"
"text/scanner"
"time"
"github.com/hulizhen/blogo/config"
"github.com/hulizhen/blogo/internal/markdown"
"github.com/hulizhen/blogo/internal/xtime"
"github.com/hulizhen/blogo/service"
"github.com/jmoiron/sqlx"
"github.com/pelletier/go-toml"
)
type articleMetadata struct {
Title string `toml:"title"`
Categories []string `toml:"categories"`
Tags []string `toml:"tags"`
Pinned bool `toml:"pinned"`
Draft bool `toml:"drfat"`
Date time.Time `toml:"date"`
}
type Article struct {
Slug string `db:"slug"`
Title string `db:"title"`
Content template.HTML `db:"content"`
Preview template.HTML `db:"preview"`
Categories string `db:"categories"`
Tags string `db:"tags"`
Pinned bool `db:"pinned"`
Draft bool `db:"draft"`
PublishedAt time.Time `db:"published_at"`
}
const (
metadataDelimiter = "+++\n"
previewDelimiter = "^^^\n"
categoryDelimiter = "/"
tagDelimiter = ","
)
// NewArticle creates a Article instance with provided repo path `base`, article `path` and article `entry`.
func NewArticle(path string, entry fs.DirEntry) (article *Article, err error) {
// Get URL slug by stripping extension of the file basename.
basename := filepath.Base(path)
slug := strings.TrimSuffix(basename, filepath.Ext(basename))
// Parse article to extract metadata and content.
metadata, content, err := parseArticle(path)
if err != nil {
return
}
// Extract preview from content.
preview := ""
count := 3
strs := strings.SplitN(content, previewDelimiter, count)
if len(strs) == count {
preview = strings.TrimSpace(strs[1])
content = strs[0] + strs[1] + strs[2]
}
// Parse the content and preview with markdown parser.
var buf bytes.Buffer
if err = markdown.SharedMarkdown().Convert([]byte(content), &buf); err != nil {
return
}
content = buf.String()
buf.Reset()
if err = markdown.SharedMarkdown().Convert([]byte(preview), &buf); err != nil {
return
}
preview = buf.String()
// Parse metadata and fill article.
article = &Article{
Slug: slug,
Content: template.HTML(content),
Preview: template.HTML(preview),
}
am := &articleMetadata{}
err = toml.Unmarshal([]byte(metadata), am)
if err != nil {
return
}
article.updateMetadata(am)
return
}
func (a *Article) ShortPublicationDate() string {
return xtime.ShortFormat(a.PublishedAt)
}
func (a *Article) Href() string {
return filepath.Join("/articles", a.Slug)
}
// updateMetadata updates the article model with the extracted metadata.
func (a *Article) updateMetadata(am *articleMetadata) {
a.Title = am.Title
a.Categories = strings.Join(am.Categories, categoryDelimiter)
a.Tags = strings.Join(am.Tags, tagDelimiter)
a.Pinned = am.Pinned
a.Draft = am.Draft
a.PublishedAt = am.Date
}
func isWhitespace(c byte) bool {
return scanner.GoWhitespace&(1<<c) != 0
}
// parseArticle parses the *.md article file and extracts the metadata and content.
func parseArticle(path string) (metadata string, content string, err error) {
f, err := os.Open(path)
if err != nil {
return
}
defer func() {
err = f.Close()
}()
hasMetadata := false
removed := false
articleScanner := bufio.NewScanner(f)
articleScanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return
}
if len(metadata) > 0 {
advance = len(data)
token = data
return
}
// Remove the whitespaces at the file beginning if they exist.
if !removed {
removed = true
i := 0
for i < len(data) && isWhitespace(data[i]) {
i++
}
if i > 0 {
return i, nil, nil
}
}
// Extract metadata with delimiter.
// The metadata surrounded by delimiter should be at the beginning of article, with some whitespaces, if any.
count := 3
strs := strings.SplitN(string(data), metadataDelimiter, count)
if len(strs) == count && len(strs[0]) == 0 {
hasMetadata = true
return len(strs[1]) + 2*len(metadataDelimiter), []byte(strs[1]), nil
} else {
if atEOF {
return len(data), data, nil
} else {
return
}
}
})
for articleScanner.Scan() {
text := strings.TrimSpace(articleScanner.Text())
if hasMetadata && len(metadata) == 0 {
metadata = text
} else {
content += text
}
}
return
}
type ArticleStore struct {
config *config.Config
db *sqlx.DB
}
func NewArticleStore(db *sqlx.DB, cfg *config.Config) (*ArticleStore, error) {
s := &ArticleStore{
config: cfg,
db: db,
}
err := s.ScanArticles()
return s, err
}
// ScanArticles walks the artile file tree of the repo and parses them concurrently.
func (s *ArticleStore) ScanArticles() error {
repoService := service.NewRepoService(s.config)
if err := repoService.UpdateRepo(); err != nil {
return err
}
var wg sync.WaitGroup
repoPath := s.config.Repository.LocalPath
articlePath := path.Join(repoPath, "articles")
err := filepath.WalkDir(articlePath, func(p string, d fs.DirEntry, err error) error {
basename := d.Name()
if err != nil ||
d.IsDir() || // Exclude directories
strings.HasPrefix(basename, ".") || // Exclude hidden files
filepath.Ext(basename) != ".md" { // Exclude non-markdown files
return nil
}
// Parse the articles concurrently.
wg.Add(1)
go func() {
defer wg.Done()
article, err := NewArticle(p, d)
if err == nil {
_, err = s.db.NamedExec(`
REPLACE INTO articles(
slug, title, content, preview, categories, tags, pinned, draft, published_at
) VALUES(
:slug, :title, :content, :preview, :categories, :tags, :pinned, :draft, :published_at
)`,
article,
)
}
if err != nil {
log.Panicf("Failed to parse article with error: %v.", err)
}
}()
return err
})
wg.Wait()
return err
}
func (s *ArticleStore) ReadArticles(limit int, offset int) ([]*Article, error) {
rows, err := s.db.Queryx(`SELECT * FROM articles ORDER BY published_at DESC LIMIT ? OFFSET ?`, limit, offset*limit)
if err != nil {
return nil, err
}
var articles []*Article
for rows.Next() {
var article Article
err = rows.StructScan(&article)
if err != nil {
return nil, err
}
articles = append(articles, &article)
}
return articles, nil
}
func (s *ArticleStore) ReadArticleCount() (int, error) {
var count int
err := s.db.Get(&count, `SELECT COUNT(*) FROM articles`)
return count, err
}
func (s *ArticleStore) ReadArticle(slug string) (*Article, error) {
var article Article
if err := s.db.Get(&article, `SELECT * FROM articles WHERE slug=?`, slug); err != nil {
return nil, err
}
return &article, nil
}