1
1
from bs4 import BeautifulSoup
2
2
3
3
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
-
10
4
def on_page_content (html , page , config , files ):
11
5
# 修改 tags 页每个 tag 的 id 属性值
12
- is_cn_tag_page = page .abs_url == ' /leetcode/tags/'
6
+ is_cn_tag_page = page .abs_url == " /leetcode/tags/"
13
7
if is_cn_tag_page :
14
8
soup = BeautifulSoup (html , "html.parser" )
15
9
h2_tags = soup .find_all ("h2" )
@@ -21,28 +15,28 @@ def on_page_content(html, page, config, files):
21
15
ul_tags = soup .find_all ("ul" )
22
16
for ul_tag in ul_tags :
23
17
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 ()))
25
19
ul_tag .clear ()
26
20
for li_tag in li_tags :
27
21
ul_tag .append (li_tag )
28
22
html = str (soup )
29
-
23
+
30
24
return html
31
25
32
26
33
27
def on_page_context (context , page , config , nav ):
34
28
# 修改每个问题顶部 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
37
31
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" ]} '
40
34
return context
41
35
42
36
43
37
def on_post_page (output , page , config ):
44
38
# 修改 tags 页的左侧导航链接
45
- is_cn_tag_page = page .abs_url == ' /leetcode/tags/'
39
+ is_cn_tag_page = page .abs_url == " /leetcode/tags/"
46
40
if is_cn_tag_page :
47
41
soup = BeautifulSoup (output , "html.parser" )
48
42
nav_tag = soup .find ("nav" , class_ = "md-nav md-nav--secondary" )
@@ -57,4 +51,4 @@ def on_post_page(output, page, config):
57
51
tag_name = span_tag .text .strip ()
58
52
a_tag ["href" ] = f"#{ tag_name } "
59
53
output = str (soup )
60
- return output
54
+ return output
0 commit comments