|
64 | 64 | this for other tags. |
65 | 65 | * link-patterns: Auto-link given regex patterns in text (e.g. bug number |
66 | 66 | references, revision number references). |
| 67 | +* link-shortrefs: allow shortcut reference links, not followed by `[]` or |
| 68 | + a link label. |
67 | 69 | * markdown-in-html: Allow the use of `markdown="1"` in a block HTML tag to |
68 | 70 | have markdown processing be done on its contents. Similar to |
69 | 71 | <http://michelf.com/projects/php-markdown/extra/#markdown-attr> but with |
@@ -1646,7 +1648,23 @@ def _do_links(self, text: str) -> str: |
1646 | 1648 |
|
1647 | 1649 | # Reference anchor or img? |
1648 | 1650 | else: |
1649 | | - match = self._tail_of_reference_link_re.match(text, p) |
| 1651 | + match = None |
| 1652 | + if 'link-shortrefs' in self.extras: |
| 1653 | + # check if there's no tailing id section |
| 1654 | + if link_text and re.match(r'[ ]?(?:\n[ ]*)?(?!\[)', text[p:]): |
| 1655 | + # try a match with `[]` inserted into the text |
| 1656 | + match = self._tail_of_reference_link_re.match(f'{text[:p]}[]{text[p:]}', p) |
| 1657 | + if match: |
| 1658 | + # if we get a match, we'll have to modify the `text` variable to insert the `[]` |
| 1659 | + # but we ONLY want to do that if the link_id is valid. This makes sure that we |
| 1660 | + # don't get stuck in any loops and also that when a user inputs `[abc]` we don't |
| 1661 | + # output `[abc][]` in the final HTML |
| 1662 | + if (match.group("id").lower() or link_text.lower()) in self.urls: |
| 1663 | + text = f'{text[:p]}[]{text[p:]}' |
| 1664 | + else: |
| 1665 | + match = None |
| 1666 | + |
| 1667 | + match = match or self._tail_of_reference_link_re.match(text, p) |
1650 | 1668 | if match: |
1651 | 1669 | # Handle a reference-style anchor or img. |
1652 | 1670 | is_img = start_idx > 0 and text[start_idx-1] == "!" |
|
0 commit comments