@dijitrak/react-nextjs-seo-plugin
Version:
A modern, user-friendly SEO plugin for React and Next.js with multilingual support and comprehensive optimization tools
84 lines (72 loc) • 2.71 kB
JavaScript
/**
* Sitemap.xml oluşturma işlemleri
*/
/**
* Sitemap.xml oluşturur
* @param {Array} pages Sayfalar
* @param {string} siteUrl Site URL'si
* @param {Array} excludedPaths Hariç tutulacak yollar
* @param {Array} customUrls Özel URL'ler
* @returns {string} Sitemap.xml içeriği
*/
export function generateSitemapXml(
pages = [],
siteUrl = '',
excludedPaths = [],
customUrls = []
) {
if (!siteUrl) {
throw new Error('Site URL is required to generate sitemap.');
}
// Base URL formatını düzelt
const baseUrl = siteUrl.endsWith('/') ? siteUrl.slice(0, -1) : siteUrl;
// XML başlangıcı
let xml = '<?xml version="1.0" encoding="UTF-8"?>\n';
xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"\n';
xml += ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n';
xml += ' xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9\n';
xml += ' http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">\n';
// Sayfa URL'lerini ekle
if (pages && pages.length > 0) {
pages.forEach(page => {
// Hariç tutulan yolları atla
if (excludedPaths.includes(page.path)) {
return;
}
// Eğer sayfa durumu yayında değilse, atla
if (page.status && page.status !== 'published') {
return;
}
const path = page.path.startsWith('/') ? page.path : `/${page.path}`;
const url = `${baseUrl}${path}`;
const lastmod = page.lastModified || page.updatedAt || new Date().toISOString().split('T')[0];
const changefreq = page.changeFrequency || 'weekly';
const priority = page.priority || (path === '/' ? '1.0' : '0.8');
xml += ' <url>\n';
xml += ` <loc>${url}</loc>\n`;
xml += ` <lastmod>${lastmod}</lastmod>\n`;
xml += ` <changefreq>${changefreq}</changefreq>\n`;
xml += ` <priority>${priority}</priority>\n`;
xml += ' </url>\n';
});
}
// Özel URL'leri ekle
if (customUrls && customUrls.length > 0) {
customUrls.forEach(item => {
const path = item.path.startsWith('/') ? item.path : `/${item.path}`;
const url = item.url || `${baseUrl}${path}`;
const lastmod = item.lastModified || new Date().toISOString().split('T')[0];
const changefreq = item.changeFrequency || 'monthly';
const priority = item.priority || '0.5';
xml += ' <url>\n';
xml += ` <loc>${url}</loc>\n`;
xml += ` <lastmod>${lastmod}</lastmod>\n`;
xml += ` <changefreq>${changefreq}</changefreq>\n`;
xml += ` <priority>${priority}</priority>\n`;
xml += ' </url>\n';
});
}
// XML sonlandırma
xml += '</urlset>';
return xml;
}