Skip to content

Commit 71d2c31

Browse files
committed
Hook to make edit links point to English version
1 parent bd2a82a commit 71d2c31

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ venv
88
site
99
site/
1010
.env
11+
*.pyc

overrides/hooks.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
"""
55

66
import yaml
7+
import re
8+
import logging
79
from pathlib import Path
810

11+
log = logging.getLogger(f"mkdocs.plugins.hooks")
12+
913

1014
def load_translations(translation_file):
1115
"""Load translations from YAML file."""
@@ -18,3 +22,23 @@ def on_env(env, config, files, **kwargs):
1822
"""MkDocs hook to inject translations into home.html environment."""
1923
env.globals['home_translations'] = load_translations('home.translations.yml')
2024
return env
25+
26+
27+
def on_page_context(context, page, config, nav, **kwargs):
28+
"""
29+
MkDocs hook to force edit URLs to always point to English version.
30+
Since translations are auto-generated, all edits should be made to /en/ files.
31+
32+
The Material theme uses page.edit_url to generate the edit button.
33+
We modify it to always point to /docs/en/ regardless of the current language.
34+
"""
35+
if page and hasattr(page, 'edit_url') and page.edit_url:
36+
original_url = page.edit_url
37+
# Replace any language code with 'en' in the edit URL
38+
# Pattern: /docs/{language}/ -> /docs/en/
39+
new_url = re.sub(r'/docs/[a-z]{2}/', '/docs/en/', original_url)
40+
41+
if original_url != new_url:
42+
page.edit_url = new_url
43+
44+
return context

0 commit comments

Comments
 (0)