Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/mistune/directives/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def toc_hook(self, md: "Markdown", state: "BlockState") -> None:
# adding ID for each heading
for i, tok in enumerate(headings):
tok["attrs"]["id"] = self.generate_heading_id(tok, i)
toc_items.append(normalize_toc_item(md, tok))
toc_items.append(normalize_toc_item(md, tok, parent=state))

for sec in sections:
_min = sec["attrs"]["min_level"]
Expand Down
6 changes: 3 additions & 3 deletions src/mistune/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ def toc_hook(md: "Markdown", state: "BlockState") -> None:
toc_items = []
for i, tok in enumerate(headings):
tok["attrs"]["id"] = heading_id(tok, i)
toc_items.append(normalize_toc_item(md, tok))
toc_items.append(normalize_toc_item(md, tok, parent=state))

# save items into state
state.env["toc_items"] = toc_items

md.before_render_hooks.append(toc_hook)


def normalize_toc_item(md: "Markdown", token: Dict[str, Any]) -> Tuple[int, str, str]:
def normalize_toc_item(md: "Markdown", token: Dict[str, Any], parent: Optional[Any] = None) -> Tuple[int, str, str]:
text = token["text"]
tokens = md.inline(text, {})
tokens = md.inline(text, parent.env if parent else {})
assert md.renderer is not None
html = md.renderer(tokens, BlockState())
text = striptags(html)
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/fenced_toc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,19 @@ none

```````````````````````````````` example
# [foo](/bar)
# [baz][ref]
[ref]: /quux

```{toc}
```
.
<h1 id="toc_1"><a href="/bar">foo</a></h1>
<h1 id="toc_2"><a href="/quux">baz</a></h1>
<details class="toc" open>
<summary>Table of Contents</summary>
<ul>
<li><a href="#toc_1">foo</a></li>
<li><a href="#toc_2">baz</a></li>
</ul>
</details>
````````````````````````````````
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/hook_toc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,14 @@ content

```````````````````````````````` example
# [foo](/bar)
# [baz][ref]
[ref]: /quux
.
<h1 id="toc_1"><a href="/bar">foo</a></h1>
<h1 id="toc_2"><a href="/quux">baz</a></h1>
<ul>
<li><a href="#toc_1">foo</a></li>
<li><a href="#toc_2">baz</a></li>
</ul>
````````````````````````````````

Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/rst_toc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,17 @@ none

```````````````````````````````` example
# [foo](/bar)
# [baz][ref]
[ref]: /quux
.. toc::
.
<h1 id="toc_1"><a href="/bar">foo</a></h1>
<h1 id="toc_2"><a href="/quux">baz</a></h1>
<details class="toc" open>
<summary>Table of Contents</summary>
<ul>
<li><a href="#toc_1">foo</a></li>
<li><a href="#toc_2">baz</a></li>
</ul>
</details>
````````````````````````````````
Expand Down