Debian Codegraph @

third_party/python/markdown_it_py/markdown_it/helpers/parse_link_label.py

raw
1 """
2 Parse link label
3
4 this function assumes that first character ("[") already matches
5 returns the end of the label
6
7 """
8 from markdown_it.rules_inline import StateInline
9
10
11 def parseLinkLabel(state: StateInline, start: int, disableNested: bool = False) -> int:
12 labelEnd = -1
13 oldPos = state.pos
14 found = False
15
16 state.pos = start + 1
17 level = 1
18
19 while state.pos < state.posMax:
20 marker = state.src[state.pos]
21 if marker == "]":
22 level -= 1
23 if level == 0:
24 found = True
25 break
26
27 prevPos = state.pos
28 state.md.inline.skipToken(state)
29 if marker == "[":
30 if prevPos == state.pos - 1:
31 # increase level if we find text `[`,
32 # which is not a part of any token
33 level += 1
34 elif disableNested:
35 state.pos = oldPos
36 return -1
37 if found:
38 labelEnd = state.pos
39
40 # restore old state
41 state.pos = oldPos
42
43 return labelEnd