feat: accessibility enhancement
All checks were successful
Deploy / Deploy (push) Successful in 1m16s
All checks were successful
Deploy / Deploy (push) Successful in 1m16s
closes #3
This commit is contained in:
parent
3e652d5e4e
commit
01c5131055
31 changed files with 1121 additions and 58 deletions
101
scripts/generate-update-sitemaps.js
Normal file
101
scripts/generate-update-sitemaps.js
Normal file
|
@ -0,0 +1,101 @@
|
|||
import { promises as fs } from 'fs';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { dirname, join } from 'path';
|
||||
import xml2js from 'xml2js';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
|
||||
const LANGUAGES = ['en-US', 'zh-CN', 'zh-TW'];
|
||||
const BASE_URL = 'starset.wiki'; // Replace with your actual domain
|
||||
|
||||
async function getYearlyIndices(lang) {
|
||||
const indexDir = join(__dirname, '..', 'data', lang, 'updates', 'index');
|
||||
try {
|
||||
const files = await fs.readdir(indexDir);
|
||||
return files.filter(file => file.match(/^\d{4}\.json$/));
|
||||
} catch (err) {
|
||||
console.error(`Error reading index directory for ${lang}:`, err);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async function generateUpdateUrls(lang) {
|
||||
const yearFiles = await getYearlyIndices(lang);
|
||||
const urls = [];
|
||||
|
||||
for (const yearFile of yearFiles) {
|
||||
try {
|
||||
const content = await fs.readFile(
|
||||
join(__dirname, '..', 'data', lang, 'updates', 'index', yearFile),
|
||||
'utf-8'
|
||||
);
|
||||
const data = JSON.parse(content);
|
||||
|
||||
if (data.updates) {
|
||||
for (const update of data.updates) {
|
||||
urls.push({
|
||||
loc: `https://${BASE_URL}/updates/${update.id}`,
|
||||
lastmod: update.date,
|
||||
changefreq: 'monthly',
|
||||
priority: '0.7'
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`Error processing ${yearFile} for ${lang}:`, err);
|
||||
}
|
||||
}
|
||||
|
||||
return urls;
|
||||
}
|
||||
|
||||
async function generateSitemap(urls) {
|
||||
const sitemap = {
|
||||
urlset: {
|
||||
$: {
|
||||
xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9'
|
||||
},
|
||||
url: urls
|
||||
}
|
||||
};
|
||||
|
||||
const builder = new xml2js.Builder();
|
||||
return builder.buildObject(sitemap);
|
||||
}
|
||||
|
||||
async function mergeSitemaps() {
|
||||
const parser = new xml2js.Parser();
|
||||
const mainSitemapPath = join(__dirname, '..', 'dist', 'sitemap.xml');
|
||||
|
||||
try {
|
||||
const mainSitemapContent = await fs.readFile(mainSitemapPath, 'utf-8');
|
||||
const mainSitemap = await parser.parseStringPromise(mainSitemapContent);
|
||||
|
||||
// Filter out existing update URLs while maintaining order
|
||||
const existingUrls = mainSitemap.urlset.url.filter(url =>
|
||||
!url.loc[0].includes('/updates/'));
|
||||
|
||||
mainSitemap.urlset.url = existingUrls;
|
||||
|
||||
// Add language-specific update URLs at the end
|
||||
for (const lang of LANGUAGES) {
|
||||
const updateUrls = await generateUpdateUrls(lang);
|
||||
mainSitemap.urlset.url.push(...updateUrls);
|
||||
}
|
||||
|
||||
const builder = new xml2js.Builder();
|
||||
const newSitemap = builder.buildObject(mainSitemap);
|
||||
|
||||
await fs.writeFile(mainSitemapPath, newSitemap);
|
||||
console.log('Successfully updated sitemap.xml in dist directory');
|
||||
} catch (err) {
|
||||
console.error('Error merging sitemaps:', err);
|
||||
if (err.code === 'ENOENT') {
|
||||
console.error('Make sure the dist directory exists and contains sitemap.xml');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Run the script
|
||||
mergeSitemaps().catch(console.error);
|
Loading…
Add table
Add a link
Reference in a new issue