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 b88112f

Browse files
author
huhuhang
committed
Add sitemap and robots.txt generation to build process
1 parent 0fe2c0d commit b88112f

File tree

4 files changed

+3033
-13
lines changed

4 files changed

+3033
-13
lines changed

‎build.py

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,17 +253,80 @@ def render_html(commands):
253253
)
254254

255255

256-
# 主函数
256+
# 在 render_html 函数后添加这两个新函数
257+
def generate_sitemap():
258+
# 获取所有命令的链接
259+
def get_url_entries(commands):
260+
entries = []
261+
# 添加主页
262+
entries.append(
263+
""" <url>
264+
<loc>https://linux-commands.labex.io/</loc>
265+
<lastmod>{date}</lastmod>
266+
<changefreq>daily</changefreq>
267+
<priority>1.0</priority>
268+
</url>"""
269+
)
270+
271+
# 添加所有命令链接
272+
for cmd in commands:
273+
if cmd["link"] and cmd["link"] != "#":
274+
entries.append(
275+
f""" <url>
276+
<loc>{cmd["link"]}</loc>
277+
<lastmod>{{date}}</lastmod>
278+
<changefreq>monthly</changefreq>
279+
<priority>0.8</priority>
280+
</url>"""
281+
)
282+
283+
return "\n".join(entries)
284+
285+
sitemap_template = """<?xml version="1.0" encoding="UTF-8"?>
286+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
287+
{entries}
288+
</urlset>"""
289+
290+
from datetime import datetime
291+
292+
current_date = datetime.now().strftime("%Y-%m-%d")
293+
294+
# 重新解析 README 获取所有链接
295+
all_commands = parse_markdown("README.md")
296+
entries = get_url_entries(all_commands)
297+
298+
with open("sitemap.xml", "w", encoding="utf-8") as f:
299+
f.write(sitemap_template.format(entries=entries).format(date=current_date))
300+
301+
print("Sitemap generated: sitemap.xml")
302+
303+
304+
def generate_robots_txt():
305+
robots_content = """User-agent: *
306+
Allow: /
307+
308+
Sitemap: https://linux-commands.labex.io/sitemap.xml"""
309+
310+
with open("robots.txt", "w", encoding="utf-8") as f:
311+
f.write(robots_content)
312+
313+
print("Robots.txt generated: robots.txt")
314+
315+
316+
# 修改主函数
257317
if __name__ == "__main__":
258-
input_file = "README.md"# 替换为你的 Markdown 文件路径
318+
input_file = "README.md"
259319
output_file = "index.html"
260320

261321
commands = parse_markdown(input_file)
262322
html_content = render_html(commands)
263323

324+
# 先运行 prettier 只格式化 HTML
264325
with open(output_file, "w", encoding="utf-8") as file:
265326
file.write(html_content)
266-
327+
os.system(f"prettier --write *.html")
267328
print(f"HTML file generated: {output_file}")
268329

269-
os.system(f"prettier --write *")
330+
# 然后生成 sitemap.xml 和 robots.txt
331+
generate_sitemap()
332+
generate_robots_txt()

‎index.html

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!doctype html>
1+
<!DOCTYPE html>
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8" />
@@ -153,7 +153,7 @@
153153
(navigator.userAgent.toLowerCase().indexOf("mac") != -1
154154
? "Command/Cmd"
155155
: "CTRL") +
156-
" + D to bookmark this page.",
156+
" + D to bookmark this page."
157157
);
158158
}
159159
}
@@ -166,13 +166,11 @@ <h1 class="text-4xl font-extrabold">Linux Commands Cheat Sheet</h1>
166166
<p class="mt-2 text-lg">
167167
A clean and minimal guide to 491 Linux commands
168168
</p>
169-
<a href="https://labex.io">
170-
<img
171-
src="assets/labex-logo-white.svg"
172-
alt="LabEx Logo"
173-
class="mx-auto mt-8"
174-
/>
175-
</a>
169+
<img
170+
src="assets/labex-logo-white.svg"
171+
alt="LabEx Logo"
172+
class="mx-auto mt-8"
173+
/>
176174
</div>
177175
</header>
178176
<nav>

‎robots.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
User-agent: *
2+
Allow: /
3+
4+
Sitemap: https://linux-commands.labex.io/sitemap.xml

0 commit comments

Comments
(0)

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