|
| 1 | +/** |
| 2 | + * Wraps @docusaurus/plugin-content-blog to expose the latest posts as |
| 3 | + * global data, so homepage components can render them via usePluginData. |
| 4 | + * Registered in docusaurus.config.ts in place of the preset's blog |
| 5 | + * (preset blog is set to false) with the same options. |
| 6 | + */ |
| 7 | +const path = require("path"); |
| 8 | +const fs = require("fs"); |
| 9 | +const blogPluginExports = require("@docusaurus/plugin-content-blog"); |
| 10 | + |
| 11 | +const defaultBlogPlugin = blogPluginExports.default; |
| 12 | + |
| 13 | +// Posts don't set an `image` frontmatter, so fall back to the first |
| 14 | +// image referenced in the post body for the card cover |
| 15 | +function extractFirstImage(content) { |
| 16 | + if (!content) return undefined; |
| 17 | + const markdownImage = content.match(/!\[[^\]]*\]\(\s*([^)\s]+)/); |
| 18 | + if (markdownImage) return markdownImage[1]; |
| 19 | + const htmlImage = content.match(/<img[^>]+src=["']([^"']+)["']/i); |
| 20 | + return htmlImage ? htmlImage[1] : undefined; |
| 21 | +} |
| 22 | + |
| 23 | +// Covers referenced relative to the post folder (e.g. ./assets/x.png) aren't |
| 24 | +// served outside the post page, so copy them into static/ and return that URL. |
| 25 | +// Generated files live in static/img/blog-covers/ (gitignored). |
| 26 | +function resolveCover(post, siteDir) { |
| 27 | + const raw = |
| 28 | + post.metadata.frontMatter?.image || extractFirstImage(post.content); |
| 29 | + if (!raw) return undefined; |
| 30 | + if (/^(https?:)?\/\//i.test(raw) || raw.startsWith("/")) return raw; |
| 31 | + |
| 32 | + try { |
| 33 | + const sourcePath = post.metadata.source.replace(/^@site[\\/]/, ""); |
| 34 | + const postDir = path.dirname(path.join(siteDir, sourcePath)); |
| 35 | + const imagePath = path.resolve(postDir, raw); |
| 36 | + if (!fs.existsSync(imagePath)) return undefined; |
| 37 | + |
| 38 | + const coversDir = path.join(siteDir, "static", "img", "blog-covers"); |
| 39 | + const fileName = `${path.basename(postDir)}-${path.basename(imagePath)}`; |
| 40 | + fs.mkdirSync(coversDir, { recursive: true }); |
| 41 | + fs.copyFileSync(imagePath, path.join(coversDir, fileName)); |
| 42 | + return `/img/blog-covers/${fileName}`; |
| 43 | + } catch { |
| 44 | + return undefined; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +async function blogPluginEnhanced(context, options) { |
| 49 | + const blogPlugin = await defaultBlogPlugin(context, options); |
| 50 | + |
| 51 | + return { |
| 52 | + ...blogPlugin, |
| 53 | + async contentLoaded(args) { |
| 54 | + await blogPlugin.contentLoaded?.(args); |
| 55 | + |
| 56 | + const { content, actions } = args; |
| 57 | + const recentPosts = [...content.blogPosts] |
| 58 | + .sort( |
| 59 | + (a, b) => |
| 60 | + new Date(b.metadata.date).getTime() - |
| 61 | + new Date(a.metadata.date).getTime(), |
| 62 | + ) |
| 63 | + .slice(0, 4) |
| 64 | + .map((post) => { |
| 65 | + const author = post.metadata.authors?.[0]; |
| 66 | + return { |
| 67 | + title: post.metadata.title, |
| 68 | + permalink: post.metadata.permalink, |
| 69 | + date: post.metadata.date, |
| 70 | + readingTime: post.metadata.readingTime, |
| 71 | + image: resolveCover(post, context.siteDir), |
| 72 | + author: author |
| 73 | + ? { |
| 74 | + name: author.name, |
| 75 | + handle: author.key || author.name, |
| 76 | + imageURL: author.imageURL, |
| 77 | + } |
| 78 | + : null, |
| 79 | + }; |
| 80 | + }); |
| 81 | + |
| 82 | + actions.setGlobalData({ recentPosts }); |
| 83 | + }, |
| 84 | + }; |
| 85 | +} |
| 86 | + |
| 87 | +module.exports = { |
| 88 | + ...blogPluginExports, |
| 89 | + default: blogPluginEnhanced, |
| 90 | +}; |
0 commit comments