From a5ed52e79f2c4124b1df6a1a339005d90bff20ba Mon Sep 17 00:00:00 2001 From: Andrea Frittoli Date: Mon, 9 Nov 2020 15:00:55 +0000 Subject: [PATCH] Fix the case of non-md files in nested folders In case of non-md files in nested folders, the filename used in the rewrite included the target path leading to double-path in the link. Fixed that and added a use case for the non-md file in a subfolder. Signed-off-by: Andrea Frittoli --- sync/sync.py | 8 ++++---- sync/test-content/nested/example.yaml | 0 sync/test_sync.py | 13 ++++++++++--- 3 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 sync/test-content/nested/example.yaml diff --git a/sync/sync.py b/sync/sync.py index 6b92632..ce231b0 100755 --- a/sync/sync.py +++ b/sync/sync.py @@ -266,16 +266,16 @@ def transform_link(link, base_path, local_files, rewrite_path, rewrite_url): if ext == '.md': # Links to the index file are rendered as base_path/ if is_index: - path = '' + target_file = '' # links to md other files are rendered as .../[md filename]/ else: - path = filename + '/' + target_file = filename + '/' # for .md files, lower the case of fragments to match hugo's behaviour parsed = parsed._replace(fragment=parsed.fragment.lower()) if target_folder: - new_path = [rewrite_path, target_folder, path] + new_path = [rewrite_path, target_folder, target_file] else: - new_path = [rewrite_path, path] + new_path = [rewrite_path, target_file] return parsed._replace(path="/".join(new_path)).geturl() # when not found on disk, append to the base_url return urljoin(rewrite_url, parsed._replace(path=fq_path).geturl()) diff --git a/sync/test-content/nested/example.yaml b/sync/test-content/nested/example.yaml new file mode 100644 index 0000000..e69de29 diff --git a/sync/test_sync.py b/sync/test_sync.py index 2392f06..83e75a5 100644 --- a/sync/test_sync.py +++ b/sync/test_sync.py @@ -225,7 +225,9 @@ class TestSync(unittest.TestCase): local_files = { 'test-content/content.md': ('_index.md', ''), 'test-content/test.txt': ('test.txt', ''), - 'another-content/test.md': ('test.md', 'another') + 'another-content/test.md': ('test.md', 'another'), + 'test-content/nested/content.md': ('content.md', 'nested'), + 'test-content/nested/example.yaml': ('example.yaml', 'nested') } cases = [ @@ -234,7 +236,9 @@ class TestSync(unittest.TestCase): "test.txt", "content.md", "notthere.txt", - "../another-content/test.md" + "../another-content/test.md", + "./nested/content.md", + "./nested/example.yaml" ] expected_results = [ @@ -243,7 +247,9 @@ class TestSync(unittest.TestCase): "/docs/foo/test.txt", "/docs/foo/", "https://foo.bar/test-content/notthere.txt", - "/docs/foo/another/test/" + "/docs/foo/another/test/", + "/docs/foo/nested/content/", + "/docs/foo/nested/example.yaml" ] for case, expected in zip(cases, expected_results): @@ -385,5 +391,6 @@ class TestSync(unittest.TestCase): base_path='/docs/test', base_url=f'http://test.com/test/tree/{self.tagname}/') + if __name__ == '__main__': unittest.main() -- GitLab