From 5b18451828a8ec23e3eefd318acfb15887dd4fc9 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Tue, 8 Oct 2024 16:35:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=94=99=E8=AF=AF=E7=9A=84?= =?UTF-8?q?=E6=A0=87=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/db/getSiteData.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/lib/db/getSiteData.js b/lib/db/getSiteData.js index 266e8f87..f27f30a5 100755 --- a/lib/db/getSiteData.js +++ b/lib/db/getSiteData.js @@ -113,9 +113,47 @@ function handleDataBeforeReturn(db) { db.allNavPages = shortenIds(db?.allNavPages) // db.allPages = cleanBlocks(db?.allPages) + db.allNavPages = cleanPages(db?.allNavPages, db.tagOptions) + db.allPages = cleanPages(db.allPages, db.tagOptions) + db.latestPosts = cleanPages(db.latestPosts, db.tagOptions) return db } +/** + * 处理文章列表中的异常数据 + * @param {Array} allPages - 所有页面数据 + * @param {Array} tagOptions - 标签选项 + * @returns {Array} 处理后的 allPages + */ +function cleanPages(allPages, tagOptions) { + // 校验参数是否为数组 + if (!Array.isArray(allPages) || !Array.isArray(tagOptions)) { + console.warn('Invalid input: allPages and tagOptions should be arrays.') + return allPages || [] // 返回空数组或原始值 + } + + // 提取 tagOptions 中所有合法的标签名 + const validTags = new Set( + tagOptions + .map(tag => (typeof tag.name === 'string' ? tag.name : null)) + .filter(Boolean) // 只保留合法的字符串 + ) + + // 遍历所有的 pages + allPages.forEach(page => { + // 确保 tagItems 是数组 + if (Array.isArray(page.tagItems)) { + // 对每个 page 的 tagItems 进行过滤 + page.tagItems = page.tagItems.filter( + tagItem => + validTags.has(tagItem?.name) && typeof tagItem.name === 'string' // 校验 tagItem.name 是否是字符串 + ) + } + }) + + return allPages +} + /** * 清理一组数据的id * @param {*} items