From 0b5c82c41c648a4b606e5b6bafe6adad5ac690d7 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Mon, 13 Mar 2023 12:49:41 +0800 Subject: [PATCH] build --- lib/sitemap.xml.js | 89 +++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/lib/sitemap.xml.js b/lib/sitemap.xml.js index c182b38b..d51b5e16 100644 --- a/lib/sitemap.xml.js +++ b/lib/sitemap.xml.js @@ -3,53 +3,52 @@ import fs from 'fs' import BLOG from '@/blog.config' export async function generateSitemapXml({ allPages }) { - const urls = [{ - loc: `${BLOG.LINK}`, - lastmod: new Date().toISOString().split('T')[0], - changefreq: 'daily' - }, { - loc: `${BLOG.LINK}/archive`, - lastmod: new Date().toISOString().split('T')[0], - changefreq: 'daily' - }, { - loc: `${BLOG.LINK}/category`, - lastmod: new Date().toISOString().split('T')[0], - changefreq: 'daily' - }, { - loc: `${BLOG.LINK}/tag`, - lastmod: new Date().toISOString().split('T')[0], - changefreq: 'daily' - }] + const urls = [{ + loc: `${BLOG.LINK}`, + lastmod: new Date().toISOString().split('T')[0], + changefreq: 'daily' + }, { + loc: `${BLOG.LINK}/archive`, + lastmod: new Date().toISOString().split('T')[0], + changefreq: 'daily' + }, { + loc: `${BLOG.LINK}/category`, + lastmod: new Date().toISOString().split('T')[0], + changefreq: 'daily' + }, { + loc: `${BLOG.LINK}/tag`, + lastmod: new Date().toISOString().split('T')[0], + changefreq: 'daily' + }] - allPages?.forEach(post => { - urls.push({ - loc: `${BLOG.LINK}/${post.slug}`, - lastmod: new Date(post?.date?.start_date || post?.createdTime).toISOString().split('T')[0], - changefreq: 'daily' - }) + allPages?.forEach(post => { + urls.push({ + loc: `${BLOG.LINK}/${post.slug}`, + lastmod: new Date(post?.date?.start_date || post?.createdTime).toISOString().split('T')[0], + changefreq: 'daily' }) - const xml = createSitemapXml(urls) - try { - fs.writeFileSync('sitemap.xml', xml) - fs.writeFileSync('./public/sitemap.xml', xml) - } catch (error) { - console.warn('无法写入文件', error) - } + }) + const xml = createSitemapXml(urls) + try { + fs.writeFileSync('sitemap.xml', xml) + fs.writeFileSync('./public/sitemap.xml', xml) + } catch (error) { + console.warn('无法写入文件', error) + } +} +function createSitemapXml(urls) { + let urlsXml = '' + urls.forEach(u => { + urlsXml += ` +${u.loc} +${u.lastmod} +${u.changefreq} + +` + }) - - function createSitemapXml(urls) { - let urlsXml = '' - urls.forEach(u => { - urlsXml += ` - ${u.loc} - ${u.lastmod} - ${u.changefreq} - - ` - }) - - return ` - ` - } +}