From 8d4b5eeb35cb68fde13f56f142218a737877aee2 Mon Sep 17 00:00:00 2001 From: "Druskat, Stephan" Date: Fri, 24 Nov 2023 22:42:02 +0100 Subject: [PATCH 1/3] Fix #11: Adapt link regex to exclude anchor links - Changes the LINK_PATTERN_REGEX to only match links that don't start with a hash by adding '[^#]' in front of the link_url group. --- external_markdown/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/external_markdown/plugin.py b/external_markdown/plugin.py index 4f068ba..d8ce18e 100644 --- a/external_markdown/plugin.py +++ b/external_markdown/plugin.py @@ -9,7 +9,7 @@ # Pre-compile regular expressions SECTION_LEVEL_REGEX = re.compile("^#+ ", re.IGNORECASE) -LINK_PATTERN_REGEX = re.compile(r"\[(?P[^\]]*)\]\((?P[^\)]*)\)", re.MULTILINE | re.IGNORECASE) +LINK_PATTERN_REGEX = re.compile(r"\[(?P[^\]]*)\]\([^#](?P[^\)]*)\)", re.MULTILINE | re.IGNORECASE) logger = logging.getLogger("mkdocs.plugins") From 20d538f88f94328e9c9a8178dbd793109e579b5f Mon Sep 17 00:00:00 2001 From: "Druskat, Stephan" Date: Fri, 24 Nov 2023 22:47:56 +0100 Subject: [PATCH 2/3] Bump PATCH version part to reflect bugfix --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 26a5e05..75dbcaf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"}] From a8f5bea542aef86302ab3fc08d826006e5a49ddd Mon Sep 17 00:00:00 2001 From: Stephan Druskat Date: Mon, 27 Nov 2023 10:14:21 +0100 Subject: [PATCH 3/3] Fix #11 properly and add tests * Add test dependencies and document how to install and run tests * Add regression tests for different types of links * Fix #11 properly: Test for `#` prefix in links * Rolls back the broken fix in 8d4b5ee *Fixes #11 by checking for a hash prefix in closure code --- README.md | 14 +++++++++ external_markdown/plugin.py | 6 ++-- pyproject.toml | 4 +++ requirements.txt | 2 ++ tests/external-markdown/test_plugin.py | 39 ++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 tests/external-markdown/test_plugin.py diff --git a/README.md b/README.md index 198f82c..3619e10 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/external_markdown/plugin.py b/external_markdown/plugin.py index d8ce18e..1271118 100644 --- a/external_markdown/plugin.py +++ b/external_markdown/plugin.py @@ -9,7 +9,7 @@ # Pre-compile regular expressions SECTION_LEVEL_REGEX = re.compile("^#+ ", re.IGNORECASE) -LINK_PATTERN_REGEX = re.compile(r"\[(?P[^\]]*)\]\([^#](?P[^\)]*)\)", re.MULTILINE | re.IGNORECASE) +LINK_PATTERN_REGEX = re.compile(r"\[(?P[^\]]*)\]\((?P[^\)]*)\)", re.MULTILINE | re.IGNORECASE) logger = logging.getLogger("mkdocs.plugins") @@ -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) diff --git a/pyproject.toml b/pyproject.toml index 75dbcaf..3564f69 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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] diff --git a/requirements.txt b/requirements.txt index 6380a66..e576d0d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ Jinja2==3.1.2 requests==2.31.0 +pytest[test]==7.4.3 +mkdocs[test]==1.5.3 diff --git a/tests/external-markdown/test_plugin.py b/tests/external-markdown/test_plugin.py new file mode 100644 index 0000000..9c41bf8 --- /dev/null +++ b/tests/external-markdown/test_plugin.py @@ -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