Skip to content

Commit 07a1f4d

Browse files
committed
feat: add stay on page plugin
1 parent af86e91 commit 07a1f4d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

hooks/stay_on_page.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import re
2+
3+
4+
def on_post_page(output, page, config):
5+
"""
6+
<ul class="md-select__list">
7+
8+
<li class="md-select__item">
9+
<a href="/leetcode/en/" hreflang="en" class="md-select__link">
10+
English
11+
</a>
12+
</li>
13+
14+
<li class="md-select__item">
15+
<a href="/leetcode/" hreflang="zh" class="md-select__link">
16+
中文
17+
</a>
18+
</li>
19+
20+
</ul>
21+
"""
22+
23+
url = page.abs_url
24+
cn_url = url.replace("/leetcode/en/", "/leetcode/")
25+
en_url = cn_url.replace("/leetcode/", "/leetcode/en/")
26+
support_en_lang = not cn_url.startswith("/leetcode/lcof") and not cn_url.startswith(
27+
"/leetcode/lcof2"
28+
)
29+
patterns = {
30+
"zh": r'(<a\s+[^>]*href=["\'])[^"\']*(["\'][^>]*hreflang=["\']zh["\'][^>]*>)',
31+
"en": r'(<a\s+[^>]*href=["\'])[^"\']*(["\'][^>]*hreflang=["\']en["\'][^>]*>)',
32+
}
33+
output = re.sub(patterns["zh"], r"\1" + cn_url + r"\2", output)
34+
if support_en_lang:
35+
output = re.sub(patterns["en"], r"\1" + en_url + r"\2", output)
36+
return output

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ hooks:
8686
- hooks/fix_markdown_block.py
8787
- hooks/ext_info.py
8888
- hooks/tags.py
89+
- hooks/stay_on_page.py
8990

9091
markdown_extensions:
9192
- admonition

0 commit comments

Comments
 (0)