11import re
2- from typing import Callable , List , Optional
2+ from typing import Callable , List , Optional , Set
33
44from markdown_it import MarkdownIt
55from markdown_it .rules_core import StateCore
@@ -65,19 +65,20 @@ def _make_anchors_func(
6565 permalinkBefore : bool ,
6666 permalinkSpace : bool ,
6767):
68- slugs = set ()
68+ slugs : Set [ str ] = set ()
6969
7070 def _anchor_func (state : StateCore ):
7171 for (idx , token ) in enumerate (state .tokens ):
72- token : Token
7372 if token .type != "heading_open" :
7473 continue
7574 level = int (token .tag [1 ])
7675 if level not in selected_levels :
7776 continue
77+ inline_token = state .tokens [idx + 1 ]
78+ assert inline_token .children is not None
7879 title = "" .join (
7980 child .content
80- for child in state . tokens [ idx + 1 ] .children
81+ for child in inline_token .children
8182 if child .type in ["text" , "code_inline" ]
8283 )
8384 slug = unique_slug (slug_func (title ), slugs )
@@ -95,17 +96,17 @@ def _anchor_func(state: StateCore):
9596 Token ("link_close" , "a" , - 1 ),
9697 ]
9798 if permalinkBefore :
98- state . tokens [ idx + 1 ] .children = (
99+ inline_token .children = (
99100 link_tokens
100101 + (
101102 [Token ("text" , "" , 0 , content = " " )]
102103 if permalinkSpace
103104 else []
104105 )
105- + state . tokens [ idx + 1 ] .children
106+ + inline_token .children
106107 )
107108 else :
108- state . tokens [ idx + 1 ] .children .extend (
109+ inline_token .children .extend (
109110 ([Token ("text" , "" , 0 , content = " " )] if permalinkSpace else [])
110111 + link_tokens
111112 )
0 commit comments