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

Commit 2e6f084

Browse files
committed
✨ Make checked attributes configurable
Closes #1
1 parent 8738889 commit 2e6f084

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# MkDocs Site URLs
1+
# MkDocs Site URLs Plugin
22

33
A MkDocs plugin that adds support for site-relative `site:` URLs.
44

@@ -11,7 +11,7 @@ Example:
1111

1212
**Please note**: This plugin requires MkDocs 1.5 or higher.
1313

14-
## Usage
14+
## Getting Started
1515

1616
1. Install the plugin from PyPI
1717
```bash
@@ -22,18 +22,35 @@ Example:
2222
plugins:
2323
- site-urls
2424
```
25-
There are no configuration options.
2625
3. Start using site-relative URLs in your Markdown files by prefixing them with `site:`:
2726
```markdown
2827
[Link to another page](site:another-page/relative/to/the/site/root)
2928
3029
![Image](site:images/foo.png)
3130
```
3231

32+
## Configuration
33+
34+
By default the plugin will replace URLs in `href`, `src` and `data` attributes. You can configure the attributes to replace
35+
by setting the `attributes` option in your `mkdocs.yml`, e.g.:
36+
37+
```yaml
38+
plugins:
39+
- site-urls:
40+
attributes:
41+
- href
42+
- src
43+
- data
44+
- data-url
45+
```
46+
47+
Be advised that in case of any customization on your part you need to include the default attributes as well if you want
48+
to keep them, as the default list will not be included automatically anymore.
49+
3350
## How it works
3451

3552
The plugin hooks into the [`on_page_content` event](https://www.mkdocs.org/dev-guide/plugins/#on_page_content)
36-
and replaces all URLs in `href` or `src` attributes in the rendered HTML with the corresponding site-relative URLs.
53+
and replaces all URLs in the configured attributes (by default `href`, `src` or `data`) in the rendered HTML with the corresponding site-relative URLs.
3754

3855
## License
3956

mkdocs_site_urls/__init__.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@
22
import urllib.parse
33

44
import mkdocs.plugins
5-
6-
SITE_URLS_REGEX = re.compile(r'(href|src)="site:([^"]+)"', re.IGNORECASE)
5+
from mkdocs.config import config_options as c
6+
from mkdocs.config.defaults import MkDocsConfig
77

88
logger = mkdocs.plugins.get_plugin_logger(__name__)
99

1010

11-
class SiteUrlsPlugin(mkdocs.plugins.BasePlugin):
11+
class SiteUrlsConfig(mkdocs.config.base.Config):
12+
attributes = c.Type(list, default=["href", "src", "data"])
13+
14+
15+
class SiteUrlsPlugin(mkdocs.plugins.BasePlugin[SiteUrlsConfig]):
16+
def on_pre_build(self, *, config: MkDocsConfig) -> None:
17+
self._regex = re.compile(
18+
r"(" + "|".join(self.config["attributes"]) + r')="site:([^"]+)"',
19+
re.IGNORECASE,
20+
)
21+
1222
@mkdocs.plugins.event_priority(50)
1323
def on_page_content(self, html, page, config, files):
1424
site_url = config["site_url"]
@@ -25,7 +35,7 @@ def _replace(match):
2535
if url.startswith("/"):
2636
url = url[1:]
2737

28-
logger.info(f"Replacing site:{match.group(2)} with {path}{url}...")
38+
logger.info(f"Replacing site:{match.group(2)} with {path}{url}")
2939
return f'{param}="{path}{url}"'
3040

31-
return SITE_URLS_REGEX.sub(_replace, html)
41+
return self._regex.sub(_replace, html)

0 commit comments

Comments
 (0)