From b36a0d1c4b0f5a6441ee6a2de7409942a8702bd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Sun, 1 Sep 2024 15:19:32 +0200 Subject: [PATCH] fix: Don't ignore identifiers containing spaces and slashes Issue-55: https://github.com/mkdocstrings/autorefs/issues/55 --- src/mkdocs_autorefs/references.py | 9 ++++----- tests/test_references.py | 19 ++++++++++++------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/mkdocs_autorefs/references.py b/src/mkdocs_autorefs/references.py index eac458e..7c361d1 100644 --- a/src/mkdocs_autorefs/references.py +++ b/src/mkdocs_autorefs/references.py @@ -88,11 +88,10 @@ def handleMatch(self, m: Match[str], data: str) -> tuple[Element | None, int | N if not handled or identifier is None: return None, None, None - if re.search(r"[/ \x00-\x1f]", identifier): - # Do nothing if the matched reference contains: - # - a space, slash or control character (considered unintended); - # - specifically \x01 is used by Python-Markdown HTML stash when there's inline formatting, - # but references with Markdown formatting are not possible anyway. + if re.search(r"[\x00-\x1f]", identifier): + # Do nothing if the matched reference contains control characters (from 0 to 31 included). + # Specifically `\x01` is used by Python-Markdown HTML stash when there's inline formatting, + # but references with Markdown formatting are not possible anyway. return None, m.start(0), end return self._make_tag(identifier, text), m.start(0), end diff --git a/tests/test_references.py b/tests/test_references.py index 18fdc6e..3eab1f0 100644 --- a/tests/test_references.py +++ b/tests/test_references.py @@ -146,11 +146,11 @@ def test_multiline_links() -> None: def test_no_reference_with_space() -> None: - """Check that references with spaces are not fixed.""" + """Check that references with spaces are fixed.""" run_references_test( - url_map={"Foo bar": "foo.html#Foo bar"}, + url_map={"Foo bar": "foo.html#bar"}, source="This [Foo bar][].", - output="

This [Foo bar][].

", + output='

This Foo bar.

', ) @@ -203,12 +203,17 @@ def test_missing_reference_with_markdown_implicit() -> None: ) -def test_ignore_reference_with_special_char() -> None: - """Check that references are not considered if there is a space character inside.""" +def test_reference_with_markup() -> None: + """Check that references with markup are resolved (and need escaping to prevent rendering).""" run_references_test( - url_map={"a b": "foo.html#Foo"}, + url_map={"*a b*": "foo.html#Foo"}, source="This [*a b*][].", - output="

This [a b][].

", + output='

This a b.

', + ) + run_references_test( + url_map={"*a/b*": "foo.html#Foo"}, + source="This [`*a/b*`][].", + output='

This *a/b*.

', )