1
Fork 0

Mutate external URLs

This commit is contained in:
Jake Howard 2023-09-24 18:04:13 +01:00
parent 8da317e061
commit f3edcc1752
Signed by: jake
GPG Key ID: 57AFB45680EDD477
3 changed files with 33 additions and 1 deletions

View File

@ -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)

23
hooks/url-rewrite.py Normal file
View File

@ -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)

View File

@ -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