-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
75 lines (68 loc) · 2.09 KB
/
utils.js
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
const fs = require('fs')
const moment = require('moment-timezone')
module.exports = {}
module.exports.articleList = (articles, authors) => {
const ret = []
for (const artId in articles) {
const article = articles[artId]
const author = authors[article.author]
const body = fs.readFileSync(`content/${article.body}`, 'utf8')
const publishTime = moment(article.datetime).tz('America/Denver')
ret.unshift({
title: article.title,
author: `${author.last}, ${author.first}`,
slug: module.exports.slugArticle(article),
datetime: publishTime.format('llll zz'),
summary: body.split(/\n/, 1)[0],
})
}
return ret
}
module.exports.slugAuthor = (author) => {
return `${author.last.toLowerCase()}-${author.first.toLowerCase()}`.replace(/\s+/g, '-')
}
module.exports.slugArticle = (article) => {
return article.title.toLowerCase().replace(/\s+/g, '-')
}
module.exports.articleRecords = (articles, authors) => {
const ret = []
for (const artId in articles) {
const article = articles[artId]
const author = authors[article.author]
const publishTime = moment(article.datetime).tz('America/Denver')
ret.unshift({
type: 'article',
title: article.title,
author,
slug: module.exports.slugArticle(article),
datetime: publishTime.format('LLLL zz'),
body: fs.readFileSync(`content/${article.body}`, 'utf8'),
})
}
return ret
}
module.exports.authorList = (authors) => {
const ret = []
for (const authorId in authors) {
const author = authors[authorId]
ret.push(Object.assign({}, author, {
type: 'author',
fullname: `${author.first} ${author.last}`,
slug: module.exports.slugAuthor(author),
}))
}
return ret
}
module.exports.authorRecords = (authors) => {
const ret = []
for (const authorId in authors) {
const author = authors[authorId]
ret.push(Object.assign({}, author, {
type: 'author',
fullname: `${author.first} ${author.last}`,
slug: module.exports.slugAuthor(author),
bio: fs.readFileSync(`content/${author.bio}`, 'utf8'),
}))
}
return ret
}