Skip to content

Commit

Permalink
Add support for hash fragment redirect targets (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
wwarriner authored Aug 21, 2022
1 parent 9a14fd2 commit 52f95e0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
20 changes: 17 additions & 3 deletions mkdocs_redirects/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def write_html(site_dir, old_path, new_path):
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 = get_html_path(new_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 All @@ -65,7 +66,7 @@ def get_relative_html_path(old_page, new_page, use_directory_urls):
if use_directory_urls:
relative_path = relative_path + '/'

return relative_path
return relative_path + new_hash_fragment


def get_html_path(path, use_directory_urls):
Expand Down Expand Up @@ -125,12 +126,14 @@ 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))

# 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 in self.doc_pages:
elif page_new_without_hash in self.doc_pages:
dest_path = get_relative_html_path(page_old, page_new, use_directory_urls)

# If the redirect target isn't external or a valid internal page, throw an error
Expand All @@ -143,3 +146,14 @@ def on_post_build(self, config, **kwargs):
write_html(config['site_dir'],
get_html_path(page_old, use_directory_urls),
dest_path)


def _split_hash_fragment(path):
"""
Returns (path, hash-fragment)
"path/to/file#hash" => ("/path/to/file", "#hash")
"path/to/file" => ("/path/to/file", "")
"""
path, hash, after = path.partition('#')
return path, hash + after
18 changes: 18 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
("foo/old.md", "foo/new.md", "../new/"),
("foo/fizz/old.md", "foo/bar/new.md", "../../bar/new/"),
("fizz/old.md", "foo/bar/new.md", "../../foo/bar/new/"),
("old.md", "index.md#hash", "../#hash"),
("old.md", "README.md#hash", "../#hash"),
("old.md", "new.md#hash", "../new/#hash"),
("old.md", "new/index.md#hash", "../new/#hash"),
("old.md", "new/README.md#hash", "../new/#hash"),
("foo/old.md", "foo/new.md#hash", "../new/#hash"),
("foo/fizz/old.md", "foo/bar/new.md#hash", "../../bar/new/#hash"),
("fizz/old.md", "foo/bar/new.md#hash", "../../foo/bar/new/#hash"),
])
def test_relative_redirect_directory_urls(old_page, new_page, expected):
result = get_relative_html_path(old_page, new_page, use_directory_urls=True)
Expand All @@ -33,6 +41,16 @@ def test_relative_redirect_directory_urls(old_page, new_page, expected):
("fizz/old.md", "foo/bar/new.md", "../foo/bar/new.html"),
("foo.md", "foo/index.md", "foo/index.html"),
("foo.md", "foo/README.md", "foo/index.html"),
("old.md", "index.md#hash", "index.html#hash"),
("old.md", "README.md#hash", "index.html#hash"),
("old.md", "new.md#hash", "new.html#hash"),
("old.md", "new/index.md#hash", "new/index.html#hash"),
("old.md", "new/README.md#hash", "new/index.html#hash"),
("foo/old.md", "foo/new.md#hash", "new.html#hash"),
("foo/fizz/old.md", "foo/bar/new.md#hash", "../bar/new.html#hash"),
("fizz/old.md", "foo/bar/new.md#hash", "../foo/bar/new.html#hash"),
("foo.md", "foo/index.md#hash", "foo/index.html#hash"),
("foo.md", "foo/README.md#hash", "foo/index.html#hash"),
])
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)
Expand Down

0 comments on commit 52f95e0

Please sign in to comment.