Skip to content
New issue

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

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

Already on GitHub? # to your account

Root/Site links support #53

Merged
merged 3 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions htmlproofer/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 7 additions & 1 deletion tests/integration/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
[GitHub Action](#github-action)

## Image Link absolute/relative

<a href="assets/hello-world.drawio.svg">![test](assets/hello-world.drawio.svg)</a>

<a href="/assets/hello-world.drawio.svg">![test](/assets/hello-world.drawio.svg)</a>
6 changes: 6 additions & 0 deletions tests/integration/docs/nested/page1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<a href="../assets/hello-world.drawio.svg">![test](../assets/hello-world.drawio.svg)</a>

<a href="/assets/hello-world.drawio.svg">![test](/assets/hello-world.drawio.svg)</a>
2 changes: 1 addition & 1 deletion tests/integration/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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