parse_link_label.py 1.0 KB

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