Skip to content

Commit 1205746

Browse files
committed
chore: update ext info plugin
1 parent a167775 commit 1205746

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

docs/lc/2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ tags:
44
- 模拟
55
- 堆(优先队列)
66
edit_url: 'https://github.com/doocs/leetcode/edit/main/solution/0000-0099/0002.Add%20Two%20Numbers/README.md'
7+
difficulty: 中等
78
rating: 1345
89
---
910

docs/stylesheets/extra.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.rating {
2+
display: flex;
3+
gap: .5em;
4+
}
5+
6+
.rating-item {
7+
float: left;
8+
padding: 8px 12px;
9+
border-radius: 2.4rem;
10+
font-size: min(.8em,.64rem);
11+
background-color: #00000012;
12+
}

hooks/ext_info.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11
from mkdocs import plugins
22

3+
from bs4 import BeautifulSoup
4+
35

46
# https://www.mkdocs.org/dev-guide/plugins/#events
57

8+
colors = {
9+
"简单": "#46c6c2",
10+
"中等": "#fac31d",
11+
"困难": "#f8615c",
12+
"Easy": "#46c6c2",
13+
"Medium": "#fac31d",
14+
"Hard": "#f8615c",
15+
}
16+
617

718
@plugins.event_priority(90)
819
def on_post_page(output, page, config):
9-
difficulty = page.meta.get('difficulty')
10-
rating = page.meta.get('rating')
11-
12-
# 在 h1 标题之后插入难度和评分信息及其样式
20+
is_cn_problem_page = (page.edit_url or "").endswith("README.md")
21+
difficulty = page.meta.get("difficulty")
22+
rating = page.meta.get("rating")
23+
if not difficulty and not rating:
24+
return output
25+
if rating:
26+
rating = f"难度分 {rating}" if is_cn_problem_page else f"Rating {rating}"
1327
html = str(output)
14-
# if difficulty:
15-
# # 找到 h1 标签的结束位置
16-
# h1_end = html.find('</h1>') + 5
17-
# # 在 h1 标签之后插入难度信息
18-
# html = html[:h1_end] + f'<p>Difficulty: {difficulty}</p>' + html[h1_end:]
19-
# if rating:
20-
# # 找到 h1 标签的结束位置
21-
# h1_end = html.find('</h1>') + 5
22-
# # 在 h1 标签之后插入评分信息
23-
# html = html[:h1_end] + f'<p>Rating: {rating}</p>' + html[h1_end:]
24-
return html
28+
soup = BeautifulSoup(html, "html.parser")
29+
div = soup.new_tag("div", attrs={"class": "rating"})
30+
soup.select_one("h1").insert_after(div)
31+
color = colors.get(difficulty) or "black"
32+
for s in (difficulty, rating):
33+
if not s:
34+
continue
35+
sub = soup.new_tag("div", attrs={"class": "rating-item"})
36+
sub.string = str(s)
37+
sub["style"] = f"color: {color};"
38+
div.append(sub)
39+
return str(soup)

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ hooks:
8686
- hooks/tags.py
8787
- hooks/committer.py
8888
- hooks/fix_markdown_block.py
89-
- hooks/ext_info.py
89+
# - hooks/ext_info.py
9090

9191
markdown_extensions:
9292
- admonition

0 commit comments

Comments
 (0)