Skip to content
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
3 changes: 3 additions & 0 deletions examples/internal-anchors/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{%
include-markdown "../test.md"
%}
5 changes: 5 additions & 0 deletions examples/internal-anchors/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
site_name: My Docs
site_url: https://example.com/org-name/repo-name/

plugins:
- include-markdown
7 changes: 7 additions & 0 deletions examples/internal-anchors/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# First level heading

Example data

## Second level heading

Link to [second level heading](#second-level-heading).
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.5"
version = "7.1.6"
description = "Mkdocs Markdown includer plugin."
readme = "README.md"
license = "Apache-2.0"
Expand Down
13 changes: 12 additions & 1 deletion src/mkdocs_include_markdown_plugin/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions tests/test_unit/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
" class='bar'>example</a>"),
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(
('<img id="foo" attr="3>2" attr2 src="assets/diagram.png"'
Expand All @@ -136,7 +136,7 @@
('<img id="foo" attr="3>2" attr2 src="../assets/diagram.png"'
' alt="diagram" class="bar" /><img id="foo" attr="3>2"'
' src="../assets/diagram.png" alt="diagram" class="bar" />'),
id='html-image-adverarial-test',
id='html-image-adversarial-test',
),
pytest.param(
('<a id="foo" attr="3>2" attr2 href="badge.png" class="bar">'
Expand All @@ -147,7 +147,7 @@
('<a id="foo" attr="3>2" attr2 href="../badge.png" class="bar">'
'foo</a><a id="foo" attr="3>2" href="../badge.png" class="bar">'
'bar</a>'),
id='html-anchor-adverarial-test',
id='html-anchor-adversarial-test',
),
# HTML Relative Links Adversarial test: img no end slash
pytest.param(
Expand Down Expand Up @@ -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(
Expand Down