-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathblog-plugin.js
78 lines (67 loc) · 2.92 KB
/
blog-plugin.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
const blogPluginExports = require('@docusaurus/plugin-content-blog');
const utils = require('@docusaurus/utils');
const path = require('path');
const defaultBlogPlugin = blogPluginExports.default;
const MIN_RELATED_POSTS = 10;
function getMultipleRandomElement(arr, num) {
const shuffled = [...arr].sort(() => 0.5 - Math.random());
return shuffled.slice(0, num);
}
function getRelatedPosts(allBlogPosts, metadata) {
const currentTags = new Set(metadata.frontMatter.tags?.filter((tag) => tag?.toLowerCase() != 'zenstack'));
let relatedPosts = allBlogPosts.filter(
(post) =>
post.metadata.frontMatter.tags.some((tag) => currentTags.has(tag)) && post.metadata.title !== metadata.title
);
if (relatedPosts.length < MIN_RELATED_POSTS) {
remainingCount = MIN_RELATED_POSTS - relatedPosts.length;
const remainingPosts = getMultipleRandomElement(
allBlogPosts.filter((post) => !relatedPosts.includes(post) && post.metadata.title !== metadata.title),
remainingCount
);
relatedPosts = relatedPosts.concat(remainingPosts);
}
const filteredPostInfos = relatedPosts.map((post) => {
return {
title: post.metadata.title,
description: post.metadata.description,
permalink: post.metadata.permalink,
formattedDate: post.metadata.formattedDate,
authors: post.metadata.authors,
readingTime: post.metadata.readingTime,
date: post.metadata.date,
relatedWeight: post.metadata.frontMatter.tags.filter((tag) => currentTags.has(tag)).length * 4 + 1,
};
});
return filteredPostInfos;
}
async function blogPluginExtended(...pluginArgs) {
const blogPluginInstance = await defaultBlogPlugin(...pluginArgs);
return {
// Add all properties of the default blog plugin so existing functionality is preserved
...blogPluginInstance,
contentLoaded: async function (data) {
await blogPluginInstance.contentLoaded(data);
const { content: blogContents, actions } = data;
const { blogPosts: allBlogPosts } = blogContents;
const { createData } = actions;
// Create routes for blog entries.
await Promise.all(
allBlogPosts.map(async (blogPost) => {
const { metadata } = blogPost;
const relatedPosts = getRelatedPosts(allBlogPosts, metadata);
await createData(
// Note that this created data path must be in sync with
// metadataPath provided to mdx-loader.
`${utils.docuHash(metadata.source)}.json`,
JSON.stringify({ ...metadata, relatedPosts }, null, 2)
);
})
);
},
};
}
module.exports = {
...blogPluginExports,
default: blogPluginExtended,
};