1
Fork 0
mkdocs-site/hooks/url-rewrite.py

24 lines
661 B
Python
Raw Normal View History

2023-09-24 18:04:13 +01:00
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)