Skip to content

Commit 5561afb

Browse files
Support nested elements
1 parent 75565a2 commit 5561afb

3 files changed

Lines changed: 48 additions & 6 deletions

File tree

docs/navigation.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,28 @@ nav = [
5252
]
5353
```
5454

55-
This allows the theme to display navigation controls, as well as including `← previous` and `next →` links.
55+
This allows the theme to display navigation controls, as well as including `← previous` and `next →` links.
56+
57+
The navigation configuration can also include nested elements.
58+
59+
```
60+
[mkdocs]
61+
version = 2
62+
63+
[site]
64+
title = "MkDocs"
65+
favicon = "📘"
66+
nav = [
67+
{title="Introduction", path="index.md"},
68+
{title="Tutorial", children=[
69+
{title="Creating a project", path="tutorial/new.md"},
70+
{title="Adding pages", path="tutorial/pages.md"},
71+
{title="Publishing your work", path="tutorial/publish.md"},
72+
]},
73+
{title="Topics", children=[
74+
{title="Page layouts", path="topics/layouts.md"},
75+
{title="Typography", path="topics/typography.md"},
76+
{title="Color schemes", path="topics/schemes.md"},
77+
]}
78+
]
79+
```

src/mkdocs/mkdocs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,11 @@ def nav_lines(self, nav: dict, indent="") -> list[str]:
289289
def render(self, resource: Resource, resources: list[Resource], config: dict, env: jinja2.Environment, md: markdown.Markdown) -> bytes:
290290
if resource.path.suffix == '.md':
291291
mapping = {resource.path: resource.url for resource in resources}
292-
with PageContext(resource.path, mapping) as page:
292+
with PageContext(resource.path, mapping, relative=False) as page:
293293
nav_lines = self.nav_lines(config['site']['nav'])
294294
nav_text = '\n'.join(nav_lines)
295295
nav_html = md.reset().convert(nav_text)
296-
with PageContext(resource.path, mapping):
296+
with PageContext(resource.path, mapping, relative=True):
297297
page_text = resource.read().decode('utf-8')
298298
page_html = md.reset().convert(page_text)
299299

src/mkdocs/rewrite_urls.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import contextvars
22
import markdown
3-
import posixpath
43
import httpx
4+
import os
55
import pathlib
6+
import posixpath
67

78

89
page_context = contextvars.ContextVar('page_context')
910

1011

12+
def joinpath(x: pathlib.Path, y: pathlib.Path):
13+
return pathlib.Path(os.path.normpath(x.joinpath(y)))
14+
1115
class PageContext:
12-
def __init__(self, path: pathlib.Path, path_to_url: dict[pathlib.Path, str]):
16+
def __init__(self, path: pathlib.Path, path_to_url: dict[pathlib.Path, str], relative: bool):
1317
self.path = path
1418
self.path_to_url = path_to_url
1519
self.url_to_path = {u: p for p, u in path_to_url.items()}
20+
self.relative = relative
1621

1722
def __enter__(self):
1823
self._token = page_context.set(self)
@@ -31,6 +36,7 @@ def run(self, root):
3136
link = ''
3237

3338
for el in root.iter():
39+
# We want to rewrite image and links.
3440
if el.tag == 'a':
3541
key = 'href'
3642
link = el.get(key)
@@ -43,11 +49,21 @@ def run(self, root):
4349

4450
if link:
4551
url = httpx.URL(link)
52+
# We want to rewrite relative links... '/page'
53+
# We don't want to rewrite external links. 'https://elsewhere.com/here'
54+
# We don't want to rewrite anchor links. '#section'
4655
if url.is_relative_url and url._uri_reference.path:
56+
link = pathlib.PosixPath(url.path)
57+
58+
# Path to the current page...
4759
from_path = ctx.path
48-
to_path = ctx.path.parent.joinpath(url.path)
60+
# Path to the linked page...
61+
to_path = joinpath(ctx.path.parent, link) if ctx.relative else link
62+
# Current URL...
4963
from_url = ctx.path_to_url[from_path]
64+
# Linked URL...
5065
to_url = ctx.path_to_url.get(to_path)
66+
5167
if to_url is None:
5268
continue
5369
rewrite = posixpath.relpath(to_url, from_url)
@@ -60,10 +76,12 @@ def run(self, root):
6076
el.set(key, rewrite)
6177
links.append({"title": el.text, "url": rewrite})
6278

79+
# Is this is a link to the current page?
6380
if from_url == to_url:
6481
el.set('class', 'active')
6582
idx = len(links)
6683

84+
# We also want to track the previous and next link.
6785
links = [None] + links + [None]
6886
if idx is not None:
6987
ctx.previous = links[idx - 1]

0 commit comments

Comments
 (0)