Skip to content

Commit 80772f6

Browse files
committed
feat: add hooks script
1 parent 33641f2 commit 80772f6

File tree

7 files changed

+81
-3
lines changed

7 files changed

+81
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ node_modules
33
.cache
44
.vscode
55
solution
6-
.DS_Store
6+
.DS_Store
7+
__pycache__

docs-en/lc/1.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
---
2+
comments: true
3+
tags:
4+
- Array
5+
- Hash Table
6+
edit_url: 'https://github.com/doocs/leetcode/edit/main/solution/0000-0099/0001.Two%20Sum/README_EN.md'
7+
---
8+
9+
110
# [1. Two Sum](https://leetcode.com/problems/two-sum)
211

312

docs/lc/1.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
---
22
comments: true
3+
tags:
4+
- 数组
5+
- 哈希表
6+
- 排序
7+
- 暴力
8+
- 模拟
9+
- 枚举
10+
- 双指针
311
edit_url: 'https://github.com/doocs/leetcode/edit/main/solution/0000-0099/0001.Two%20Sum/README.md'
412
---
513

docs/lc/2.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
---
22
comments: true
3+
tags:
4+
- 模拟
5+
- 堆(优先队列)
36
edit_url: 'https://github.com/doocs/leetcode/edit/main/solution/0000-0099/0002.Add%20Two%20Numbers/README.md'
47
---
58

hooks.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,60 @@
1-
# https://www.mkdocs.org/dev-guide/plugins/#events
1+
from bs4 import BeautifulSoup
22

33

4+
# https://www.mkdocs.org/dev-guide/plugins/#events
45
def on_page_markdown(markdown, page, config, files):
56
page_edit_url = page.meta.get("edit_url")
67
page.edit_url = str(page_edit_url) if page_edit_url else None
8+
9+
10+
def on_page_content(html, page, config, files):
11+
# 修改 tags 页每个 tag 的 id 属性值
12+
is_cn_tag_page = page.abs_url == '/leetcode/tags/'
13+
if is_cn_tag_page:
14+
soup = BeautifulSoup(html, "html.parser")
15+
h2_tags = soup.find_all("h2")
16+
for tag in h2_tags:
17+
span_tag = tag.find("span", class_="md-tag")
18+
if span_tag:
19+
tag["id"] = span_tag.text.strip()
20+
# 重新排序每个 tag 下的问题列表
21+
ul_tags = soup.find_all("ul")
22+
for ul_tag in ul_tags:
23+
li_tags = ul_tag.find_all("li")
24+
li_tags.sort(key=lambda x: int(x.text.split('.')[0].strip()))
25+
ul_tag.clear()
26+
for li_tag in li_tags:
27+
ul_tag.append(li_tag)
28+
html = str(soup)
29+
30+
return html
31+
32+
33+
def on_page_context(context, page, config, nav):
34+
# 修改每个问题顶部 tags 的跳转链接
35+
is_cn_problem_page = (page.edit_url or '').endswith('README.md')
36+
has_tag = 'tags' in context
37+
if is_cn_problem_page and has_tag:
38+
for tag in context['tags']:
39+
tag['url'] = f'tags/#{tag["name"]}'
40+
return context
41+
42+
43+
def on_post_page(output, page, config):
44+
# 修改 tags 页的左侧导航链接
45+
is_cn_tag_page = page.abs_url == '/leetcode/tags/'
46+
if is_cn_tag_page:
47+
soup = BeautifulSoup(output, "html.parser")
48+
nav_tag = soup.find("nav", class_="md-nav md-nav--secondary")
49+
if nav_tag:
50+
ul_tag = nav_tag.find("ul", class_="md-nav__list")
51+
if ul_tag:
52+
for li_tag in ul_tag.find_all("li", class_="md-nav__item"):
53+
a_tag = li_tag.find("a", class_="md-nav__link")
54+
if a_tag:
55+
span_tag = a_tag.find("span", class_="md-ellipsis")
56+
if span_tag:
57+
tag_name = span_tag.text.strip()
58+
a_tag["href"] = f"#{tag_name}"
59+
output = str(soup)
60+
return output

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,5 @@ nav:
431431
- 17.24. 最大子矩阵: lcci/17.24.md
432432
- 17.25. 单词矩阵: lcci/17.25.md
433433
- 17.26. 稀疏相似度: lcci/17.26.md
434+
435+
- 专项训练: tags.md

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ urllib3==1.26.18
55
mkdocs-git-committers-plugin-2==2.3.0
66
mkdocs-material==9.5.13
77
mkdocs-glightbox==0.3.7
8-
jieba==0.42.1
8+
jieba==0.42.1
9+
beautifulsoup4==4.12.0

0 commit comments

Comments
 (0)