Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.

Commit 87bbc25

Browse files
committed
fix: add default for unset or empty site_url
Also update the documentation accordingly and add tests. Closes #5
1 parent 9ff2eb5 commit 87bbc25

File tree

6 files changed

+61
-35
lines changed

6 files changed

+61
-35
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ trim_trailing_whitespace = true
1212
indent_style = space
1313
max_line_length = 90
1414

15-
[*.{yaml,yml}]
15+
[*.{yaml,yml,md}]
1616
indent_size = 2

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ A MkDocs plugin that adds support for site-relative `site:` URLs.
1212

1313
Example:
1414

15-
| URL | site_url | resulting URL |
16-
| --- | -------- | ------------- |
17-
| `site:images/foo.png` | `https://example.com/` | `/images/foo.png` |
18-
| `site:images/foo.png` | `https://example.com/bar/` | `/bar/images/foo.png` |
15+
| URL | site_url | resulting URL |
16+
| --------------------- | --------------------------- | ---------------------- |
17+
| `site:images/foo.png` | `https://example.com/` | `/images/foo.png` |
18+
| `site:images/foo.png` | `https://example.com/path/` | `/path/images/foo.png` |
19+
| `site:images/foo.png` | unset/empty | `/images/foo.png` |
1920

2021
**Please note**: This plugin requires MkDocs 1.5 or higher.
2122

@@ -67,6 +68,9 @@ plugins:
6768
This can also be used to interpret absolute URLs like `/example/file.png` as relative,
6869
by setting the `prefix` to `/`.
6970

71+
The plugin will extract the path prefix to use from the [`site_url`](https://www.mkdocs.org/user-guide/configuration/#site_url).
72+
configured in your `mkdocs.yaml`. If no `site_url` is configured, it will default to using the root path `/`.
73+
7074
## How it works
7175

7276
The plugin hooks into the [`on_page_content` event](https://www.mkdocs.org/dev-guide/plugins/#on_page_content)

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
site_name: MkDocs Site URLs Plugin
2-
site_url: !ENV [SITE_URL, "http://127.0.0.1:8000"]
2+
site_url: !ENV [SITE_URL, null]
33
dev_addr: 127.0.0.1:8000
44
repo_name: OctoPrint/mkdocs-site-urls
55
repo_url: https://github.com/OctoPrint/mkdocs-site-urls

mkdocs_site_urls/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ def on_config(self, config: MkDocsConfig) -> MkDocsConfig:
3333

3434
@mkdocs.plugins.event_priority(50)
3535
def on_page_content(self, html, page, config, files):
36-
site_url = config["site_url"]
36+
site_url = config.get("site_url")
37+
if not site_url:
38+
site_url = ""
3739
if not site_url.endswith("/"):
3840
site_url += "/"
3941
path = urllib.parse.urlparse(site_url).path

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ license_file = LICENSE
33

44
[flake8]
55
max-line-length = 90
6-
extend-ignore = E203, E231, E265, E266, E402, E501, E731, B023, B903, B904, B907, B950, W503
6+
extend-ignore = E203, E221, E222, E231, E265, E266, E402, E501, E731, B023, B903, B904, B907, B950, W503
77
select = B,C,E,F,W,T4,B9
88

99
[isort]

tests/test_mkdocs_site_urls.py

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,29 @@ def _plugin(config=mock_plugin_config, **kwargs):
2828
@pytest.mark.parametrize(
2929
"attributes, prefix, string_to_match, match_group1, match_group3, should_match",
3030
[
31+
(
32+
["data"],
33+
"site:",
34+
"data= 'site:image.png'",
35+
"data",
36+
"image.png",
37+
True,
38+
), # valid1
3139
(
3240
["href", "src"],
3341
"/docs/",
3442
'src = "/docs/image.png"',
3543
"src",
3644
"image.png",
3745
True,
38-
), # valid1
46+
), # valid2
3947
(
4048
["href", "src"],
4149
"/",
4250
"href ='/docs/image.png'",
4351
"href",
4452
"docs/image.png",
4553
True,
46-
), # valid2
47-
(
48-
["data"],
49-
"site:",
50-
"data= 'site:image.png'",
51-
"data",
52-
"image.png",
53-
True,
5454
), # valid3
5555
(
5656
["href", "src"],
@@ -117,38 +117,58 @@ def test_on_config_sets_regex(
117117

118118

119119
@pytest.mark.parametrize(
120-
"site_url",
120+
"prefix, site_url, expected_path",
121121
[
122-
"https://example.com/docs/subpage/",
123-
"https://example.com/docs/subpage",
122+
("site:", "https://example.com", "/"),
123+
("site:", "https://example.com/docs/subpage/", "/docs/subpage/"),
124+
("site:", "https://example.com/docs/subpage", "/docs/subpage/"),
125+
("site:", None, "/"),
126+
("site:", "", "/"),
127+
("prefix", None, "/"),
128+
("/", "https://example.com/docs/subpage/", "/docs/subpage/"),
129+
],
130+
ids=[
131+
"basic",
132+
"path_trailing_slash",
133+
"path_no_trailing_slash",
134+
"none_siteurl",
135+
"empty_siteurl",
136+
"custom_prefix",
137+
"slash_as_prefix",
124138
],
125-
ids=["trailing_slash", "no_trailing_slash"],
126139
)
127-
def test_on_page_content(create_plugin, site_url):
140+
def test_on_page_content(create_plugin, prefix, site_url, expected_path):
128141
"""Test the on_page_content method of the SiteUrlsPlugin."""
129142
plugin = create_plugin(
130143
{
131-
"attributes": ["src", "data"],
132-
"prefix": "prefix",
144+
"attributes": ["src", "data", "href"],
145+
"prefix": prefix,
133146
}
134147
)
135148
page = MagicMock()
136149
config = MagicMock()
137-
config.__getitem__.side_effect = lambda key: site_url if key == "site_url" else None
150+
config.get.side_effect = (
151+
lambda key, default=None: site_url
152+
if key == "site_url" and site_url is not None
153+
else default
154+
)
138155
files = MagicMock()
139-
html = """
140-
<img src ="prefixexample.png" alt="Image">
141-
<img data = \'prefix/image.png\'>
142-
<img data="site:docs/image.svg" class="example">
143-
<img attr ="prefix/docs/image.png" >
156+
157+
html = f"""
158+
<img src ="{prefix}example.png" alt="Image">
159+
<img data = \'{prefix}/image.png\'>
160+
<img data="{prefix}docs/image.svg" class="example">
161+
<img attr ="{prefix}/docs/image.png" title="this won't get touched, wrong attribute" >
162+
<img href = "NOT_A_PREFIX/foo/image.png" title="neither will this, wrong prefix">
144163
"""
145164

146165
plugin.on_config(config)
147166
result = plugin.on_page_content(html, page, config, files)
148-
expected_result = """
149-
<img src="/docs/subpage/example.png" alt="Image">
150-
<img data="/docs/subpage//image.png">
151-
<img data="site:docs/image.svg" class="example">
152-
<img attr ="prefix/docs/image.png" >
167+
expected_result = f"""
168+
<img src="{expected_path}example.png" alt="Image">
169+
<img data="{expected_path}/image.png">
170+
<img data="{expected_path}docs/image.svg" class="example">
171+
<img attr ="{prefix}/docs/image.png" title="this won't get touched, wrong attribute" >
172+
<img href = "NOT_A_PREFIX/foo/image.png" title="neither will this, wrong prefix">
153173
"""
154174
assert result == expected_result

0 commit comments

Comments
 (0)