Skip to content

Commit 310e613

Browse files
committed
修复未启用国际化时构建站点地图错误
1 parent 8b7d9fc commit 310e613

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

lib/sitemap-generator.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,20 @@ class SitemapGenerator {
1717

1818
// 生成单个 URL 节点
1919
generateUrlNode(url, lang, metadata) {
20-
const langCode = this.getLanguageCode(lang);
2120
const normalizedUrl = url.startsWith('/') ? url.substring(1) : url;
22-
const fullUrl = `${langCode}/${normalizedUrl}`;
21+
const isI18nEnabled = isFeatureEnabled(this.config, 'i18n');
22+
const langCode = this.getLanguageCode(lang);
23+
24+
// 当启用国际化时才添加语言前缀
25+
const fullUrl = isI18nEnabled ?
26+
`${langCode}/${normalizedUrl}` :
27+
normalizedUrl;
2328

2429
return ` <url>
2530
<loc>${this.baseUrl}/${fullUrl}</loc>${metadata?.gitInfo?.revision?.lastModifiedTime ? `
2631
<lastmod>${metadata.gitInfo.revision.lastModifiedTime}</lastmod>` : ''}
27-
<changefreq>${metadata.changefreq || 'weekly'}</changefreq>
28-
<priority>${metadata.priority || '0.5'}</priority>
32+
<changefreq>weekly</changefreq>
33+
<priority>0.5</priority>
2934
</url>`;
3035
}
3136

@@ -36,13 +41,16 @@ class SitemapGenerator {
3641
}
3742

3843
if (item.href) {
39-
const metadata = this.pages.get(item.href) || {};
44+
// 将 /.html 转换为 /index.html
45+
const normalizedHref = item.href === '/.html' ? '/index.html' : item.href;
46+
47+
const metadata = this.pages.get(normalizedHref) || {};
4048

4149
const langCode = this.getLanguageCode(lang);
42-
const key = `${langCode}:${item.href}`;
50+
const key = `${langCode}:${normalizedHref}`;
4351
urls.set(key, {
4452
lang: langCode,
45-
url: item.href,
53+
url: normalizedHref,
4654
metadata
4755
});
4856
}
@@ -57,9 +65,11 @@ class SitemapGenerator {
5765
collectNavigationUrls() {
5866
const urls = new Map();
5967
const isI18nEnabled = isFeatureEnabled(this.config, 'i18n');
68+
69+
// 如果没有启用国际化,则只使用空数组作为默认语言
6070
const languages = isI18nEnabled
6171
? (this.config.languages || [this.config.i18n?.default || 'en'])
62-
: ['en'];
72+
: ['']; // 改为空字符串,表示没有语言前缀
6373

6474
// 收集每种语言的 URL
6575
for (const lang of languages) {

0 commit comments

Comments
 (0)