- Updates to reference the new Richtext field from Strapi - Update to include feature image in RSS - General cleanups (removing a lot of console.log) - I think there's a nextjs update in there as well
65 lines
No EOL
1.9 KiB
JavaScript
Executable file
65 lines
No EOL
1.9 KiB
JavaScript
Executable file
import { getAllPosts } from "../external/cms"
|
|
import fs from "fs"
|
|
import prettier from "prettier"
|
|
import config from './config'
|
|
import path from "path"
|
|
|
|
|
|
export const generateSitemap = async () => {
|
|
const getDate = new Date().toISOString()
|
|
|
|
const staticPaths = fs.readdirSync("pages", {withFileTypes: true})
|
|
.filter((staticPage) => {
|
|
if(staticPage.isFile()) {
|
|
return ![
|
|
"api",
|
|
"_app.js",
|
|
"_document.js",
|
|
"404.js",
|
|
"sitemap.xml.js",
|
|
"index.js"
|
|
].includes(staticPage.name);
|
|
}
|
|
})
|
|
.map((staticPagePath) => {
|
|
return `${config.siteURL}/${path.parse(staticPagePath.name).name}`;
|
|
}
|
|
);
|
|
|
|
const postData = await getAllPosts()
|
|
const postList = []
|
|
postData.data.forEach(post => postList.push({slug: post.attributes.Slug, updatedAt: post.attributes.updatedAt}))
|
|
|
|
const formatted = sitemap => prettier.format(sitemap, { parser: "html" });
|
|
|
|
const pageListMap = staticPaths.map(page => {
|
|
return `
|
|
<url>
|
|
<loc>${page}</loc>
|
|
<lastmod>${getDate}</lastmod>
|
|
<priority>1</priority>
|
|
</url>`
|
|
}).join("")
|
|
|
|
const postListSiteMap = postList.map(post => {
|
|
return `
|
|
<url>
|
|
<loc>${`${config.siteURL}/news/${post.slug}`}</loc>
|
|
<lastmod>${post.updatedAt}</lastmod>
|
|
<priority>0.5</priority>
|
|
</url>`
|
|
})
|
|
.join("")
|
|
|
|
const generatedSitemap = `<?xml version="1.0" encoding="UTF-8"?>
|
|
<urlset
|
|
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
|
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
|
|
>${pageListMap}${postListSiteMap}
|
|
</urlset>
|
|
`
|
|
const formattedSitemap = [formatted(generatedSitemap)];
|
|
|
|
fs.writeFileSync("public/sitemap.xml", generatedSitemap, "utf8")
|
|
} |