Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTML support for relative url rewrites #252

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 35 additions & 5 deletions src/mkdocs_include_markdown_plugin/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,17 @@
MARKDOWN_HTML_IMAGE_REGEX = re.compile(
maekawataiki marked this conversation as resolved.
Show resolved Hide resolved
r'''
<img
[^>]*
(?:\s+ # More than one whitespace
(?!src=) # Not src=
[\w-]+ # attribute name
\s*=\s* # arbitrary whitespace
(?:
"[^"]*" # Quoted value
|
'[^']*' # Quoted value
)
maekawataiki marked this conversation as resolved.
Show resolved Hide resolved
)* # Other attributes are repeated 0 or more times
\s+ # More than one whitespace
src="(\S+?)" # src = $1
''',
flags=re.VERBOSE | re.MULTILINE,
Expand All @@ -128,7 +138,17 @@
MARKDOWN_HTML_SOURCE_REGEX = re.compile(
r'''
<source
[^>]*
(?:\s+ # More than one whitespace
(?!src=) # Not src=
[\w-]+ # attribute name
\s*=\s* # arbitrary whitespace
(?:
"[^"]*" # Quoted value
|
'[^']*' # Quoted value
)
)* # Other attributes are repeated 0 or more times
\s+ # More than one whitespace
src="(\S+?)" # src = $1
''',
flags=re.VERBOSE | re.MULTILINE,
Expand All @@ -139,7 +159,17 @@
MARKDOWN_HTML_ANCHOR_DEFINITION_REGEX = re.compile(
r'''
<a
[^>]*
(?:\s+ # More than one whitespace
(?!src=) # Not src=
[\w-]+ # attribute name
\s*=\s* # arbitrary whitespace
(?:
"[^"]*" # Quoted value
|
'[^']*' # Quoted value
)
)* # Other attributes are repeated 0 or more times
\s+ # More than one whitespace
href="(\S+?)" # href = $1
''',
flags=re.VERBOSE | re.MULTILINE,
Expand Down Expand Up @@ -282,8 +312,8 @@ def rewrite_relative_urls(
def rewrite_url(url: str) -> str:
scheme, netloc, path, params, query, fragment = urlparse(url)

# absolute or mail
if path.startswith('/') or scheme == 'mailto':
# external or absolute or mail
if (is_url(url) or path.startswith('/') or scheme == 'mailto'):
return url

new_path = os.path.relpath(
Expand Down
74 changes: 70 additions & 4 deletions tests/test_unit/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@
"Here's a [link](../CHANGELOG.md) to the changelog.",
id='relative-link',
),
pytest.param(
"Here's a [link](/CHANGELOG.md) to the changelog.",
'README',
'docs/nav.md',
"Here's a [link](/CHANGELOG.md) to the changelog.",
id='absolute-link',
),
pytest.param(
"Here's a [link](https://example.com/index.html) to the changelog.",
'README',
'docs/nav.md',
"Here's a [link](https://example.com/index.html) to the changelog.",
id='external-link',
),
pytest.param(
"Here's a [link](https://example.com) to the changelog.",
'README',
'docs/nav.md',
"Here's a [link](https://example.com) to the changelog.",
id='external-top-level-link',
),
pytest.param(
'''Here's a [link whose text is really long and so is broken across
multiple lines](CHANGELOG.md) to the changelog.
Expand Down Expand Up @@ -79,17 +100,17 @@
id='image-inside-link',
),
pytest.param(
'Here\'s a diagram: <img id="foo" src="assets/diagram.png" alt="diagram" class="bar">',
'Here\'s a diagram: <img id="foo" src="assets/diagram.png" alt="diagram" class="bar" />',
'README',
'docs/home.md',
'Here\'s a diagram: <img id="foo" src="../assets/diagram.png" alt="diagram" class="bar">',
'Here\'s a diagram: <img id="foo" src="../assets/diagram.png" alt="diagram" class="bar" />',
id='html-image',
),
pytest.param(
'Here\'s a diagram: <source id="foo" src="assets/diagram.png" class="bar">',
'Here\'s a diagram: <source id="foo" src="assets/diagram.png" class="bar" />',
'README',
'docs/home.md',
'Here\'s a diagram: <source id="foo" src="../assets/diagram.png" class="bar">',
'Here\'s a diagram: <source id="foo" src="../assets/diagram.png" class="bar" />',
id='html-source',
),
pytest.param(
Expand All @@ -99,6 +120,51 @@
'Here\'s a diagram: <a id="foo" href="../badge.png" class="bar">example</a>',
id='html-anchor',
),
# Adverarial tests: contains >, multiple tag in line.
pytest.param(
'<img id="foo" attr="3>2" src="assets/diagram.png" alt="diagram" class="bar" /><img id="foo" attr="3>2" src="assets/diagram.png" alt="diagram" class="bar" />',
'README',
'docs/home.md',
'<img id="foo" attr="3>2" 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',
),
pytest.param(
'<a id="foo" attr="3>2" href="badge.png" class="bar">foo</a><a id="foo" attr="3>2" href="badge.png" class="bar">bar</a>',
'README',
'docs/home.md',
'<a id="foo" attr="3>2" 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',
),
# Adversarial test: img no end slash
pytest.param(
'Here\'s a diagram: <img id="foo" src="assets/diagram.png" alt="diagram" class="bar">',
'README',
'docs/home.md',
'Here\'s a diagram: <img id="foo" src="../assets/diagram.png" alt="diagram" class="bar">',
id='html-image-no-end-slash',
),
# external link
pytest.param(
'<img id="foo" attr="3>2" src="https://example.com/image.png" class="bar" />',
'README',
'docs/home.md',
'<img id="foo" attr="3>2" src="https://example.com/image.png" class="bar" />',
id='html-image-external-link',
),
pytest.param(
'<a id="foo" attr="3>2" href="https://example.com/index.html" class="bar" />',
'README',
'docs/home.md',
'<a id="foo" attr="3>2" href="https://example.com/index.html" class="bar" />',
id='html-anchor-external-link',
),
pytest.param(
'<a id="foo" attr="3>2" href="https://example.com" class="bar" />',
'README',
'docs/home.md',
'<a id="foo" attr="3>2" href="https://example.com" class="bar" />',
id='html-anchor-external-top-level-link',
maekawataiki marked this conversation as resolved.
Show resolved Hide resolved
),
pytest.param(
'''[Homepage](/) [Github](https://github.com/user/repo)
[Privacy policy](/privacy)''',
Expand Down
Loading