From f3edcc1752c231312f7ee05b430f7010e983ef72 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 24 Sep 2023 18:04:13 +0100 Subject: [PATCH] Mutate external URLs --- content/index.md | 6 +++++- hooks/url-rewrite.py | 23 +++++++++++++++++++++++ mkdocs.yml | 5 +++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 hooks/url-rewrite.py diff --git a/content/index.md b/content/index.md index 60a81c5..dc160a6 100644 --- a/content/index.md +++ b/content/index.md @@ -8,4 +8,8 @@ After image ![Image](https://theorangeone.net/images/ml7eDl0dfJcNa0gb78bceOAZA2Y=/1/width-1200/header.jpg) -{{ 1 + 2}} +{{ 1 + 2 }} + +- [Another page](./other.md) +- [Something else on the site](/sitemap.xml) +- [An external site](https://mkdocs.org) diff --git a/hooks/url-rewrite.py b/hooks/url-rewrite.py new file mode 100644 index 0000000..a6fabb7 --- /dev/null +++ b/hooks/url-rewrite.py @@ -0,0 +1,23 @@ +from bs4 import BeautifulSoup +from urllib.parse import urlsplit, urlunsplit, parse_qs, urlencode + + +def on_page_content(html, page, config, files): + soup = BeautifulSoup(html, "html.parser") + + for link in soup.find_all("a", href=True): + parsed_url = urlsplit(link["href"]) + + if not parsed_url.scheme or not parsed_url.netloc: + # Not an external link + continue + + # Stick a reference on external URLs + parsed_url = parsed_url._replace(query=urlencode({ + **parse_qs(parsed_url.query), + "ref": "mysite.com" + })) + + link["href"] = urlunsplit(parsed_url) + + return str(soup) diff --git a/mkdocs.yml b/mkdocs.yml index a6e0ab3..a6c0d79 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -21,6 +21,7 @@ hooks: - hooks/include-file.py - hooks/unsplash.py - hooks/exclude-static.py + - hooks/url-rewrite.py markdown_extensions: - attr_list @@ -29,3 +30,7 @@ markdown_extensions: plugins: - search + +validation: + links: + absolute_links: ignore