Skip to content

Commit

Permalink
Fix internal anchors in included file not correctly rewritten (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja authored Feb 9, 2025
1 parent 1b73aef commit 253cbc4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "mkdocs-include-markdown-plugin"
version = "7.1.3"
version = "7.1.4"
description = "Mkdocs Markdown includer plugin."
readme = "README.md"
license = "Apache-2.0"
Expand Down
4 changes: 4 additions & 0 deletions src/mkdocs_include_markdown_plugin/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ def rewrite_url(url: str) -> str:
except IndexError: # pragma: no cover
pass

# ensure that links to the same file are not rewritten
if new_path == '.':
new_path = ''

return urlunparse((scheme, netloc, new_path, params, query, fragment))

def found_href(m: re.Match[str], url_group_index: int = -1) -> str:
Expand Down
1 change: 0 additions & 1 deletion tests/test_unit/test_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@
[],
id='rstrip-windows-trailing-newlines',
),
),
)
def test_include(
Expand Down
27 changes: 27 additions & 0 deletions tests/test_unit/test_include_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,33 @@
[],
id='include-code-block-to-list-item (#123)',
),
# Internal anchor in included file properly rewritten
pytest.param(
'''# Header
{% include-markdown "{filepath}" %}''',
'''# First level heading
Example data
## Second level heading
Link to [second level heading](#second-level-heading).
''',
'''# Header
# First level heading
Example data
## Second level heading
Link to [second level heading](#second-level-heading).
''',
[],
id='internal-anchor',
),
),
)
def test_include_markdown(
Expand Down

0 comments on commit 253cbc4

Please # to comment.