-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
createFeedItems.mjs
86 lines (71 loc) · 2.26 KB
/
createFeedItems.mjs
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
//@ts-check
/* eslint-env node */
import path from 'path';
import { simpleGit } from 'simple-git';
/** @type {import('@docusaurus/plugin-content-blog').CreateFeedItemsFn} */
export async function createFeedItems(params) {
const { blogPosts, defaultCreateFeedItems, ...rest } = params;
const feedItems = await defaultCreateFeedItems({
blogPosts,
...rest,
});
for (const feedItem of feedItems) {
// blogPost.metadata.permalink: '/2023/01/22/image-optimisation-tinypng-api',
// feedItem.link: 'https://johnnyreilly.com/2023/01/22/image-optimisation-tinypng-api',
const relatedBlogEntry = blogPosts.find((blogPost) =>
feedItem.link.endsWith(blogPost.metadata.permalink),
);
if (!relatedBlogEntry) {
console.log('blogFilePath not found', feedItem.link);
throw new Error(`blogFilePath not found ${feedItem.link}`);
}
// source: '@site/blog/2023-01-22-image-optimisation-tinypng-api/index.md',
const gitLatestCommitString = await getGitLatestCommitDateFromFilePath(
relatedBlogEntry.metadata.source.replace('@site/', 'blog-website/'),
);
const gitLatestCommitDate = gitLatestCommitString
? new Date(gitLatestCommitString)
: undefined;
if (gitLatestCommitDate) {
feedItem.date = gitLatestCommitDate;
}
}
// keep only the 20 most recently updated blog posts in the feed
const latest20FeedItems = Array.from(feedItems)
.sort((a, b) => b.date.getDate() - a.date.getDate())
.slice(0, 20);
return latest20FeedItems;
}
/**
* Given a file path, return the last commit date
* @param {string} filePath
* @returns
*/
async function getGitLatestCommitDateFromFilePath(filePath) {
const git = getSimpleGit();
const log = await git.log({
file: filePath,
});
const latestCommitDate = log.latest?.date;
return latestCommitDate;
}
/** @type {import('simple-git').SimpleGit | undefined} */
let git;
/**
* get a simple git instance
* @returns SimpleGit
*/
function getSimpleGit() {
if (!git) {
const baseDir = path.resolve(process.cwd(), '..');
/** @type {Partial<import('simple-git').SimpleGitOptions>} */
const options = {
baseDir,
binary: 'git',
maxConcurrentProcesses: 6,
trimmed: false,
};
git = simpleGit(options);
}
return git;
}