From cc05858b1ff08dcd25d42230f44c1243a3381266 Mon Sep 17 00:00:00 2001 From: mDuo13 Date: Thu, 7 Feb 2019 13:37:30 -0800 Subject: [PATCH] Add external link marker --- dactyl-config.yml | 5 +++++ tool/filter_external_links.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 tool/filter_external_links.py diff --git a/dactyl-config.yml b/dactyl-config.yml index 9daaa92770..f7bc157740 100644 --- a/dactyl-config.yml +++ b/dactyl-config.yml @@ -20,6 +20,10 @@ content_static_path: img # PDF creation needs a dir for temporary files temporary_files_path: /tmp/ +# Custom filters live here and start with filter_ +filter_paths: + - tool + default_filters: - multicode_tabs - standardize_header_ids @@ -27,6 +31,7 @@ default_filters: - callouts - badges - link_replacement + - external_links callout_class: "devportal-callout" diff --git a/tool/filter_external_links.py b/tool/filter_external_links.py new file mode 100644 index 0000000000..1421827491 --- /dev/null +++ b/tool/filter_external_links.py @@ -0,0 +1,31 @@ +################################################ +## External Link Marker +## Author: Rome Reginelli +## Copyright: Ripple Labs, 2019 +## +## Finds external links and changes them to: +## - open in a new tab (target="_blank") +## - end with an "external link" icon +## (FontAwesome required) +################################################ + +import re + + +def filter_soup(soup, **kwargs): + """ + Adds an external link marker to external links + and makes them open in new tabs. + """ + + extern_regex = re.compile(r"^https?://") + + links = soup.find_all("a", href=True) + for link in links: + if extern_regex.match(link["href"]): + link["target"] = "_blank" + ex_link_marker = soup.new_tag("i", attrs={ + "class":"fa fa-external-link", + "aria-hidden": "true"}) + link.append(" ") + link.append(ex_link_marker)