aaronjy-me/next-sitemap.config.cjs
2025-03-21 12:45:31 +00:00

71 lines
No EOL
1.5 KiB
JavaScript

const fs = require('fs'),
fm = require('front-matter')
/** @type {import('next-sitemap').IConfig} */
module.exports = {
siteUrl: process.env.SITE_URL || 'https://www.aaronjy.me',
changefreq: 'weekly',
generateRobotsTxt: true,
autoLastmod: false,
generateIndexSitemap: false,
robotsTxtOptions: {
policies: [
{
userAgent: '*',
allow: '/'
}
]
},
transform: async (config, path) => {
const metadata = {
loc: path
};
if (isHomepage(path)) {
metadata.priority = 1;
} else if (isBasePage(path)) {
metadata.priority = 0.8;
} else {
if (isArticle(path)) {
metadata.priority = 0.6;
const attributes = getArticleAttibutes(`content${path}.md`);
if (!attributes)
return null;
metadata.pubdate = attributes.pubdate ?? null;
metadata.moddate = attributes.moddate ?? null;
console.log("Calculated sitemap dates for article", path);
}
}
return metadata;
}
}
function isHomepage(path) {
return path === "/";
}
function isBasePage(path) {
return path.split("/").length === 2;
}
function isArticle(path) {
return path.startsWith("/writing/");
}
function getArticleAttibutes(path) {
const fileContents = fs.readFileSync(path, {
encoding: 'utf-8'
})
// @ts-ignore
const { attributes } = fm(fileContents);
return {
...attributes,
pubdate: attributes.pubdate?.toISOString() ?? null,
moddate: attributes.moddate?.toISOString() ?? null
};
}