Skip to content

Commit f5c4e16

Browse files
committed
feat: split hooks script
1 parent 80772f6 commit f5c4e16

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

hooks/edit_url.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from mkdocs import plugins
2+
3+
4+
# https://www.mkdocs.org/dev-guide/plugins/#events
5+
6+
7+
@plugins.event_priority(100)
8+
def on_page_markdown(markdown, page, config, files):
9+
page_edit_url = page.meta.get("edit_url")
10+
page.edit_url = str(page_edit_url) if page_edit_url else None

hooks.py renamed to hooks/tags.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
from bs4 import BeautifulSoup
22

33

4-
# https://www.mkdocs.org/dev-guide/plugins/#events
5-
def on_page_markdown(markdown, page, config, files):
6-
page_edit_url = page.meta.get("edit_url")
7-
page.edit_url = str(page_edit_url) if page_edit_url else None
8-
9-
104
def on_page_content(html, page, config, files):
115
# 修改 tags 页每个 tag 的 id 属性值
12-
is_cn_tag_page = page.abs_url == '/leetcode/tags/'
6+
is_cn_tag_page = page.abs_url == "/leetcode/tags/"
137
if is_cn_tag_page:
148
soup = BeautifulSoup(html, "html.parser")
159
h2_tags = soup.find_all("h2")
@@ -21,28 +15,28 @@ def on_page_content(html, page, config, files):
2115
ul_tags = soup.find_all("ul")
2216
for ul_tag in ul_tags:
2317
li_tags = ul_tag.find_all("li")
24-
li_tags.sort(key=lambda x: int(x.text.split('.')[0].strip()))
18+
li_tags.sort(key=lambda x: int(x.text.split(".")[0].strip()))
2519
ul_tag.clear()
2620
for li_tag in li_tags:
2721
ul_tag.append(li_tag)
2822
html = str(soup)
29-
23+
3024
return html
3125

3226

3327
def on_page_context(context, page, config, nav):
3428
# 修改每个问题顶部 tags 的跳转链接
35-
is_cn_problem_page = (page.edit_url or '').endswith('README.md')
36-
has_tag = 'tags' in context
29+
is_cn_problem_page = (page.edit_url or "").endswith("README.md")
30+
has_tag = "tags" in context
3731
if is_cn_problem_page and has_tag:
38-
for tag in context['tags']:
39-
tag['url'] = f'tags/#{tag["name"]}'
32+
for tag in context["tags"]:
33+
tag["url"] = f'tags/#{tag["name"]}'
4034
return context
4135

4236

4337
def on_post_page(output, page, config):
4438
# 修改 tags 页的左侧导航链接
45-
is_cn_tag_page = page.abs_url == '/leetcode/tags/'
39+
is_cn_tag_page = page.abs_url == "/leetcode/tags/"
4640
if is_cn_tag_page:
4741
soup = BeautifulSoup(output, "html.parser")
4842
nav_tag = soup.find("nav", class_="md-nav md-nav--secondary")
@@ -57,4 +51,4 @@ def on_post_page(output, page, config):
5751
tag_name = span_tag.text.strip()
5852
a_tag["href"] = f"#{tag_name}"
5953
output = str(soup)
60-
return output
54+
return output

mkdocs.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ plugins:
8181
shadow: true
8282

8383
hooks:
84-
- hooks.py
84+
- hooks/edit_url.py
85+
- hooks/tags.py
8586

8687
markdown_extensions:
8788
- pymdownx.superfences

0 commit comments

Comments
 (0)