Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed redirects for computed destinations and URLs #45

Merged
merged 7 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions mkdocs_redirects/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def get_relative_html_path(old_page, new_page, use_directory_urls):
"""Return the relative path from the old html path to the new html path"""
old_path = get_html_path(old_page, use_directory_urls)
new_path, new_hash_fragment = _split_hash_fragment(new_page)
new_path = get_html_path(new_path, use_directory_urls)

if use_directory_urls:
# remove /index.html from end of path
Expand Down Expand Up @@ -129,14 +128,15 @@ def on_post_build(self, config, **kwargs):
# Walk through the redirect map and write their HTML files
for page_old, page_new in self.redirects.items():
# Need to remove hash fragment from new page to verify existence
page_new_without_hash, _ = _split_hash_fragment(str(page_new))
page_new_without_hash, hash = _split_hash_fragment(str(page_new))

# External redirect targets are easy, just use it as the target path
if page_new.lower().startswith(('http://', 'https://')):
dest_path = page_new

elif page_new_without_hash in self.doc_pages:
dest_path = get_relative_html_path(page_old, page_new, use_directory_urls)
file = self.doc_pages[page_new_without_hash]
dest_path = get_relative_html_path(page_old, "".join([file.url, hash]), use_directory_urls)
squidfunk marked this conversation as resolved.
Show resolved Hide resolved

# If the redirect target isn't external or a valid internal page, throw an error
# Note: we use 'warn' here specifically; mkdocs treats warnings specially when in strict mode
Expand Down
11 changes: 8 additions & 3 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"""
import pytest

from mkdocs_redirects.plugin import get_relative_html_path
from mkdocs_redirects.plugin import get_relative_html_path, _split_hash_fragment
from mkdocs.structure.files import File
squidfunk marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.parametrize(
Expand All @@ -29,7 +30,9 @@
],
)
def test_relative_redirect_directory_urls(old_page, new_page, expected):
result = get_relative_html_path(old_page, new_page, use_directory_urls=True)
page_new_without_hash, hash = _split_hash_fragment(new_page)
file = File(page_new_without_hash, ".", ".", True)
result = get_relative_html_path(old_page, "".join([file.url, hash]), use_directory_urls=True)
squidfunk marked this conversation as resolved.
Show resolved Hide resolved

assert result == expected

Expand Down Expand Up @@ -60,6 +63,8 @@ def test_relative_redirect_directory_urls(old_page, new_page, expected):
],
)
def test_relative_redirect_no_directory_urls(old_page, new_page, expected):
result = get_relative_html_path(old_page, new_page, use_directory_urls=False)
page_new_without_hash, hash = _split_hash_fragment(new_page)
file = File(page_new_without_hash, ".", ".", False)
result = get_relative_html_path(old_page, "".join([file.url, hash]), use_directory_urls=False)
squidfunk marked this conversation as resolved.
Show resolved Hide resolved

assert result == expected