Skip to content

Commit fe7de53

Browse files
TableOfContents
1 parent a806294 commit fe7de53

5 files changed

Lines changed: 39 additions & 23 deletions

File tree

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ A starting point can be as simple as...
3838
<head>
3939
<meta charset="utf-8">
4040
<meta name="viewport" content="width=device-width, initial-scale=1.0">
41-
<title>{{ page.title }}</title>
41+
<title>{{ page.toc.title }}</title>
4242
<link rel="stylesheet" href="{{ '/css/default.css' | url }}">
4343
</head>
4444
<body>

src/mkdocs/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from .__version__ import __title__, __version__
2-
from .mkdocs import get_current_page, get_site_index, Page, Static, SiteIndex, MkDocs, cli
2+
from .mkdocs import get_current_page, get_site, Page, Static, Site, MkDocs, cli
33

44

55
__all__ = [
66
'__title__',
77
'__version__',
88
'get_current_page',
9-
'get_site_index',
9+
'get_site',
1010
'Page',
1111
'Static',
12-
'SiteIndex',
12+
'Site',
1313
'MkDocs',
1414
'cli',
1515
]

src/mkdocs/extensions/relative_urls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class URLProcessor(markdown.treeprocessors.Treeprocessor):
99
def run(self, root):
1010
page = mkdocs.get_current_page()
11-
site_index = mkdocs.get_site_index()
11+
site = mkdocs.get_site()
1212
key = ''
1313
link = ''
1414

@@ -34,7 +34,7 @@ def run(self, root):
3434
path_from = page.path
3535
path_to = os.path.normpath(path_from.parent.joinpath(url.path))
3636

37-
target = site_index.lookup.get(path_to)
37+
target = site.lookup.get(path_to)
3838
if target is None:
3939
continue # Broken link!
4040

src/mkdocs/mkdocs.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def get_current_page():
3030
return ctx
3131

3232

33-
def get_site_index():
33+
def get_site():
3434
ctx = _site_index.get()
3535
if ctx is None:
3636
raise RuntimeError("No current context")
@@ -52,23 +52,14 @@ def __init__(self, path):
5252
self.url = str(url).removesuffix('index.html')
5353

5454

55-
class PageContext:
56-
def __init__(self, page, title, html, toc):
57-
self.path = page.path
58-
self.url = page.url
59-
self.title = title
60-
self.html = html
61-
self.toc = toc
62-
63-
6455
class Static:
6556
def __init__(self, path):
6657
self.path = path
6758
url = pathlib.PosixPath('/').joinpath(self.path)
6859
self.url = str(url).removesuffix('index.html')
6960

7061

71-
class SiteIndex:
62+
class Site:
7263
def __init__(self, pages, statics):
7364
self._pages = pages
7465
self._statics = statics
@@ -92,6 +83,31 @@ def __len__(self) -> int:
9283
return len(self.pages) + len(self.statics)
9384

9485

86+
class TableOfContents:
87+
def __init__(self, md):
88+
self.html = md.toc
89+
self.title = md.toc_tokens[0]['name'] if md.toc_tokens else ''
90+
self._items = list(md.toc_tokens)
91+
92+
def __bool__(self):
93+
return bool(self._items)
94+
95+
96+
class PageContext:
97+
def __init__(self, page, text, html, toc):
98+
self.path = page.path
99+
self.url = page.url
100+
self.text = text
101+
self.html = html
102+
self.toc = toc
103+
104+
# Leaving this here for compatability with... `{{ page.title }}`
105+
# TODO.... probably deprecate, but follow through with toc design discussion.
106+
@property
107+
def title(self):
108+
return self.toc.title
109+
110+
95111
class MkDocs:
96112
def __init__(self, input_dir):
97113
self.site_index = self.load_site(input_dir)
@@ -122,7 +138,7 @@ def load_site(self, input_dir):
122138

123139
pages = sorted(pages, key=lambda x: x.url)
124140
statics = sorted(statics, key=lambda x: x.url)
125-
return SiteIndex(pages, statics)
141+
return Site(pages, statics)
126142

127143
def init_env(self, input_dir) -> jinja2.Environment:
128144
@jinja2.pass_context
@@ -182,8 +198,8 @@ def build(self, input, output):
182198
with self.set_context(page):
183199
text = input_path.read_text()
184200
html = self.md.reset().convert(text)
185-
title = self.md.toc_tokens[0]['name'] if self.md.toc_tokens else ''
186-
page_ctx = PageContext(page=page, title=title, html=html, toc=self.md.toc)
201+
toc = TableOfContents(self.md)
202+
page_ctx = PageContext(page=page, text=text, html=html, toc=toc)
187203
output = self.base.render(page=page_ctx)
188204

189205
output_path.parent.mkdir(parents=True, exist_ok=True)
@@ -215,8 +231,8 @@ def app(request):
215231
with self.set_context(resource):
216232
text = input_path.read_text()
217233
html = self.md.reset().convert(text)
218-
title = self.md.toc_tokens[0]['name'] if self.md.toc_tokens else ''
219-
page_ctx = PageContext(page=resource, title=title, html=html, toc=self.md.toc)
234+
toc = TableOfContents(self.md)
235+
page_ctx = PageContext(page=resource, text=text, html=html, toc=toc)
220236
output = self.base.render(page=page_ctx)
221237
return httpx.Response(200, content=httpx.HTML(output))
222238
elif isinstance(resource, Static):

src/mkdocs/theme/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@
212212
<body>
213213
{% if page.toc %}
214214
<nav class="toc">
215-
{{ page.toc }}
215+
{{ page.toc.html }}
216216
</nav>
217217
{% endif %}
218218
<main>

0 commit comments

Comments
 (0)