Skip to content
Open
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
14 changes: 13 additions & 1 deletion src/mkdocs_include_markdown_plugin/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import stat
from collections.abc import Callable, Iterator
from typing import TYPE_CHECKING
from urllib.parse import urljoin


if TYPE_CHECKING: # pragma: no cover
Expand Down Expand Up @@ -291,8 +292,19 @@ def rewrite_relative_urls(
``source_path`` will still work when inserted into a file at
``destination_path``.
"""
source_is_url = is_url(source_path)

def rewrite_url(url: str) -> str:
if is_url(url) or is_absolute_path(url) or is_anchor(url):
if is_anchor(url):
return url

if source_is_url:
# `source_path` is a remote URL, so relative and
# root-relative references must resolve against it rather
# than against the local `destination_path`.
return url if is_url(url) else urljoin(source_path, url)

if is_url(url) or is_absolute_path(url):
return url

new_path = os.path.relpath(
Expand Down
47 changes: 47 additions & 0 deletions tests/test_unit/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,53 @@
'This is a link to an [internal anchor](#internal-anchor).',
id='internal-anchor',
),
# Remote source (content included from a URL): relative and
# root-relative references must resolve against that URL, not
# against the local destination file.
# https://github.com/mondeja/mkdocs-include-markdown-plugin/issues/302
pytest.param(
"Here's a diagram: ![diagram](assets/diagram.png)",
'https://raw.githubusercontent.com/user/repo/main/README.md',
'docs/home.md',
(
"Here's a diagram: ![diagram]("
'https://raw.githubusercontent.com/user/repo/main'
'/assets/diagram.png)'
),
id='image-remote-source',
),
pytest.param(
'[link](../CHANGELOG.md)',
'https://raw.githubusercontent.com/user/repo/main/docs/nav.md',
'docs/home.md',
(
'[link]('
'https://raw.githubusercontent.com/user/repo/main'
'/CHANGELOG.md)'
),
id='relative-link-up-remote-source',
),
pytest.param(
'[abs](/images/diagram.png)',
'https://raw.githubusercontent.com/user/repo/main/README.md',
'docs/home.md',
'[abs](https://raw.githubusercontent.com/images/diagram.png)',
id='root-relative-remote-source',
),
pytest.param(
'[ext](https://example.com/index.html)',
'https://raw.githubusercontent.com/user/repo/main/README.md',
'docs/home.md',
'[ext](https://example.com/index.html)',
id='external-link-remote-source-unchanged',
),
pytest.param(
'This is a link to an [internal anchor](#internal-anchor).',
'https://raw.githubusercontent.com/user/repo/main/README.md',
'docs/home.md',
'This is a link to an [internal anchor](#internal-anchor).',
id='internal-anchor-remote-source',
),
),
)
def test_rewrite_relative_urls(
Expand Down