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

Merged
merged 7 commits into from
Feb 5, 2025
Merged
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
18 changes: 17 additions & 1 deletion src/mkdocs_include_markdown_plugin/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@
flags=re.VERBOSE | re.MULTILINE,
)

# Matched html image definition.
# e.g. <img src="path/to/image.png" alt="alt-text">
MARKDOWN_HTML_IMAGE_REGEX = re.compile(
maekawataiki marked this conversation as resolved.
Show resolved Hide resolved
r'''
<img
\s+
src="(\S+?)" # src = $1
maekawataiki marked this conversation as resolved.
Show resolved Hide resolved
''',
flags=re.VERBOSE | re.MULTILINE,
)


def transform_p_by_p_skipping_codeblocks( # noqa: PLR0912, PLR0915
markdown: str,
Expand Down Expand Up @@ -296,10 +307,15 @@ def transform(paragraph: str) -> str:
found_href_url_group_index_3,
paragraph,
)
return MARKDOWN_LINK_DEFINITION_REGEX.sub(
paragraph = MARKDOWN_LINK_DEFINITION_REGEX.sub(
functools.partial(found_href, url_group_index=2),
paragraph,
)
paragraph = MARKDOWN_HTML_IMAGE_REGEX.sub(
functools.partial(found_href, url_group_index=1),
paragraph,
)
return paragraph

return transform_p_by_p_skipping_codeblocks(
markdown,
Expand Down
7 changes: 7 additions & 0 deletions tests/test_unit/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@
'Build status: [![Build Status](../badge.png)](../build/)',
id='image-inside-link',
),
pytest.param(
'Here\'s a diagram: <img src="assets/diagram.png" alt="diagram">',
'README',
'docs/home.md',
'Here\'s a diagram: <img src="../assets/diagram.png" alt="diagram">',
id='html-image',
),
pytest.param(
'''[Homepage](/) [Github](https://github.com/user/repo)
[Privacy policy](/privacy)''',
Expand Down
Loading