blob: 621424d9d0c55ad897ac79471f6f65e4f968fc32 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
"""
Adds 'target="_blank"' to links, so they open in a new tab.
Filter base urls can be added so that every absolute path link containing the filter is ignored by the extension.
Usage:
MyExtension = get_new_tab_links(["example.org"])
"""
import marko.inline
from marko.helpers import MarkoExtension
from . import utils
def __create_new_tab_links(filters: list[str]):
class NewTabLinks:
FILTERS = filters
def render_link(self, element: marko.inline.Link) -> str:
if utils.is_foreign_url(element.dest, NewTabLinks.FILTERS):
return f"<a href=\"{element.dest}\" target=\"_blank\">{self.render_children(element)}</a>"
return super().render_link(element)
return NewTabLinks
def get_new_tab_links(filters: list[str] = None):
if filters is None:
filters = []
return MarkoExtension(
renderer_mixins=[__create_new_tab_links(filters)]
)
|