Skip to content

Commit

Permalink
Merge pull request #14 from sdruskat/main
Browse files Browse the repository at this point in the history
Fix #11: Adapt link regex to exclude anchor links
  • Loading branch information
fire1ce committed Feb 26, 2024
2 parents 6040779 + a8f5bea commit 340bdaa
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ Install the package with pip:
pip install mkdocs-embed-external-markdown
```

### Installation for development

To run the tests, first install the package and its test dependencies with pip:

```shell
pip install .[test]
```

You can now run the tests in `tests/` with `pytest`:

```shell
python -m pytest tests/
```

## Configuration

Enable the plugin in your `mkdocs.yml` file:
Expand Down
4 changes: 3 additions & 1 deletion external_markdown/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ def update_relative_links(self, markdown: str, base_url: str) -> str:
"""

def replace_link(match):
link_url = urljoin(base_url, match.group("link_url"))
link_url = str(match.group("link_url"))
if not link_url.startswith("#"):
link_url = urljoin(base_url, link_url)
return f'[{match.group("alt_text")}]({link_url})'

return LINK_PATTERN_REGEX.sub(replace_link, markdown)
Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "mkdocs-embed-external-markdown"
version = "3.0.1"
version = "3.0.2"
description = "Mkdocs plugin that allow to inject external markdown or markdown section from given url"
readme = {file = "README.md", content-type = "text/markdown"}
authors = [{name = "Stas Yakobov", email = "dev@3os.org"}]
Expand Down Expand Up @@ -32,6 +32,10 @@ dependencies = [
Source = 'https://github.com/fire1ce/pypi-test'

[project.optional-dependencies]
test = [
"pytest>=7.4.3",
"mkdocs>=1.5.3",
]

[project.scripts]

Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Jinja2==3.1.2
requests==2.31.0
pytest[test]==7.4.3
mkdocs[test]==1.5.3
39 changes: 39 additions & 0 deletions tests/external-markdown/test_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pytest

from external_markdown.plugin import EmbedExternalMarkdown

BASE_URL = "https://BASEURL/"
BASE_FILE = "https://BASEURL/FILE.md"
TEST_DATA = [
(
"external link",
"https://github.com",
"https://github.com"),
(
"external link with anchor",
"https://ansible-docs.readthedocs.io/zh/stable-2.0/rst/playbooks_variables.html#using-variables-about-jinja2",
"https://ansible-docs.readthedocs.io/zh/stable-2.0/rst/playbooks_variables.html#using-variables-about-jinja2",
),
(
"anchor",
"#links",
"#links"),
(
"local link",
"page.md",
f"{BASE_URL}page.md"),
(
"local link with anchor",
"page.md#test-subsection",
f"{BASE_URL}page.md#test-subsection",
),
]


class TestEmbedExternalMarkdown:
@pytest.mark.parametrize("label,link_url,expected_url", TEST_DATA)
def test_update_relative_links_external(self, label, link_url, expected_url):
# Regression tests for #11
link = f"[{label}]({link_url})"
expected = f"[{label}]({expected_url})"
assert EmbedExternalMarkdown().update_relative_links(link, BASE_FILE) == expected

0 comments on commit 340bdaa

Please sign in to comment.