Skip to content

Commit

Permalink
fix: Don't ignore identifiers containing spaces and slashes
Browse files Browse the repository at this point in the history
Issue-55: #55
  • Loading branch information
pawamoy committed Sep 1, 2024
1 parent 4f2be46 commit b36a0d1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
9 changes: 4 additions & 5 deletions src/mkdocs_autorefs/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 12 additions & 7 deletions tests/test_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="<p>This [Foo bar][].</p>",
output='<p>This <a class="autorefs autorefs-internal" href="foo.html#bar">Foo bar</a>.</p>',
)


Expand Down Expand Up @@ -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="<p>This [<em>a b</em>][].</p>",
output='<p>This <a class="autorefs autorefs-internal" href="foo.html#Foo"><em>a b</em></a>.</p>',
)
run_references_test(
url_map={"*a/b*": "foo.html#Foo"},
source="This [`*a/b*`][].",
output='<p>This <a class="autorefs autorefs-internal" href="foo.html#Foo"><code>*a/b*</code></a>.</p>',
)


Expand Down

0 comments on commit b36a0d1

Please sign in to comment.