Skip to content

Reference links not rendered in table of contents links #426

@lemon24

Description

@lemon24

Reference links are not rendered in table of contents links (they are left as [link][ref]). Inline links and other formatting do seem to work (specifically, the HTML is stripped).

I think the root cause is similar to #378 (comment), state not being passed around when the TOC ul is rendered; the fix probably involves passing the parent state to normalize_toc_item here.

This did use to work in v2; together with the fact that links are allowed in headings, I'd tend to consider it a regression.

Repro (mistune 3.1.4):

>>> from mistune import *
>>> from mistune.directives import *
>>> md = create_markdown(plugins=[RSTDirective([TableOfContents()])])
>>> s = """
... .. toc::
... # **bold**
... # [inline link](example.com) 
... # [link ref][ref]
... [ref]: example.com
... """
>>> print(md(s))
<details class="toc" open>
<summary>Table of Contents</summary>
<ul>
<li><a href="#toc_1">bold</a></li>
<li><a href="#toc_2">inline link</a></li>
<li><a href="#toc_3">[link ref][ref]</a></li>
</ul>
</details>
<h1 id="toc_1"><strong>bold</strong></h1>
<h1 id="toc_2"><a href="example.com">inline link</a></h1>
<h1 id="toc_3"><a href="example.com">link ref</a></h1>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions