Skip to content

Commit 0604be2

Browse files
authored
Add new setting include_from_url(#299)
1 parent f0809f9 commit 0604be2

12 files changed

Lines changed: 144 additions & 43 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,18 @@ plugins:
142142
include: replace
143143
```
144144

145+
<!-- mdpo-disable-next-line -->
146+
147+
#### `include_from_url`
148+
149+
Allow including content from URLs.
150+
151+
```yaml
152+
plugins:
153+
- include-markdown:
154+
include_from_url: true
155+
```
156+
145157
### Reference
146158

147159
This plugin provides two directives, one to include Markdown files and another

locale/es/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ plugins:
125125
include: replace
126126
```
127127

128+
#### `include_from_url`
129+
130+
Permite incluir contenido desde URLs.
131+
132+
```yaml
133+
plugins:
134+
- include-markdown:
135+
include_from_url: true
136+
```
137+
128138
### Referencia
129139

130140
Este plugin provee dos directivas, una para incluir archivos Markdown y otra para

locale/es/README.md.po

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,6 @@ msgstr ""
480480

481481
msgid "Common arguments"
482482
msgstr "Argumentos comunes"
483+
484+
msgid "Allow including content from URLs."
485+
msgstr "Permite incluir contenido desde URLs."

locale/fr/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ plugins:
126126
include: replace
127127
```
128128

129+
#### `include_from_url`
130+
131+
Autoriser l'inclusion de contenu provenant d'URL.
132+
133+
```yaml
134+
plugins:
135+
- include-markdown:
136+
include_from_url: true
137+
```
138+
129139
### Référence
130140

131141
Ce plugin fournit deux directives, une pour inclure des fichiers Markdown et une

locale/fr/README.md.po

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,6 @@ msgstr ""
478478

479479
msgid "Common arguments"
480480
msgstr "Arguments communs"
481+
482+
msgid "Allow including content from URLs."
483+
msgstr "Autoriser l'inclusion de contenu provenant d'URL."

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "mkdocs-include-markdown-plugin"
3-
version = "7.2.2"
3+
version = "7.3.0"
44
description = "Mkdocs Markdown includer plugin."
55
readme = "README.md"
66
license = "Apache-2.0"

src/mkdocs_include_markdown_plugin/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ class PluginConfig(Config): # noqa: D101
3535
'include-markdown': 'include-markdown',
3636
},
3737
)
38+
include_from_url = MkType(bool, default=True)

src/mkdocs_include_markdown_plugin/event.py

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
@dataclass
5656
class Settings: # noqa: D101
5757
exclude: list[str] | None
58+
include_from_url: bool = False
5859

5960

6061
def get_file_content( # noqa: PLR0913, PLR0915
@@ -169,14 +170,25 @@ def found_include_tag( # noqa: PLR0912, PLR0915
169170
order,
170171
)
171172

172-
if is_url and 'order' in used_arguments: # pragma: no cover
173-
location = process.file_lineno_message(
174-
page_src_path, docs_dir, directive_lineno(),
175-
)
176-
logger.warning(
177-
f"Ignoring 'order' argument of 'include' directive"
178-
f" at {location} because the included path is a URL",
179-
)
173+
if is_url:
174+
if 'order' in used_arguments: # pragma: no cover
175+
location = process.file_lineno_message(
176+
page_src_path, docs_dir, directive_lineno(),
177+
)
178+
logger.warning(
179+
f"Ignoring 'order' argument of 'include' directive"
180+
f" at {location} because the included path is a URL",
181+
)
182+
183+
if not settings.include_from_url:
184+
location = process.file_lineno_message(
185+
page_src_path, docs_dir, directive_lineno(),
186+
)
187+
raise PluginError(
188+
f'Including from URL at {location} is not allowed because'
189+
' include-markdown is configured with include_from_url set'
190+
' to false',
191+
)
180192

181193
if not file_paths_to_include:
182194
location = process.file_lineno_message(
@@ -414,14 +426,25 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
414426
order,
415427
)
416428

417-
if is_url and 'order' in used_arguments: # pragma: no cover
418-
location = process.file_lineno_message(
419-
page_src_path, docs_dir, directive_lineno(),
420-
)
421-
logger.warning(
422-
f"Ignoring 'order' argument of 'include-markdown' directive"
423-
f" at {location} because the included path is a URL",
424-
)
429+
if is_url:
430+
if 'order' in used_arguments: # pragma: no cover
431+
location = process.file_lineno_message(
432+
page_src_path, docs_dir, directive_lineno(),
433+
)
434+
logger.warning(
435+
f"Ignoring 'order' argument of 'include-markdown' directive"
436+
f" at {location} because the included path is a URL",
437+
)
438+
439+
if not settings.include_from_url:
440+
location = process.file_lineno_message(
441+
page_src_path, docs_dir, directive_lineno(),
442+
)
443+
raise PluginError(
444+
f'Including from URL at {location} is not allowed because'
445+
' include-markdown is configured with include_from_url set'
446+
' to false',
447+
)
425448

426449
if not file_paths_to_include:
427450
location = process.file_lineno_message(
@@ -713,6 +736,7 @@ def on_page_markdown(
713736
},
714737
Settings(
715738
exclude=config.exclude,
739+
include_from_url=config.include_from_url,
716740
),
717741
files_watcher=plugin._files_watcher,
718742
http_cache=plugin._cache or http_cache,

tests/test_integration/test_cache_integration.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
from mkdocs.exceptions import PluginError
55

66
import mkdocs_include_markdown_plugin.cache
7-
from mkdocs_include_markdown_plugin import IncludeMarkdownPlugin
87
from mkdocs_include_markdown_plugin.cache import (
98
Cache,
109
get_cache_directory,
1110
initialize_cache,
1211
is_platformdirs_installed,
1312
)
1413
from mkdocs_include_markdown_plugin.event import on_page_markdown
15-
from testing_helpers import FakeConfig, parametrize_directives
14+
from testing_helpers import parametrize_directives
1615

1716

1817
@pytest.mark.parametrize(
@@ -80,14 +79,14 @@ def run():
8079
os.remove(file_path)
8180

8281

83-
def test_cache_setting_when_not_available_raises_error(monkeypatch):
82+
def test_cache_setting_when_not_available_raises_error(plugin, monkeypatch):
8483
monkeypatch.setattr(
8584
mkdocs_include_markdown_plugin.cache,
8685
'is_platformdirs_installed',
8786
lambda: False,
8887
)
89-
plugin = IncludeMarkdownPlugin()
90-
plugin.config = FakeConfig(cache=600, cache_dir='')
88+
monkeypatch.setattr(plugin.config, 'cache', 600)
89+
monkeypatch.setattr(plugin.config, 'cache_dir', '')
9190
with pytest.raises(PluginError) as exc:
9291
plugin.on_config({})
9392
assert (
@@ -96,12 +95,12 @@ def test_cache_setting_when_not_available_raises_error(monkeypatch):
9695
) in str(exc.value)
9796

9897

99-
def test_cache_setting_available_with_cache_dir(monkeypatch):
98+
def test_cache_setting_available_with_cache_dir(plugin, monkeypatch):
10099
monkeypatch.setattr(
101100
mkdocs_include_markdown_plugin.cache,
102101
'is_platformdirs_installed',
103102
lambda: False,
104103
)
105-
plugin = IncludeMarkdownPlugin()
106-
plugin.config = FakeConfig(cache=600, cache_dir='foo')
104+
monkeypatch.setattr(plugin.config, 'cache', 600)
105+
monkeypatch.setattr(plugin.config, 'cache_dir', 'foo')
107106
plugin.on_config({})
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Tests for the ``include_from_url`` global setting."""
2+
3+
import pytest
4+
from mkdocs.exceptions import PluginError
5+
6+
from mkdocs_include_markdown_plugin.event import on_page_markdown
7+
from testing_helpers import mock_read_url, parametrize_directives
8+
9+
10+
URL = (
11+
'https://raw.githubusercontent.com/mondeja/'
12+
'mkdocs-include-markdown-plugin/master/examples/basic/docs/included.md'
13+
)
14+
URL_CONTENT = '''Some ignored content.
15+
16+
<--start-->
17+
18+
Some included content.
19+
'''
20+
21+
22+
@parametrize_directives
23+
def test_include_from_url_disabled_by_default_raises(
24+
directive, page, tmp_path, plugin, monkeypatch,
25+
):
26+
monkeypatch.setattr(plugin.config, 'include_from_url', False)
27+
mock_read_url(monkeypatch, URL_CONTENT)
28+
includer = tmp_path / 'includer.md'
29+
content = f'{{% {directive} "{URL}" %}}'
30+
includer.write_text(content, encoding='utf-8')
31+
32+
with pytest.raises(PluginError) as exc:
33+
on_page_markdown(content, page(includer), tmp_path, plugin)
34+
assert 'include_from_url set to false' in str(exc.value)
35+
36+
37+
@parametrize_directives
38+
def test_include_from_url_enabled_allows_url(
39+
directive, page, tmp_path, plugin, monkeypatch,
40+
):
41+
monkeypatch.setattr(plugin.config, 'include_from_url', True)
42+
mock_read_url(monkeypatch, URL_CONTENT)
43+
44+
includer = tmp_path / 'includer.md'
45+
content = f'{{% {directive} "{URL}" %}}'
46+
includer.write_text(content, encoding='utf-8')
47+
48+
result = on_page_markdown(content, page(includer), tmp_path, plugin)
49+
assert 'Some included content.' in result

0 commit comments

Comments
 (0)