Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9727cc1

Browse files
committed
Refactor sitemap generation to include xhtml:link elements for alternate language URLs, enhancing multi-language support and improving SEO. Update build.py to streamline sitemap creation and remove redundant code for URL entries.
1 parent 89c4058 commit 9727cc1

File tree

2 files changed

+48
-22172
lines changed

2 files changed

+48
-22172
lines changed

‎build.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,9 @@ def render_html(commands, lang="en", all_langs=None, year=None):
459459
path = "/" if l == "en" else f"/{l}/"
460460
lang_name = lang_map.get(l, l.upper())
461461
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+
)
463465
else:
464466
links.append(
465467
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):
491493

492494
# 在 render_html 函数后添加这两个新函数
493495
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-
524496
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">
526499
{entries}
527500
</urlset>"""
528501

529502
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>"""
530530

531-
entries = get_url_entries(commands_by_lang)
531+
final_sitemap_content = sitemap_template.format(entries=url_entry)
532532

533533
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)
535535

536536
print("Sitemap generated: sitemap.xml")
537537

@@ -588,7 +588,7 @@ def generate_robots_txt():
588588

589589
with open(output_file, "w", encoding="utf-8") as file:
590590
file.write(html_content)
591-
591+
592592
os.system(f"prettier --write {output_file}")
593593
print(f"HTML file generated: {output_file}")
594594

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /