Skip to content

Commit 486a44b

Browse files
authored
Edit Page Button (#20)
1 parent 9f79de0 commit 486a44b

File tree

5 files changed

+109
-1
lines changed

5 files changed

+109
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ mike deploy 0.3 'experimental' -t '0.3: Experimental development' --push
8080
```
8181

8282
Note the format for the title (-t). Keep a consistent naming structure. Also note that this version is aliased as `experimental`.
83+
84+
Also, edit the edit_uri in mkdocs.yml to have the new version as a branch name.
85+
8386
## Adding a New Submodule Repo
8487
```commandline
8588
cd ~/repos/hello-robot.github.io/repos

mkdocs.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ site_description: "Hello Robot User Documentation"
44
copyright: 'Copyright © 2020-2025 Hello Robot Inc'
55
site_author: Hello Robot Inc
66
use_directory_urls: True
7-
edit_uri: ''
7+
repo_url: https://github.com/hello-robot/hello-robot.github.io
8+
edit_uri: blob/0.3/docs
89

910
theme:
1011
name: material
@@ -18,6 +19,7 @@ theme:
1819
- scheme: default
1920
primary: hello-robot-light
2021
features:
22+
- content.action.edit
2123
- navigation.footer
2224

2325
extra_css:
@@ -52,6 +54,9 @@ plugins:
5254
- glightbox-skip
5355
- include-markdown
5456

57+
hooks:
58+
- plugins/edit_links_across_submodules.py
59+
5560
extra:
5661
version:
5762
provider: mike
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
Replaces the edit_url for documentation included in other submodules.
3+
4+
For example, the file robot_parameters.md [1] is included using the submodule located at repos/stretch_body
5+
6+
When edit_uri is defined in the mkdocs.yml, the edit link for that page is
7+
https://github.com/hello-robot/hello-robot.github.io/blob/0.3/repos/stretch_body/docs/robot_parameters.md
8+
which does not link properly.
9+
10+
The code contained in this module is inspired by the submodule-edit-uri plugin [2]
11+
and uses the mkdocs hooks [3] functionality.
12+
13+
It reads the submodules from `.gitmodules` using GitPython during the configuration step,
14+
and then substitutes the proper url in the full output.
15+
16+
The resulting link is now correct, linking to the full github blob url [1]
17+
18+
[1] https://github.com/hello-robot/stretch_body/blob/master/docs/robot_parameters.md
19+
[2] https://github.com/sondregronas/mkdocs-submodule-edit-uri
20+
[3] https://www.mkdocs.org/user-guide/configuration/#hooks
21+
22+
23+
"""
24+
from git import Repo
25+
26+
27+
def urljoin(*a):
28+
s = ''
29+
for sub in a:
30+
if s and not s.endswith('/'):
31+
s += '/'
32+
s += sub
33+
return s
34+
35+
36+
def on_config(config):
37+
submod_d = {}
38+
config['submodules'] = submod_d
39+
repo = Repo('.')
40+
for submod in repo.submodules:
41+
url = submod.url
42+
if url.endswith('.git'):
43+
url = url[:-4]
44+
submod_d[submod.name] = {
45+
'path': submod.path,
46+
'url': url,
47+
'branch': submod.branch.name,
48+
}
49+
50+
51+
def on_post_page(output, page, config):
52+
# Assume that the edit uri is (blob|edit)/(branch_name)/docs/
53+
edit_pieces = config['edit_uri'].split('/')
54+
assert len(edit_pieces) == 4, config['edit_uri']
55+
verb = edit_pieces[0]
56+
branch = edit_pieces[1]
57+
58+
base = urljoin(config['repo_url'], verb, branch)
59+
60+
for name, submod in config['submodules'].items():
61+
old_path = urljoin(base, submod['path'])
62+
new_path = urljoin(submod['url'], verb, submod['branch'])
63+
output = output.replace(old_path, new_path)
64+
return output

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ description = "This repository generates the documentation hosted at [docs.hello
55
readme = "README.md"
66
requires-python = ">=3.10"
77
dependencies = [
8+
"GitPython>3.1",
89
"jinja2==3.0.3",
910
"mike>=2.1.3",
1011
"mkdocs>=1.6.1",

uv.lock

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)