@@ -459,7 +459,9 @@ def render_html(commands, lang="en", all_langs=None, year=None):
459
459
path = "/" if l == "en" else f"/{ l } /"
460
460
lang_name = lang_map .get (l , l .upper ())
461
461
if l == lang :
462
- links .append (f'<span class="text-white font-bold mx-2">{ lang_name } </span>' )
462
+ links .append (
463
+ f'<span class="text-white font-bold mx-2">{ lang_name } </span>'
464
+ )
463
465
else :
464
466
links .append (
465
467
f'<a href="{ path } " class="text-white hover:underline mx-2">{ lang_name } </a>'
@@ -491,47 +493,45 @@ def render_html(commands, lang="en", all_langs=None, year=None):
491
493
492
494
# 在 render_html 函数后添加这两个新函数
493
495
def generate_sitemap (commands_by_lang ):
494
- # 获取所有命令的链接
495
- def get_url_entries (commands_by_lang ):
496
- entries = []
497
- # 添加主页
498
- for lang in commands_by_lang .keys ():
499
- path = f"/{ lang } " if lang != "en" else ""
500
- entries .append (
501
- f""" <url>
502
- <loc>https://linux-commands.labex.io{ path } /</loc>
503
- <lastmod>{{date}}</lastmod>
504
- <changefreq>daily</changefreq>
505
- <priority>1.0</priority>
506
- </url>"""
507
- )
508
-
509
- # 添加所有命令链接
510
- for lang , commands in commands_by_lang .items ():
511
- for cmd in commands :
512
- if cmd ["link" ] and cmd ["link" ] != "#" :
513
- entries .append (
514
- f""" <url>
515
- <loc>{ cmd ["link" ]} </loc>
516
- <lastmod>{{date}}</lastmod>
517
- <changefreq>monthly</changefreq>
518
- <priority>0.8</priority>
519
- </url>"""
520
- )
521
-
522
- return "\\ n" .join (entries )
523
-
524
496
sitemap_template = """<?xml version="1.0" encoding="UTF-8"?>
525
- <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
497
+ <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
498
+ xmlns:xhtml="http://www.w3.org/1999/xhtml">
526
499
{entries}
527
500
</urlset>"""
528
501
529
502
current_date = datetime .now ().strftime ("%Y-%m-%d" )
503
+ base_url = "https://linux-commands.labex.io"
504
+
505
+ # All available languages from the build process
506
+ languages = list (commands_by_lang .keys ())
507
+
508
+ # Generate xhtml:link entries for the homepage
509
+ xhtml_links = []
510
+ for lang in languages :
511
+ path = f"/{ lang } /" if lang != "en" else "/"
512
+ href = f"{ base_url } { path } "
513
+ xhtml_links .append (
514
+ f' <xhtml:link rel="alternate" hreflang="{ lang } " href="{ href } "/>'
515
+ )
516
+
517
+ # Add x-default link
518
+ xhtml_links .append (
519
+ f' <xhtml:link rel="alternate" hreflang="x-default" href="{ base_url } /"/>'
520
+ )
521
+
522
+ # Create the single <url> entry for the homepage with all language alternatives
523
+ url_entry = f""" <url>
524
+ <loc>{ base_url } /</loc>
525
+ <lastmod>{ current_date } </lastmod>
526
+ <changefreq>daily</changefreq>
527
+ <priority>1.0</priority>
528
+ { '\n ' .join (xhtml_links )}
529
+ </url>"""
530
530
531
- entries = get_url_entries ( commands_by_lang )
531
+ final_sitemap_content = sitemap_template . format ( entries = url_entry )
532
532
533
533
with open ("sitemap.xml" , "w" , encoding = "utf-8" ) as f :
534
- f .write (sitemap_template . format ( entries = entries ). format ( date = current_date ) )
534
+ f .write (final_sitemap_content )
535
535
536
536
print ("Sitemap generated: sitemap.xml" )
537
537
@@ -588,7 +588,7 @@ def generate_robots_txt():
588
588
589
589
with open (output_file , "w" , encoding = "utf-8" ) as file :
590
590
file .write (html_content )
591
-
591
+
592
592
os .system (f"prettier --write { output_file } " )
593
593
print (f"HTML file generated: { output_file } " )
594
594
0 commit comments