Skip to content

Commit 2d3182d

Browse files
authored
feat: Allow [identifier][], understood as [identifier][identifier]
PR #5: #5
1 parent deca0ec commit 2d3182d

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

src/mkdocs_autorefs/references.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from markdown import Markdown
99
from markdown.extensions import Extension
1010
from markdown.inlinepatterns import REFERENCE_RE, ReferenceInlineProcessor
11+
from markdown.util import INLINE_PLACEHOLDER_RE
1112

1213
AUTO_REF_RE = re.compile(r'<span data-mkdocstrings-identifier=("?)(?P<identifier>[^"<>]*)\1>(?P<title>.*?)</span>')
1314
"""A regular expression to match mkdocs-autorefs' special reference markers
@@ -69,7 +70,16 @@ def evalId(self, data: str, index: int, text: str) -> EvalIDType: # noqa: N802
6970
m = self.RE_LINK.match(data, pos=index)
7071
if not m:
7172
return None, index, False
72-
identifier = m.group(1) or text
73+
74+
identifier = m.group(1)
75+
if not identifier:
76+
identifier = text
77+
# Allow the entire content to be one placeholder, with the intent of catching things like [`Foo`][].
78+
# It doesn't catch [*Foo*][] though, just due to the priority order.
79+
# https://github.com/Python-Markdown/markdown/blob/1858c1b601ead62ed49646ae0d99298f41b1a271/markdown/inlinepatterns.py#L78
80+
if INLINE_PLACEHOLDER_RE.fullmatch(identifier):
81+
identifier = self.unescape(identifier)
82+
7383
end = m.end(0)
7484
return identifier, end, True
7585

tests/test_references.py

+27-9
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,16 @@ def test_reference_explicit_with_markdown_text():
6666
"""Check explicit references with Markdown formatting."""
6767
run_references_test(
6868
url_map={"Foo": "foo.html#Foo"},
69-
source="This [`Foo`][Foo].",
69+
source="This [**Foo**][Foo].",
70+
output='<p>This <a href="foo.html#Foo"><strong>Foo</strong></a>.</p>',
71+
)
72+
73+
74+
def test_reference_implicit_with_code():
75+
"""Check implicit references (identifier only, wrapped in backticks)."""
76+
run_references_test(
77+
url_map={"Foo": "foo.html#Foo"},
78+
source="This [`Foo`][].",
7079
output='<p>This <a href="foo.html#Foo"><code>Foo</code></a>.</p>',
7180
)
7281

@@ -121,18 +130,27 @@ def test_missing_reference_with_markdown_text():
121130
def test_missing_reference_with_markdown_id():
122131
"""Check unmapped explicit references with Markdown in the identifier."""
123132
run_references_test(
124-
url_map={"NotFoo": "foo.html#NotFoo"},
125-
source="[Foo][*oh*]",
126-
output="<p>[Foo][*oh*]</p>",
127-
unmapped=["*oh*"],
133+
url_map={"Foo": "foo.html#Foo", "NotFoo": "foo.html#NotFoo"},
134+
source="[Foo][*NotFoo*]",
135+
output="<p>[Foo][*NotFoo*]</p>",
136+
unmapped=["*NotFoo*"],
128137
)
129138

130139

131140
def test_missing_reference_with_markdown_implicit():
132141
"""Check that implicit references are not fixed when the identifier is not the exact one."""
133142
run_references_test(
134-
url_map={"Foo": "foo.html#Foo"},
135-
source="[`Foo`][]",
136-
output="<p>[<code>Foo</code>][]</p>",
137-
unmapped=[],
143+
url_map={"Foo-bar": "foo.html#Foo-bar"},
144+
source="[*Foo-bar*][] and [`Foo`-bar][]",
145+
output="<p>[<em>Foo-bar</em>][*Foo-bar*] and [<code>Foo</code>-bar][]</p>",
146+
unmapped=["*Foo-bar*"],
147+
)
148+
149+
150+
def test_ignore_reference_with_special_char():
151+
"""Check that references are not considered if there is a space character inside."""
152+
run_references_test(
153+
url_map={"a b": "foo.html#Foo"},
154+
source="This [*a b*][].",
155+
output="<p>This [<em>a b</em>][].</p>",
138156
)

0 commit comments

Comments
 (0)