summaryrefslogtreecommitdiff
path: root/newtablink.py
diff options
context:
space:
mode:
authorBotond Hende <nettingman@gmail.com>2024-07-24 23:45:48 +0200
committerBotond Hende <nettingman@gmail.com>2024-07-24 23:45:48 +0200
commitc307de76f9b54d5c65196341ee12ea38ac3d6e6b (patch)
tree9ab51d1b778fbfc01138afa89a01c5ce0e71a08d /newtablink.py
parent0f075b9602875e6babaffab9ba8535f07f1df40e (diff)
new tab link module
Diffstat (limited to 'newtablink.py')
-rw-r--r--newtablink.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/newtablink.py b/newtablink.py
new file mode 100644
index 0000000..df42833
--- /dev/null
+++ b/newtablink.py
@@ -0,0 +1,33 @@
+"""
+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
+
+
+def __create_new_tab_links(filters: list[str]):
+ class NewTabLinks:
+ FILTERS = filters
+
+ def render_link(self, element: marko.inline.Link) -> str:
+ dest_lower = element.dest.lower()
+
+ if (dest_lower.startswith("http://") or dest_lower.startswith("https://")) and not any((elem in dest_lower for elem in 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)]
+ )