Skip to content

Commit f22f474

Browse files
committed
Support overriding edit_uri on a File
1 parent 80ffca5 commit f22f474

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

mkdocs/structure/files.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ def __repr__(self):
247247
f"dest_uri={self.dest_uri!r}, inclusion={self.inclusion})"
248248
)
249249

250+
@utils.weak_property
251+
def edit_uri(self) -> str | None:
252+
return self.src_uri if self.generated_by is None else None
253+
250254
def _get_stem(self) -> str:
251255
"""Soft-deprecated, do not use."""
252256
filename = posixpath.basename(self.src_uri)

mkdocs/structure/pages.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,28 +176,33 @@ def _set_edit_url(
176176
if not edit_uri_template and not edit_uri:
177177
self.edit_url = None
178178
return
179-
src_uri = self.file.src_uri
179+
src_uri = self.file.edit_uri
180+
if src_uri is None:
181+
self.edit_url = None
182+
return
183+
180184
if edit_uri_template:
181185
noext = posixpath.splitext(src_uri)[0]
182-
edit_uri = edit_uri_template.format(path=src_uri, path_noext=noext)
186+
file_edit_uri = edit_uri_template.format(path=src_uri, path_noext=noext)
183187
else:
184188
assert edit_uri is not None and edit_uri.endswith('/')
185-
edit_uri += src_uri
189+
file_edit_uri = edit_uri + src_uri
190+
186191
if repo_url:
187192
# Ensure urljoin behavior is correct
188-
if not edit_uri.startswith(('?', '#')) and not repo_url.endswith('/'):
193+
if not file_edit_uri.startswith(('?', '#')) and not repo_url.endswith('/'):
189194
repo_url += '/'
190195
else:
191196
try:
192-
parsed_url = urlsplit(edit_uri)
197+
parsed_url = urlsplit(file_edit_uri)
193198
if not parsed_url.scheme or not parsed_url.netloc:
194199
log.warning(
195-
f"edit_uri: {edit_uri!r} is not a valid URL, it should include the http:// (scheme)"
200+
f"edit_uri: {file_edit_uri!r} is not a valid URL, it should include the http:// (scheme)"
196201
)
197202
except ValueError as e:
198-
log.warning(f"edit_uri: {edit_uri!r} is not a valid URL: {e}")
203+
log.warning(f"edit_uri: {file_edit_uri!r} is not a valid URL: {e}")
199204

200-
self.edit_url = urljoin(repo_url or '', edit_uri)
205+
self.edit_url = urljoin(repo_url or '', file_edit_uri)
201206

202207
def read_source(self, config: MkDocsConfig) -> None:
203208
source = config.plugins.on_page_read_source(page=self, config=config)

mkdocs/tests/structure/page_tests.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,26 @@ def test_page_edit_url_warning(self):
677677
self.assertEqual(pg.edit_url, case['edit_url'])
678678
self.assertEqual(cm.output, [case['warning']])
679679

680+
def test_page_edit_url_custom_from_file(self):
681+
for case in [
682+
dict(
683+
edit_uri='hooks.py',
684+
expected_edit_url='https://github.com/mkdocs/mkdocs/edit/master/docs/hooks.py',
685+
),
686+
dict(
687+
edit_uri='../scripts/hooks.py',
688+
expected_edit_url='https://github.com/mkdocs/mkdocs/edit/master/scripts/hooks.py',
689+
),
690+
dict(edit_uri=None, expected_edit_url=None),
691+
]:
692+
with self.subTest(case['edit_uri']):
693+
cfg = load_config(repo_url='https://github.com/mkdocs/mkdocs')
694+
fl = File('testing.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls)
695+
fl.edit_uri = case['edit_uri']
696+
pg = Page('Foo', fl, cfg)
697+
self.assertEqual(pg.url, 'testing/')
698+
self.assertEqual(pg.edit_url, case['expected_edit_url'])
699+
680700
def test_page_render(self):
681701
cfg = load_config()
682702
fl = File('testing.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls)

0 commit comments

Comments
 (0)