-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathblog.ts
46 lines (41 loc) · 1.05 KB
/
blog.ts
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
import * as api from "./api";
let paths = null;
function cachedPaths() {
if (paths === null) {
paths = api.getDateOrderedPaths("blog");
}
return paths;
}
export function getBlogPostsByYear(limit?: number) {
let count = 0;
return cachedPaths().reduce((years, post) => {
if (limit !== undefined && count >= limit) {
return years;
}
const date = new Date(post.date);
const year = date.getFullYear().toString();
if (!years[year]) {
years[year] = {
key: year,
title: year,
nested: [],
};
}
delete post.body;
years[year].nested.push(post);
count++;
return years;
}, {});
}
export function getNextPost(afterSlug) {
let slugIndex = cachedPaths().findIndex((post) => post.key === afterSlug);
if (slugIndex > 0) {
return paths[slugIndex - 1];
}
}
export function getPreviousPost(beforeSlug) {
let slugIndex = cachedPaths().findIndex((post) => post.key === beforeSlug);
if (slugIndex !== -1 && slugIndex + 1 < paths.length) {
return paths[slugIndex + 1];
}
}