Skip to content

Fix internal anchors in included file not correctly rewritten #258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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