-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload-data.js
142 lines (134 loc) · 4.27 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
const { promises: fs } = require('fs');
const path = require('path');
const remark = require('remark');
const html = require('remark-html');
const frontmatter = require('remark-frontmatter');
const extract = require('remark-extract-frontmatter');
const yaml = require('yaml').parse;
const toHTML = remark()
.use(html)
.use(frontmatter, ['yaml'])
.use(extract, { yaml: yaml });
const photosPerPage = 10;
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.contents.length > 0 ? file.contents : undefined,
});
}),
);
const main = async () => {
const dataFolder = path.join(process.cwd(), 'data-js', '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(), 'data-js', `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(), 'data-js', `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(),
'data-js',
'photos',
`${p.name}.json`,
);
const doc = await parse(await fs.readFile(source, 'utf-8'));
const slug = path.parse(f).name;
doc.tags?.map((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 };
}),
);
// Write paginated, sorted photos pages
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(), 'data-js', `photos-takenAt-${k}.json`),
page,
),
),
);
// Write tags
await writeFile(
path.join(process.cwd(), 'data-js', `photos-tags.json`),
Object.entries(tags)
.sort(([, v1], [, v2]) => v2.length - v1.length)
.filter(([, v]) => v.length > 1)
.reduce((t, [k, v]) => ({ ...t, [k]: v })),
);
}),
]);
};
main();