Skip to content

Commit 289a082

Browse files
authored
Merge pull request #220 from hughrun/toc
Table of Contents
2 parents f1aa12c + d485ff8 commit 289a082

File tree

5 files changed

+57
-21
lines changed

5 files changed

+57
-21
lines changed

generate.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66

77
from jinja2 import Environment, FileSystemLoader
8-
from markdown import markdown
8+
from markdown import Markdown
99
import yaml
1010

1111
import i18n
@@ -103,6 +103,15 @@ def format_markdown(file_path):
103103
dashed_header_format = first_line == "---\n"
104104

105105
with open(file_path, "r", encoding="utf-8") as markdown_content:
106+
107+
md = Markdown(
108+
extensions=["tables", "fenced_code", "codehilite", "toc", "sane_lists"],
109+
extension_configs={
110+
"codehilite": {"css_class": "highlight"},
111+
"toc": {"anchorlink": True, "anchorlink_class": "headerlink"},
112+
},
113+
)
114+
106115
if dashed_header_format:
107116
headerless = []
108117
header_block_open = False
@@ -111,22 +120,12 @@ def format_markdown(file_path):
111120
header_block_open = not header_block_open
112121
elif not header_block_open:
113122
headerless.append(line)
114-
return markdown(
115-
"".join(headerless),
116-
extensions=["tables", "fenced_code", "codehilite", "toc", "sane_lists"],
117-
extension_configs={
118-
"codehilite": {"css_class": "highlight"},
119-
"toc": {"anchorlink": True, "anchorlink_class": "headerlink"},
120-
},
121-
)
122-
return markdown(
123-
"".join(markdown_content.readlines()[3:]),
124-
extensions=["tables", "fenced_code", "codehilite", "toc", "sane_lists"],
125-
extension_configs={
126-
"codehilite": {"css_class": "highlight"},
127-
"toc": {"anchorlink": True, "anchorlink_class": "headerlink"},
128-
},
129-
)
123+
124+
body = md.convert("".join(headerless))
125+
return {"body": body, "toc": md.toc_tokens}
126+
127+
body = md.convert("".join(markdown_content.readlines()[3:]))
128+
return {"body": body, "toc": md.toc}
130129

131130

132131
if __name__ == "__main__":
@@ -168,7 +167,9 @@ def format_markdown(file_path):
168167
f"{LOCALIZED_SITE_PATH}{output_path}", "w+", encoding="utf-8"
169168
) as render_file:
170169
data = get_site_data(SLUG, locale["code"], content_path, version)
171-
data["content"] = format_markdown(content_path)
170+
formatted_md = format_markdown(content_path)
171+
data["content"] = formatted_md["body"]
172+
data["toc"] = formatted_md["toc"]
172173
data["path"] = (
173174
f"/{SLUG}{output_path}"
174175
if not version

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
black==24.3.0
22
Jinja2==3.1.6
3-
Markdown==3.3.4
3+
Markdown==3.9.0
44
PyYAML==6.0.2
55
pygments==2.16.1

site/static/js/bookwyrm.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,14 @@ document.addEventListener('DOMContentLoaded', () => {
6060
$menuTarget.classList.toggle('is-hidden-touch');
6161
});
6262
});
63+
64+
// Table of contents
65+
const $tocController = document.getElementById("toc");
66+
$tocController.addEventListener('click', () => {
67+
$tocController.classList.toggle('is-active');
68+
const target = $tocController.dataset.target;
69+
const $target = document.getElementById(target);
70+
$target.classList.toggle('is-hidden-touch');
71+
})
72+
6373
});

templates/layout.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
{% endif %}
6969

7070
<div class="columns">
71-
<nav class="menu column is-one-quarter is-hidden-touch pl-6 mt-4" id="menu">
71+
<nav class="menu column is-one-quarter is-hidden-touch pl-6" id="menu">
7272
<h2 class="menu-label">Language & Version</a></h2>
7373
<div class="select">
7474
<select aria-label="{{ _("Select a language") }}" id="language_selection">
@@ -96,7 +96,8 @@ <h2 class="menu-label">{{ category.title }}</h2>
9696
{% endfor %}
9797
</nav>
9898

99-
<div class="column">
99+
<div class="column pt-0">
100+
{% block toc %}{% endblock %}
100101
<div class="container is-max-desktop">
101102
<section class="section">
102103
<header class="content block column is-offset-one-quarter pl-2">

templates/page.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,30 @@
77
<h1 class="title is-1">{{ headers.Title }}</h1>
88
{% endblock %}
99

10+
{% block toc %}
11+
{% if toc %}
12+
<nav class="navbar is-dark" role="table-of-contents-navigation" aria-label="dropdown navigation">
13+
<div class="navbar-start">
14+
<div class="navbar-item has-dropdown" id="toc" data-target="toc-dropdown">
15+
<a class="navbar-link">
16+
{{ _("Page Contents") }}
17+
</a>
18+
19+
<div class="navbar-dropdown is-hidden-touch " id="toc-dropdown">
20+
{% for entry in toc %}
21+
<a class="navbar-item" href="#{{entry.id}}">{{entry.name}}</a>
22+
{% if entry.children %}
23+
{% for child in entry.children %}
24+
<a class="navbar-item" href="#{{child.id}}"><span class="pl-4">{{child.name}}</span></a>
25+
{% endfor %}
26+
{% endif %}
27+
{% endfor %}
28+
</div>
29+
</div>
30+
</div>
31+
</nav>
32+
{% endif %}
33+
{% endblock %}
1034

1135
{% block content %}
1236
<section class="block content">

0 commit comments

Comments
 (0)