Last active
October 20, 2023 06:13
-
-
Save zmx/fb5b8af02dd7f615e638dcfd18cd4d4f to your computer and use it in GitHub Desktop.
mkdocs with hugo "_index.md"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import posixpath | |
from urllib.parse import quote as urlquote | |
# Hugo rule https://gohugo.io/functions/urls/urlize/ | |
# | |
# We use a small subset | |
HTML_TEMPLATE = """ | |
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Redirecting...</title> | |
<link rel="canonical" href="{url}"> | |
<meta name="robots" content="noindex"> | |
<script> | |
location.pathname = "{url}" | |
</script> | |
<meta http-equiv="refresh" content="0; url={url}"> | |
</head> | |
<body> | |
Redirecting... | |
</body> | |
</html> | |
""" | |
def write_html(site_dir, old_path, mkdocs_url): | |
"""Write an HTML file in the site_dir with a meta redirect to the new page""" | |
# Determine all relevant paths | |
old_path_abs = os.path.join(site_dir, old_path) | |
old_dir = os.path.dirname(old_path) | |
old_dir_abs = os.path.dirname(old_path_abs) | |
# Create parent directories if they don't exist | |
if not os.path.exists(old_dir_abs): | |
os.makedirs(old_dir_abs) | |
# Write the HTML redirect file in place of the old file | |
content = HTML_TEMPLATE.format(url=mkdocs_url) | |
with open(old_path_abs, 'w', encoding='utf-8') as f: | |
f.write(content) | |
def on_files(files, config): | |
for file in files: | |
if file.is_documentation_page() and file.name == "_index": | |
file.name = "index" | |
if file.dest_uri == "index.html": | |
file.url = "index.html" | |
else: | |
file.dest_uri = file.dest_uri.replace("_index/index.html", "index.html") | |
# Copy from mkdocs strucuture/files.py | |
# | |
# def _get_url(self, use_directory_urls: bool) -> str: | |
# | |
url = file.dest_uri | |
dirname, filename = posixpath.split(url) | |
url = (dirname or '.') + '/' | |
file.url = urlquote(url) | |
file.abs_dest_path = file.abs_dest_path.replace("_index/index.html", "index.html") | |
def on_post_page(output, page, config): | |
# create index.html to redirect hugo-style url to mkdocs-style url | |
mkdocs_url = page.abs_url.replace("/_index/", "/") | |
hugo_path = page.file.dest_uri.lower().replace(' ', '-') | |
# hugo_path = page.file.url.lower().replace(' ', '-') | |
write_html( | |
config['site_dir'], | |
hugo_path, | |
mkdocs_url, | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
site_name: Example Site | |
site_url: https://docs.example.com/example_deparment/ | |
docs_dir: content | |
site_dir: public | |
repo_url: https://gitlab.example.com/docs | |
repo_name: docs | |
edit_uri: blob/main/content/ | |
theme: | |
name: material | |
features: | |
- content.action.edit | |
- content.action.view | |
- content.code.copy | |
hooks: | |
- mkdocs-hugo-uri.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment