From ab7de7f51709e5e5bd026f31e1667d72a447faa3 Mon Sep 17 00:00:00 2001 From: Hsu Zhong Jun <27919917+dcshzj@users.noreply.github.com> Date: Mon, 13 Jan 2025 10:41:43 +0800 Subject: [PATCH] fix: duplicate image thumbnail logic for GitHub sites (#981) --- tooling/build/scripts/generate-sitemap.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tooling/build/scripts/generate-sitemap.js b/tooling/build/scripts/generate-sitemap.js index fc2c885b4..9b343d86a 100644 --- a/tooling/build/scripts/generate-sitemap.js +++ b/tooling/build/scripts/generate-sitemap.js @@ -5,6 +5,22 @@ const JSON_SCHEMA_VERSION = "0.1.0" const schemaDirPath = path.join(__dirname, "../schema") const sitemapPath = path.join(__dirname, "../sitemap.json") +const getResourceImage = (schemaData) => { + if (schemaData.page.image) return schemaData.page.image + + if (!Array.isArray(schemaData.content)) return undefined + + const firstImageComponent = schemaData.content.find( + (item) => item.type === "image", + ) + return firstImageComponent + ? { + src: firstImageComponent.src, + alt: firstImageComponent.alt, + } + : undefined +} + const getSchemaJson = async (filePath) => { try { const schemaContent = await fs.readFile(filePath, "utf8") @@ -70,7 +86,7 @@ const getSiteMapEntry = async (fullPath, relativePath, name) => { summary, category: schemaData.page.category, date: schemaData.page.date, - image: schemaData.page.image, + image: getResourceImage(schemaData), tags: schemaData.page.tags, }