Skip to content

Commit 24bc10c

Browse files
Navigation support bugfix (#54)
1 parent fa1983b commit 24bc10c

6 files changed

Lines changed: 26 additions & 10 deletions

File tree

‎docs/CHANGELOG.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
## 2.0.dev5
2+
3+
**Navigation Support Bugfix**
4+
5+
Use relative URLs for site navigation. Ensures that sites hosted on a subpath still link correctly. [#54](https://github.com/encode/mkdocs/pull/54)
6+
17
## 2.0.dev4
28

39
**Navigation Support**
410

5-
Preliminary support for site navigation.
6-
7-
See [#49](https://github.com/encode/mkdocs/pull/49) for further details.
11+
Preliminary support for site navigation. [#49](https://github.com/encode/mkdocs/pull/49)

‎docs/templates/base.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</head>
1414
<body>
1515
<nav class="site">
16-
{{ nav.html }}
16+
{% include "navigation.html" %}
1717
</nav>
1818
<nav class="toc">
1919
{{ page.toc.html }}

‎docs/templates/navigation.html‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<ul>
2+
{% for item in nav %}<li><a href="{{ item.page.url | url }}"{% if item.page.url == page.url %} class="active"{% endif %}>{{ item.title }}</a></li>{% endfor %}
3+
</ul>

‎src/mkdocs/__version__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
__title__ = "mkdocs"
2-
__version__ = "2.0.dev4"
2+
__version__ = "2.0.dev5"

‎src/mkdocs/extensions/relative_urls.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ def run(self, root):
4040

4141
url_from = page.url
4242
url_to = target.url
43-
rewrite = posixpath.relpath(url_to, url_from)
44-
if url_to.endswith('/') and rewrite != '.':
45-
rewrite += '/'
43+
rewrite = mkdocs.link_to(url_from, url_to)
4644
if url.query:
4745
rewrite += f'?{url.query}'
4846
if url.fragment:

‎src/mkdocs/mkdocs.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ def get_site():
3737
return ctx
3838

3939

40+
def link_to(path_from, path_to):
41+
if path_from == path_to:
42+
return '.'
43+
p0 = path_from.split('/')
44+
p1 = path_to.split('/')
45+
for idx, pair in enumerate(zip(p0, p1)):
46+
if pair[0] != pair[1]:
47+
break
48+
p = ['..' for i in p0[idx+1:]] + p1[idx:]
49+
return '/'.join(p)
50+
51+
4052
class Page:
4153
def __init__(self, path):
4254
self.path = path
@@ -194,8 +206,7 @@ def init_env(self, input_dir) -> jinja2.Environment:
194206
@jinja2.pass_context
195207
def url(ctx, url_to):
196208
url_from = ctx['page'].url
197-
url_rel = posixpath.relpath(url_to, url_from) # This isn't correct
198-
return url_rel
209+
return link_to(url_from, url_to)
199210

200211
dir = pathlib.Path(input_dir)
201212
loader = jinja2.ChoiceLoader([

0 commit comments

Comments
 (0)