diff --git a/examples/internal-anchors/docs/index.md b/examples/internal-anchors/docs/index.md
new file mode 100644
index 0000000..8515d2f
--- /dev/null
+++ b/examples/internal-anchors/docs/index.md
@@ -0,0 +1,3 @@
+{%
+ include-markdown "../test.md"
+%}
diff --git a/examples/internal-anchors/mkdocs.yml b/examples/internal-anchors/mkdocs.yml
new file mode 100644
index 0000000..422f47f
--- /dev/null
+++ b/examples/internal-anchors/mkdocs.yml
@@ -0,0 +1,5 @@
+site_name: My Docs
+site_url: https://example.com/org-name/repo-name/
+
+plugins:
+ - include-markdown
diff --git a/examples/internal-anchors/test.md b/examples/internal-anchors/test.md
new file mode 100644
index 0000000..1670adc
--- /dev/null
+++ b/examples/internal-anchors/test.md
@@ -0,0 +1,7 @@
+# First level heading
+
+Example data
+
+## Second level heading
+
+Link to [second level heading](#second-level-heading).
diff --git a/pyproject.toml b/pyproject.toml
index ec376f9..089338c 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[project]
name = "mkdocs-include-markdown-plugin"
-version = "7.1.5"
+version = "7.1.6"
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 b7318a3..f16eedd 100644
--- a/src/mkdocs_include_markdown_plugin/process.py
+++ b/src/mkdocs_include_markdown_plugin/process.py
@@ -291,7 +291,7 @@ def rewrite_relative_urls(
``destination_path``.
"""
def rewrite_url(url: str) -> str:
- if is_url(url) or is_absolute_path(url):
+ if is_url(url) or is_absolute_path(url) or is_anchor(url):
return url
new_path = os.path.relpath(
@@ -583,6 +583,17 @@ def is_absolute_path(string: str) -> bool:
return False
+def is_anchor(string: str) -> bool:
+ """Check if a string looks like an anchor.
+
+ An anchor is a string that starts with `#` and is not a relative path.
+ """
+ try:
+ return string[0] == '#'
+ except IndexError: # pragma: no cover
+ return False
+
+
def read_file(file_path: str, encoding: str) -> str:
"""Read a file and return its content."""
f = open(file_path, encoding=encoding) # noqa: SIM115
diff --git a/tests/test_unit/test_process.py b/tests/test_unit/test_process.py
index 064ea86..3bf5034 100644
--- a/tests/test_unit/test_process.py
+++ b/tests/test_unit/test_process.py
@@ -125,7 +125,7 @@
" class='bar'>example"),
id='html-anchor-single-quote',
),
- # HTML Relative Links Adverarial tests:
+ # HTML Relative Links Adversarial tests:
# (attribute contains >, attribute without value, multiple tag in line)
pytest.param(
('
'),
- id='html-image-adverarial-test',
+ id='html-image-adversarial-test',
),
pytest.param(
(''
@@ -147,7 +147,7 @@
(''
'foo'
'bar'),
- id='html-anchor-adverarial-test',
+ id='html-anchor-adversarial-test',
),
# HTML Relative Links Adversarial test: img no end slash
pytest.param(
@@ -362,6 +362,15 @@
# https://spec.commonmark.org/0.28/#indented-code-blocks
id='no-exclude-indented-code-blocks-missing-newline-after',
),
+ # Internal anchor in included file
+ # https://github.com/mondeja/mkdocs-include-markdown-plugin/issues/266
+ pytest.param(
+ 'This is a link to an [internal anchor](#internal-anchor).',
+ 'README',
+ 'docs/nav.md',
+ 'This is a link to an [internal anchor](#internal-anchor).',
+ id='internal-anchor',
+ ),
),
)
def test_rewrite_relative_urls(