From a0a0148af57970c62f6fb437674f88753522ef51 Mon Sep 17 00:00:00 2001 From: lemon24 Date: Sun, 21 Dec 2025 12:33:59 +0200 Subject: [PATCH] fix: render ref links in TOC. Fixes #426. --- src/mistune/directives/toc.py | 2 +- src/mistune/toc.py | 6 +++--- tests/fixtures/fenced_toc.txt | 4 ++++ tests/fixtures/hook_toc.txt | 4 ++++ tests/fixtures/rst_toc.txt | 4 ++++ 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/mistune/directives/toc.py b/src/mistune/directives/toc.py index ac390e54..70946f0f 100644 --- a/src/mistune/directives/toc.py +++ b/src/mistune/directives/toc.py @@ -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"] diff --git a/src/mistune/toc.py b/src/mistune/toc.py index 062066be..9c1d062c 100644 --- a/src/mistune/toc.py +++ b/src/mistune/toc.py @@ -48,7 +48,7 @@ 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 @@ -56,9 +56,9 @@ def toc_hook(md: "Markdown", state: "BlockState") -> None: 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) diff --git a/tests/fixtures/fenced_toc.txt b/tests/fixtures/fenced_toc.txt index 1744000c..eb0ef00e 100644 --- a/tests/fixtures/fenced_toc.txt +++ b/tests/fixtures/fenced_toc.txt @@ -182,15 +182,19 @@ none ```````````````````````````````` example # [foo](/bar) +# [baz][ref] +[ref]: /quux ```{toc} ``` .

foo

+

baz

Table of Contents
```````````````````````````````` diff --git a/tests/fixtures/hook_toc.txt b/tests/fixtures/hook_toc.txt index 62a1a1ec..11ab0815 100644 --- a/tests/fixtures/hook_toc.txt +++ b/tests/fixtures/hook_toc.txt @@ -103,10 +103,14 @@ content ```````````````````````````````` example # [foo](/bar) +# [baz][ref] +[ref]: /quux .

foo

+

baz

```````````````````````````````` diff --git a/tests/fixtures/rst_toc.txt b/tests/fixtures/rst_toc.txt index d65e7516..daa1bae0 100644 --- a/tests/fixtures/rst_toc.txt +++ b/tests/fixtures/rst_toc.txt @@ -173,13 +173,17 @@ none ```````````````````````````````` example # [foo](/bar) +# [baz][ref] +[ref]: /quux .. toc:: .

foo

+

baz

Table of Contents
````````````````````````````````