From afa465f8691f5d0d970acec4d87995cdd0a234ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar=20Rubio?= Date: Fri, 13 Jun 2025 20:04:30 +0200 Subject: [PATCH] Fix internal anchor in included file incorrectly rewritten --- examples/internal-anchors/docs/index.md | 3 +++ examples/internal-anchors/mkdocs.yml | 5 +++++ examples/internal-anchors/test.md | 7 +++++++ pyproject.toml | 2 +- src/mkdocs_include_markdown_plugin/process.py | 13 ++++++++++++- tests/test_unit/test_process.py | 15 ++++++++++++--- 6 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 examples/internal-anchors/docs/index.md create mode 100644 examples/internal-anchors/mkdocs.yml create mode 100644 examples/internal-anchors/test.md 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( ('diagramdiagram'), - 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(