diff --git a/htmlproofer/plugin.py b/htmlproofer/plugin.py index 87ae85c..af9eefc 100644 --- a/htmlproofer/plugin.py +++ b/htmlproofer/plugin.py @@ -160,8 +160,12 @@ def find_target_markdown(url: str, src_path: str, files: Dict[str, File]) -> Opt def find_source_file(url: str, src_path: str, files: Dict[str, File]) -> Optional[File]: """From a built URL, find the original file from the project that built it.""" - # Handle relative links by concatenating the source dir with the destination path - search_path = os.path.normpath(str(pathlib.Path(src_path).parent / pathlib.Path(url))) + if len(url) > 1 and url[0] == '/': + # Convert root/site paths + search_path = os.path.normpath(url[1:]) + else: + # Handle relative links by concatenating the source dir with the destination path + search_path = os.path.normpath(str(pathlib.Path(src_path).parent / pathlib.Path(url))) try: return files[search_path] diff --git a/tests/integration/docs/index.md b/tests/integration/docs/index.md index 70b1f6f..4d302a6 100644 --- a/tests/integration/docs/index.md +++ b/tests/integration/docs/index.md @@ -74,4 +74,10 @@ This work is based on the [mkdocs-markdownextradata-plugin](https://github.com/r ## GitHub Action ![GitHub Action](https://github.com/manuzhang/mkdocs-htmlproofer-plugin/actions/workflows/ci.yml/badge.svg){ #github-action } -[GitHub Action](#github-action) \ No newline at end of file +[GitHub Action](#github-action) + +## Image Link absolute/relative + +![test](assets/hello-world.drawio.svg) + +![test](/assets/hello-world.drawio.svg) diff --git a/tests/integration/docs/nested/page1.md b/tests/integration/docs/nested/page1.md index ba26388..c96b242 100644 --- a/tests/integration/docs/nested/page1.md +++ b/tests/integration/docs/nested/page1.md @@ -23,3 +23,9 @@ or to a nested page But allows valid anchors such as [Main Page](../index.md#mkdocs-htmlproofer-plugin) and [Table of Contents](../index.md#table-of-contents). + +## Image Link absolute/relative + +![test](../assets/hello-world.drawio.svg) + +![test](/assets/hello-world.drawio.svg) diff --git a/tests/integration/mkdocs.yml b/tests/integration/mkdocs.yml index bb7a878..f79ce65 100644 --- a/tests/integration/mkdocs.yml +++ b/tests/integration/mkdocs.yml @@ -6,7 +6,7 @@ markdown_extensions: - attr_list plugins: - htmlproofer: - raise_error: True + raise_error_after_finish: True raise_error_excludes: 504: ['*'] 404: ['https://www.mkdocs.org/user-guide/plugin', diff --git a/tests/unit/test_plugin.py b/tests/unit/test_plugin.py index 39aacc3..b93f3da 100644 --- a/tests/unit/test_plugin.py +++ b/tests/unit/test_plugin.py @@ -189,6 +189,7 @@ def test_get_url_status__non_markdown_page(plugin): ])} assert plugin.get_url_status('drawing.svg', 'index.md', set(), files, False) == 0 + assert plugin.get_url_status('/drawing.svg', 'index.md', set(), files, False) == 0 assert plugin.get_url_status('not-existing.svg', 'index.md', set(), files, False) == 404 @@ -214,3 +215,5 @@ def test_get_url_status__local_page_nested(plugin): assert plugin.get_url_status('foo/bar/nested.html#nested-one', 'index.md', set(), files, False) == 0 assert plugin.get_url_status('foo/baz/nested.html#nested-two', 'index.md', set(), files, False) == 0 + + assert plugin.get_url_status('/index.html', 'foo/baz/sibling.md', set(), files, False) == 0