Skip to content

Commit 253cbc4

Browse files
authored
Fix internal anchors in included file not correctly rewritten (#258)
1 parent 1b73aef commit 253cbc4

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "mkdocs-include-markdown-plugin"
3-
version = "7.1.3"
3+
version = "7.1.4"
44
description = "Mkdocs Markdown includer plugin."
55
readme = "README.md"
66
license = "Apache-2.0"

src/mkdocs_include_markdown_plugin/process.py

+4
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ def rewrite_url(url: str) -> str:
315315
except IndexError: # pragma: no cover
316316
pass
317317

318+
# ensure that links to the same file are not rewritten
319+
if new_path == '.':
320+
new_path = ''
321+
318322
return urlunparse((scheme, netloc, new_path, params, query, fragment))
319323

320324
def found_href(m: re.Match[str], url_group_index: int = -1) -> str:

tests/test_unit/test_include.py

-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,6 @@
353353
[],
354354
id='rstrip-windows-trailing-newlines',
355355
),
356-
357356
),
358357
)
359358
def test_include(

tests/test_unit/test_include_markdown.py

+27
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,33 @@
729729
[],
730730
id='include-code-block-to-list-item (#123)',
731731
),
732+
733+
# Internal anchor in included file properly rewritten
734+
pytest.param(
735+
'''# Header
736+
737+
{% include-markdown "{filepath}" %}''',
738+
'''# First level heading
739+
740+
Example data
741+
742+
## Second level heading
743+
744+
Link to [second level heading](#second-level-heading).
745+
''',
746+
'''# Header
747+
748+
# First level heading
749+
750+
Example data
751+
752+
## Second level heading
753+
754+
Link to [second level heading](#second-level-heading).
755+
''',
756+
[],
757+
id='internal-anchor',
758+
),
732759
),
733760
)
734761
def test_include_markdown(

0 commit comments

Comments
 (0)