mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2025-08-31 23:53:25 +00:00
fix sitemap
This commit is contained in:
@@ -2,29 +2,33 @@ import fs from 'fs';
|
|||||||
import { glob } from 'glob';
|
import { glob } from 'glob';
|
||||||
import { languages } from '$lib/languages';
|
import { languages } from '$lib/languages';
|
||||||
|
|
||||||
|
function getURLForLanguage(lang: string, path: string): string {
|
||||||
|
return `https://gpx.studio${lang === 'en' ? '' : `/${lang}`}${path}`;
|
||||||
|
}
|
||||||
|
|
||||||
function generateSitemap() {
|
function generateSitemap() {
|
||||||
const pages = glob.sync('**/*.html', { cwd: 'build' });
|
const pages = glob.sync('**/*.html', { cwd: 'build' }).map((page) => `/${page}`);
|
||||||
|
|
||||||
let sitemap = '<?xml version="1.0" encoding="UTF-8"?>\n';
|
let sitemap = '<?xml version="1.0" encoding="UTF-8"?>\n';
|
||||||
sitemap += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">\n';
|
sitemap += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">\n';
|
||||||
|
|
||||||
pages.forEach((page) => {
|
pages.forEach((page) => {
|
||||||
const url = page.replace('.html', '').replace('index', '');
|
const path = page.replace('/index.html', '').replace('.html', '');
|
||||||
|
|
||||||
const rootDir = url.split('/')[0];
|
const rootDir = path.split('/')[1];
|
||||||
if (url.includes('embed') || url.includes('404') || languages[url] || languages[rootDir]) {
|
if (path.includes('embed') || path.includes('404') || languages[path] || languages[rootDir]) {
|
||||||
// Skip localized pages
|
// Skip localized pages
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.keys(languages).forEach((language) => {
|
Object.keys(languages).forEach((language) => {
|
||||||
sitemap += `<url>\n`;
|
sitemap += `<url>\n`;
|
||||||
sitemap += ` <loc>https://gpx.studio/${language === 'en' ? '' : language + '/'}${url}</loc>\n`;
|
sitemap += ` <loc>${getURLForLanguage(language, path)}</loc>\n`;
|
||||||
|
|
||||||
Object.keys(languages).forEach((alternate) => {
|
Object.keys(languages).forEach((alternate) => {
|
||||||
if (alternate === language) return;
|
if (alternate === language) return;
|
||||||
|
|
||||||
sitemap += ` <xhtml:link rel="alternate" hreflang="${alternate}" href="https://gpx.studio/${alternate === 'en' ? '' : alternate + '/'}${url}" />\n`;
|
sitemap += ` <xhtml:link rel="alternate" hreflang="${alternate}" href="${getURLForLanguage(alternate, path)}" />\n`;
|
||||||
});
|
});
|
||||||
|
|
||||||
sitemap += `</url>\n`;
|
sitemap += `</url>\n`;
|
||||||
|
Reference in New Issue
Block a user