|
| 1 | +const blogPluginExports = require('@docusaurus/plugin-content-blog'); |
| 2 | +const utils = require('@docusaurus/utils'); |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +const defaultBlogPlugin = blogPluginExports.default; |
| 6 | +const MIN_RELATED_POSTS = 10; |
| 7 | + |
| 8 | +function getMultipleRandomElement(arr, num) { |
| 9 | + const shuffled = [...arr].sort(() => 0.5 - Math.random()); |
| 10 | + |
| 11 | + return shuffled.slice(0, num); |
| 12 | +} |
| 13 | + |
| 14 | +function getRelatedPosts(allBlogPosts, metadata) { |
| 15 | + const currentTags = new Set(metadata.frontMatter.tags?.filter((tag) => tag?.toLowerCase() != 'zenstack')); |
| 16 | + |
| 17 | + let relatedPosts = allBlogPosts.filter( |
| 18 | + (post) => |
| 19 | + post.metadata.frontMatter.tags.some((tag) => currentTags.has(tag)) && post.metadata.title !== metadata.title |
| 20 | + ); |
| 21 | + |
| 22 | + if (relatedPosts.length < MIN_RELATED_POSTS) { |
| 23 | + remainingCount = MIN_RELATED_POSTS - relatedPosts.length; |
| 24 | + const remainingPosts = getMultipleRandomElement( |
| 25 | + allBlogPosts.filter((post) => !relatedPosts.includes(post) && post.metadata.title !== metadata.title), |
| 26 | + remainingCount |
| 27 | + ); |
| 28 | + relatedPosts = relatedPosts.concat(remainingPosts); |
| 29 | + } |
| 30 | + |
| 31 | + const filteredPostInfos = relatedPosts.map((post) => { |
| 32 | + return { |
| 33 | + title: post.metadata.title, |
| 34 | + description: post.metadata.description, |
| 35 | + permalink: post.metadata.permalink, |
| 36 | + formattedDate: post.metadata.formattedDate, |
| 37 | + authors: post.metadata.authors, |
| 38 | + readingTime: post.metadata.readingTime, |
| 39 | + date: post.metadata.date, |
| 40 | + relatedWeight: post.metadata.frontMatter.tags.filter((tag) => currentTags.has(tag)).length * 3 + 1, |
| 41 | + }; |
| 42 | + }); |
| 43 | + |
| 44 | + return filteredPostInfos; |
| 45 | +} |
| 46 | + |
| 47 | +async function blogPluginExtended(...pluginArgs) { |
| 48 | + const blogPluginInstance = await defaultBlogPlugin(...pluginArgs); |
| 49 | + |
| 50 | + return { |
| 51 | + // Add all properties of the default blog plugin so existing functionality is preserved |
| 52 | + ...blogPluginInstance, |
| 53 | + contentLoaded: async function (data) { |
| 54 | + await blogPluginInstance.contentLoaded(data); |
| 55 | + const { content: blogContents, actions } = data; |
| 56 | + const { blogPosts: allBlogPosts } = blogContents; |
| 57 | + const { createData } = actions; |
| 58 | + // Create routes for blog entries. |
| 59 | + await Promise.all( |
| 60 | + allBlogPosts.map(async (blogPost) => { |
| 61 | + const { metadata } = blogPost; |
| 62 | + const relatedPosts = getRelatedPosts(allBlogPosts, metadata); |
| 63 | + await createData( |
| 64 | + // Note that this created data path must be in sync with |
| 65 | + // metadataPath provided to mdx-loader. |
| 66 | + `${utils.docuHash(metadata.source)}.json`, |
| 67 | + JSON.stringify({ ...metadata, relatedPosts }, null, 2) |
| 68 | + ); |
| 69 | + }) |
| 70 | + ); |
| 71 | + }, |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +module.exports = { |
| 76 | + ...blogPluginExports, |
| 77 | + default: blogPluginExtended, |
| 78 | +}; |
0 commit comments