Mutate external URLs
This commit is contained in:
parent
8da317e061
commit
f3edcc1752
3 changed files with 33 additions and 1 deletions
|
@ -9,3 +9,7 @@ After image
|
|||
![Image](https://theorangeone.net/images/ml7eDl0dfJcNa0gb78bceOAZA2Y=/1/width-1200/header.jpg)
|
||||
|
||||
{{ 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
23
hooks/url-rewrite.py
Normal 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)
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue