Skip to content

Commit 1062404

Browse files
authored
Fix internal anchor in included file incorrectly rewritten (#267)
1 parent aa17cc0 commit 1062404

File tree

6 files changed

+40
-5
lines changed

6 files changed

+40
-5
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{%
2+
include-markdown "../test.md"
3+
%}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
site_name: My Docs
2+
site_url: https://example.com/org-name/repo-name/
3+
4+
plugins:
5+
- include-markdown

examples/internal-anchors/test.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# First level heading
2+
3+
Example data
4+
5+
## Second level heading
6+
7+
Link to [second level heading](#second-level-heading).

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "mkdocs-include-markdown-plugin"
3-
version = "7.1.5"
3+
version = "7.1.6"
44
description = "Mkdocs Markdown includer plugin."
55
readme = "README.md"
66
license = "Apache-2.0"

src/mkdocs_include_markdown_plugin/process.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def rewrite_relative_urls(
291291
``destination_path``.
292292
"""
293293
def rewrite_url(url: str) -> str:
294-
if is_url(url) or is_absolute_path(url):
294+
if is_url(url) or is_absolute_path(url) or is_anchor(url):
295295
return url
296296

297297
new_path = os.path.relpath(
@@ -583,6 +583,17 @@ def is_absolute_path(string: str) -> bool:
583583
return False
584584

585585

586+
def is_anchor(string: str) -> bool:
587+
"""Check if a string looks like an anchor.
588+
589+
An anchor is a string that starts with `#` and is not a relative path.
590+
"""
591+
try:
592+
return string[0] == '#'
593+
except IndexError: # pragma: no cover
594+
return False
595+
596+
586597
def read_file(file_path: str, encoding: str) -> str:
587598
"""Read a file and return its content."""
588599
f = open(file_path, encoding=encoding) # noqa: SIM115

tests/test_unit/test_process.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
" class='bar'>example</a>"),
126126
id='html-anchor-single-quote',
127127
),
128-
# HTML Relative Links Adverarial tests:
128+
# HTML Relative Links Adversarial tests:
129129
# (attribute contains >, attribute without value, multiple tag in line)
130130
pytest.param(
131131
('<img id="foo" attr="3>2" attr2 src="assets/diagram.png"'
@@ -136,7 +136,7 @@
136136
('<img id="foo" attr="3>2" attr2 src="../assets/diagram.png"'
137137
' alt="diagram" class="bar" /><img id="foo" attr="3>2"'
138138
' src="../assets/diagram.png" alt="diagram" class="bar" />'),
139-
id='html-image-adverarial-test',
139+
id='html-image-adversarial-test',
140140
),
141141
pytest.param(
142142
('<a id="foo" attr="3>2" attr2 href="badge.png" class="bar">'
@@ -147,7 +147,7 @@
147147
('<a id="foo" attr="3>2" attr2 href="../badge.png" class="bar">'
148148
'foo</a><a id="foo" attr="3>2" href="../badge.png" class="bar">'
149149
'bar</a>'),
150-
id='html-anchor-adverarial-test',
150+
id='html-anchor-adversarial-test',
151151
),
152152
# HTML Relative Links Adversarial test: img no end slash
153153
pytest.param(
@@ -362,6 +362,15 @@
362362
# https://spec.commonmark.org/0.28/#indented-code-blocks
363363
id='no-exclude-indented-code-blocks-missing-newline-after',
364364
),
365+
# Internal anchor in included file
366+
# https://github.com/mondeja/mkdocs-include-markdown-plugin/issues/266
367+
pytest.param(
368+
'This is a link to an [internal anchor](#internal-anchor).',
369+
'README',
370+
'docs/nav.md',
371+
'This is a link to an [internal anchor](#internal-anchor).',
372+
id='internal-anchor',
373+
),
365374
),
366375
)
367376
def test_rewrite_relative_urls(

0 commit comments

Comments
 (0)