Rule for identifying plain-text links.
(state: StateCore)
| 13 | |
| 14 | |
| 15 | def linkify(state: StateCore) -> None: |
| 16 | """Rule for identifying plain-text links.""" |
| 17 | if not state.md.options.linkify: |
| 18 | return |
| 19 | |
| 20 | if not state.md.linkify: |
| 21 | raise ModuleNotFoundError("Linkify enabled but not installed.") |
| 22 | |
| 23 | for inline_token in state.tokens: |
| 24 | if inline_token.type != "inline" or not state.md.linkify.pretest( |
| 25 | inline_token.content |
| 26 | ): |
| 27 | continue |
| 28 | |
| 29 | tokens = inline_token.children |
| 30 | |
| 31 | htmlLinkLevel = 0 |
| 32 | |
| 33 | # We scan from the end, to keep position when new tags added. |
| 34 | # Use reversed logic in links start/end match |
| 35 | assert tokens is not None |
| 36 | i = len(tokens) |
| 37 | while i >= 1: |
| 38 | i -= 1 |
| 39 | assert isinstance(tokens, list) |
| 40 | currentToken = tokens[i] |
| 41 | |
| 42 | # Skip content of markdown links |
| 43 | if currentToken.type == "link_close": |
| 44 | i -= 1 |
| 45 | while ( |
| 46 | tokens[i].level != currentToken.level |
| 47 | and tokens[i].type != "link_open" |
| 48 | ): |
| 49 | i -= 1 |
| 50 | continue |
| 51 | |
| 52 | # Skip content of html tag links |
| 53 | if currentToken.type == "html_inline": |
| 54 | if isLinkOpen(currentToken.content) and htmlLinkLevel > 0: |
| 55 | htmlLinkLevel -= 1 |
| 56 | if isLinkClose(currentToken.content): |
| 57 | htmlLinkLevel += 1 |
| 58 | if htmlLinkLevel > 0: |
| 59 | continue |
| 60 | |
| 61 | if currentToken.type == "text" and state.md.linkify.test( |
| 62 | currentToken.content |
| 63 | ): |
| 64 | text = currentToken.content |
| 65 | links: list[_LinkType] = state.md.linkify.match(text) or [] |
| 66 | |
| 67 | # Now split string to nodes |
| 68 | nodes = [] |
| 69 | level = currentToken.level |
| 70 | lastPos = 0 |
| 71 | |
| 72 | # forbid escape sequence at the start of the string, |
nothing calls this directly
no test coverage detected
searching dependent graphs…