| 1 | # linkify-it-py |
| 2 | |
| 3 | [](https://github.com/tsutsu3/linkify-it-py/actions/workflows/github-ci.yml) |
| 4 | [](https://pypi.org/project/linkify-it-py/) |
| 5 | [](https://anaconda.org/conda-forge/linkify-it-py) |
| 6 | [](https://linkify-it-py.readthedocs.io/en/latest/?badge=latest) |
| 7 | [](https://codecov.io/gh/tsutsu3/linkify-it-py) |
| 8 | |
| 9 | This is Python port of [linkify-it](https://github.com/markdown-it/linkify-it). |
| 10 | |
| 11 | > Links recognition library with FULL unicode support. |
| 12 | > Focused on high quality link patterns detection in plain text. |
| 13 | |
| 14 | __[Demo](https://linkify-it-py-demo.vercel.app/)__ |
| 15 | |
| 16 | __[Javascript Demo](http://markdown-it.github.io/linkify-it/)__ |
| 17 | |
| 18 | Why it's awesome: |
| 19 | |
| 20 | - Full unicode support, _with astral characters_! |
| 21 | - International domains support. |
| 22 | - Allows rules extension & custom normalizers. |
| 23 | |
| 24 | ## Python Version Support |
| 25 | |
| 26 | Tested on Python 3.10–3.14. The primary version for code coverage follows the latest |
| 27 | [security phase](https://devguide.python.org/versions/) release (currently __3.12__). |
| 28 | |
| 29 | ## Install |
| 30 | |
| 31 | ```bash |
| 32 | pip install linkify-it-py |
| 33 | ``` |
| 34 | |
| 35 | or |
| 36 | |
| 37 | ```bash |
| 38 | conda install -c conda-forge linkify-it-py |
| 39 | ``` |
| 40 | |
| 41 | ## Usage examples |
| 42 | |
| 43 | ### Example 1. Simple use |
| 44 | |
| 45 | ```python |
| 46 | from linkify_it import LinkifyIt |
| 47 | |
| 48 | |
| 49 | linkify = LinkifyIt() |
| 50 | |
| 51 | print(linkify.test("Site github.com!")) |
| 52 | # => True |
| 53 | |
| 54 | print(linkify.match("Site github.com!")) |
| 55 | # => [linkify_it.main.Match({ |
| 56 | # 'schema': '', |
| 57 | # 'index': 5, |
| 58 | # 'last_index': 15, |
| 59 | # 'raw': 'github.com', |
| 60 | # 'text': 'github.com', |
| 61 | # 'url': 'http://github.com' |
| 62 | # }] |
| 63 | ``` |
| 64 | |
| 65 | ### Example 2. With options |
| 66 | |
| 67 | ```python |
| 68 | from linkify_it import LinkifyIt |
| 69 | from linkify_it.tlds import TLDS |
| 70 | |
| 71 | |
| 72 | # Reload full tlds list & add unofficial `.onion` domain. |
| 73 | linkify = ( |
| 74 | LinkifyIt() |
| 75 | .tlds(TLDS) # Reload with full tlds list |
| 76 | .tlds("onion", True) # Add unofficial `.onion` domain |
| 77 | .add("git:", "http:") # Add `git:` protocol as "alias" |
| 78 | .add("ftp:", None) # Disable `ftp:` protocol |
| 79 | .set({"fuzzy_ip": True}) # Enable IPs in fuzzy links (without schema) |
| 80 | ) |
| 81 | print(linkify.test("Site tamanegi.onion!")) |
| 82 | # => True |
| 83 | |
| 84 | print(linkify.match("Site tamanegi.onion!")) |
| 85 | # => [linkify_it.main.Match({ |
| 86 | # 'schema': '', |
| 87 | # 'index': 5, |
| 88 | # 'last_index': 19, |
| 89 | # 'raw': 'tamanegi.onion', |
| 90 | # 'text': 'tamanegi.onion', |
| 91 | # 'url': 'http://tamanegi.onion' |
| 92 | # }] |
| 93 | ``` |
| 94 | |
| 95 | ### Example 3. Add twitter mentions handler |
| 96 | |
| 97 | ```python |
| 98 | from linkify_it import LinkifyIt |
| 99 | |
| 100 | |
| 101 | linkify = LinkifyIt() |
| 102 | |
| 103 | def validate(obj, text, pos): |
| 104 | tail = text[pos:] |
| 105 | |
| 106 | if not obj.re.get("twitter"): |
| 107 | obj.re["twitter"] = re.compile( |
| 108 | "^([a-zA-Z0-9_]){1,15}(?!_)(?=$|" + obj.re["src_ZPCc"] + ")" |
| 109 | ) |
| 110 | if obj.re["twitter"].search(tail): |
| 111 | if pos > 2 and tail[pos - 2] == "@": |
| 112 | return False |
| 113 | return len(obj.re["twitter"].search(tail).group()) |
| 114 | return 0 |
| 115 | |
| 116 | def normalize(obj, match): |
| 117 | match.url = "https://twitter.com/" + re.sub(r"^@", "", match.url) |
| 118 | |
| 119 | linkify.add("@", {"validate": validate, "normalize": normalize}) |
| 120 | ``` |
| 121 | |
| 122 | ## API |
| 123 | |
| 124 | [API documentation](https://linkify-it-py.readthedocs.io/en/latest/) |
| 125 | |
| 126 | ### LinkifyIt(schemas, options) |
| 127 | |
| 128 | Creates new linkifier instance with optional additional schemas. |
| 129 | |
| 130 | By default understands: |
| 131 | |
| 132 | - `http(s)://...` , `ftp://...`, `mailto:...` & `//...` links |
| 133 | - "fuzzy" links and emails (google.com, [email protected]). |
| 134 | |
| 135 | `schemas` is an dict, where each key/value describes protocol/rule: |
| 136 | |
| 137 | - __key__ - link prefix (usually, protocol name with `:` at the end, `skype:` |
| 138 | for example). `linkify-it-py` makes sure that prefix is not preceded with |
| 139 | alphanumeric char. |
| 140 | - __value__ - rule to check tail after link prefix |
| 141 | - _str_ |
| 142 | - just alias to existing rule |
| 143 | - _dict_ |
| 144 | - _validate_ - either a `re.Pattern` (start with `^`, and don't include the |
| 145 | link prefix itself), or a validator `function` which, given arguments |
| 146 | _self_, _text_ and _pos_, returns the length of a match in _text_ |
| 147 | starting at index _pos_. _pos_ is the index right after the link prefix. |
| 148 | _self_ can be used to access the linkify object to cache data. |
| 149 | - _normalize_ - optional function to normalize text & url of matched result |
| 150 | (for example, for twitter mentions). |
| 151 | |
| 152 | `options`: |
| 153 | |
| 154 | - __fuzzy_link__ - recognize URL-s without `http(s)://` head. Default `True`. |
| 155 | - __fuzzy_ip__ - allow IPs in fuzzy links above. Can conflict with some texts |
| 156 | like version numbers. Default `False`. |
| 157 | - __fuzzy_email__ - recognize emails without `mailto:` prefix. Default `True`. |
| 158 | - __---__ - set `True` to terminate link with `---` (if it's considered as long dash). |
| 159 | |
| 160 | ### .test(text) |
| 161 | |
| 162 | Searches linkifiable pattern and returns `True` on success or `False` on fail. |
| 163 | |
| 164 | ### .pretest(text) |
| 165 | |
| 166 | Quick check if link MAY BE can exist. Can be used to optimize more expensive |
| 167 | `.test()` calls. Return `False` if link can not be found, `True` - if `.test()` |
| 168 | call needed to know exactly. |
| 169 | |
| 170 | ### .test_schema_at(text, name, position) |
| 171 | |
| 172 | Similar to `.test()` but checks only specific protocol tail exactly at given |
| 173 | position. Returns length of found pattern (0 on fail). |
| 174 | |
| 175 | ### .match(text) |
| 176 | |
| 177 | Returns `list` of found link matches or null if nothing found. |
| 178 | |
| 179 | Each match has: |
| 180 | |
| 181 | - __schema__ - link schema, can be empty for fuzzy links, or `//` for |
| 182 | protocol-neutral links. |
| 183 | - __index__ - offset of matched text |
| 184 | - __last_index__ - index of next char after mathch end |
| 185 | - __raw__ - matched text |
| 186 | - __text__ - normalized text |
| 187 | - __url__ - link, generated from matched text |
| 188 | |
| 189 | ### .matchAtStart(text) |
| 190 | |
| 191 | Checks if a match exists at the start of the string. Returns `Match` |
| 192 | (see docs for `match(text)`) or null if no URL is at the start. |
| 193 | Doesn't work with fuzzy links. |
| 194 | |
| 195 | ### .tlds(list_tlds, keep_old=False) |
| 196 | |
| 197 | Load (or merge) new tlds list. Those are needed for fuzzy links (without schema) |
| 198 | to avoid false positives. By default: |
| 199 | |
| 200 | - 2-letter root zones are ok. |
| 201 | - biz|com|edu|gov|net|org|pro|web|xxx|aero|asia|coop|info|museum|name|shop|рф are ok. |
| 202 | - encoded (`xn--...`) root zones are ok. |
| 203 | |
| 204 | If that's not enough, you can reload defaults with more detailed zones list. |
| 205 | |
| 206 | ### .add(key, value) |
| 207 | |
| 208 | Add a new schema to the schemas object. As described in the constructor |
| 209 | definition, `key` is a link prefix (`skype:`, for example), and `value` |
| 210 | is a `str` to alias to another schema, or an `dict` with `validate` and |
| 211 | optionally `normalize` definitions. To disable an existing rule, use |
| 212 | `.add(key, None)`. |
| 213 | |
| 214 | ### .set(options) |
| 215 | |
| 216 | Override default options. Missed properties will not be changed. |
| 217 | |
| 218 | ## License |
| 219 | |
| 220 | [MIT](https://github.com/tsutsu3/linkify-it-py/blob/master/LICENSE) |