-
Notifications
You must be signed in to change notification settings - Fork 1
/
load-data.js
177 lines (168 loc) · 4.32 KB
/
load-data.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
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
import { promises as fs } from 'fs'
import * as path from 'path'
import format from 'rehype-format'
import html from 'rehype-stringify'
import { remark } from 'remark'
import extract from 'remark-extract-frontmatter'
import frontmatter from 'remark-frontmatter'
import remark2rehype from 'remark-rehype'
import yaml from 'yaml'
const toHTML = remark()
.use(frontmatter, ['yaml'])
.use(extract, { yaml: yaml.parse })
.use(remark2rehype)
.use(format)
.use(html)
const photosPerPage = 50
const writeFile = async (target, data) =>
fs.writeFile(target, JSON.stringify(data), 'utf-8')
const parse = async (el) =>
new Promise((resolve, reject) =>
toHTML.process(el, (err, file) => {
if (err !== undefined && err !== null) return reject(err)
return resolve({
...file.data,
html: file.value.length > 0 ? file.value : undefined,
})
}),
)
const main = async () => {
const dataFolder = path.join(process.cwd(), 'public', 'data', 'photos')
await fs.mkdir(dataFolder, {
recursive: true,
})
return Promise.all([
// Albums
fs
.readdir(path.join(process.cwd(), 'data', 'albums'))
.then(async (files) => {
const albums = await Promise.all(
files
.filter((s) => s.endsWith('.md'))
.map(async (album) => ({
slug: album.replace(/\.md$/, ''),
doc: await parse(
await fs.readFile(
path.join(process.cwd(), 'data', 'albums', album),
'utf-8',
),
),
})),
)
albums.sort(({ doc: { createdAt: a } }, { doc: { createdAt: b } }) =>
b.localeCompare(a),
)
await writeFile(
path.join(process.cwd(), 'public', 'data', `albums.json`),
albums.reduce((albums, album) => {
const { slug, doc } = album
return {
...albums,
[slug]: doc,
}
}, {}),
)
}),
// Photos
fs
.readdir(path.join(process.cwd(), 'data', 'photos'))
.then(async (files) => {
const photos = files.filter((s) => s.endsWith('.md'))
await writeFile(
path.join(process.cwd(), 'public', 'data', `stats.json`),
{
photos: photos.length,
},
)
const tags = {}
const photoDocs = await Promise.all(
photos.map(async (f) => {
const p = path.parse(f)
const source = path.join(process.cwd(), 'data', 'photos', f)
const target = path.join(
process.cwd(),
'public',
'data',
'photos',
`${p.name}.json`,
)
const doc = await parse(await fs.readFile(source, 'utf-8'))
const slug = path.parse(f).name
doc.tags
?.map((tag) => tag.toLowerCase())
.forEach((tag) => {
if (tags[tag] === undefined) {
tags[tag] = [slug]
} else {
tags[tag].push(slug)
}
})
const sourceModified = (await fs.stat(source)).mtime
let targetModified = -1
try {
targetModified = (await fs.stat(target)).mtime
} catch {}
if (targetModified < sourceModified) {
await writeFile(target, doc)
}
return { slug, doc }
}),
)
// Sort photos by date taken, write paginated
photoDocs.sort(({ doc: { takenAt: a } }, { doc: { takenAt: b } }) =>
b.localeCompare(a),
)
const photoPages = photoDocs.reduce(
(chunks, { slug }) => {
if ((chunks[chunks.length - 1].length ?? 0) >= photosPerPage) {
chunks.push([])
}
chunks[chunks.length - 1].push(slug)
return chunks
},
[[]],
)
await Promise.all(
photoPages.map((page, k) =>
writeFile(
path.join(
process.cwd(),
'public',
'data',
`photos-takenAt-${k}.json`,
),
page,
),
),
)
// Group by year and month
const photoMonths = photoDocs.reduce((photosByMonth, { slug, doc }) => {
const month = doc.takenAt.substr(0, 7)
if (photosByMonth[month] === undefined) {
photosByMonth[month] = []
}
photosByMonth[month].push(slug)
return photosByMonth
}, {})
await Promise.all(
Object.entries(photoMonths).map(([month, slugs]) =>
writeFile(
path.join(
process.cwd(),
'public',
'data',
`photos-byMonth-${month}.json`,
),
slugs,
),
),
)
// Write tags
await writeFile(
path.join(process.cwd(), 'public', 'data', `photos-tags.json`),
tags,
)
}),
])
}
main()