Repository: mondeja/mkdocs-include-markdown-plugin (python)

Issue:
rewrite-relative-urls and internal anchors

### Discussed in https://github.com/mondeja/mkdocs-include-markdown-plugin/discussions/253

<div type='discussions-op-text'>

<sup>Originally posted by **@Jan-1500-100-900** January 24, 2025</sup>
Hi.
I've been using this plugin to place the same file in multiple places in the TOC.

I made duplicate files that are empty except for the relative include line to the original file. So like this:

{%
include-markdown "../../filename.md"
%}

However, internal anchors (#chapter) in the included document don't work.
More specifically:
If I set rewrite-relative-urls=true then external relative links work but internal anchors don't.
If I set rewrite-relative-urls=false then internal anchors work, but external relative links don't.

How do I make it that both anchors and external links work?</div>

Pull request:
Fix internal anchors in included file not correctly rewritten
Fixes #254

Patch:
diff --git a/pyproject.toml b/pyproject.toml
index 3ed2640..582a4da 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -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"
diff --git a/src/mkdocs_include_markdown_plugin/process.py b/src/mkdocs_include_markdown_plugin/process.py
index 3c47550..9908cf6 100644
--- a/src/mkdocs_include_markdown_plugin/process.py
+++ b/src/mkdocs_include_markdown_plugin/process.py
@@ -315,6 +315,10 @@ def rewrite_relative_urls(
         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:


Test patch:
diff --git a/tests/test_unit/test_include.py b/tests/test_unit/test_include.py
index 2c1bf13..54a49e1 100644
--- a/tests/test_unit/test_include.py
+++ b/tests/test_unit/test_include.py
@@ -353,7 +353,6 @@ Some included content.
             [],
             id='rstrip-windows-trailing-newlines',
         ),
-
     ),
 )
 def test_include(
diff --git a/tests/test_unit/test_include_markdown.py b/tests/test_unit/test_include_markdown.py
index e4afbe6..3d874d6 100644
--- a/tests/test_unit/test_include_markdown.py
+++ b/tests/test_unit/test_include_markdown.py
@@ -729,6 +729,33 @@ With some text after it
             [],
             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(