Skip to content

Commit

Permalink
feat: Allow [identifier][], understood as [identifier][identifier]
Browse files Browse the repository at this point in the history
PR #5: #5
  • Loading branch information
oprypin committed Apr 23, 2021
1 parent deca0ec commit 2d3182d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
12 changes: 11 additions & 1 deletion src/mkdocs_autorefs/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from markdown import Markdown
from markdown.extensions import Extension
from markdown.inlinepatterns import REFERENCE_RE, ReferenceInlineProcessor
from markdown.util import INLINE_PLACEHOLDER_RE

AUTO_REF_RE = re.compile(r'<span data-mkdocstrings-identifier=("?)(?P<identifier>[^"<>]*)\1>(?P<title>.*?)</span>')
"""A regular expression to match mkdocs-autorefs' special reference markers
Expand Down Expand Up @@ -69,7 +70,16 @@ def evalId(self, data: str, index: int, text: str) -> EvalIDType: # noqa: N802
m = self.RE_LINK.match(data, pos=index)
if not m:
return None, index, False
identifier = m.group(1) or text

identifier = m.group(1)
if not identifier:
identifier = text
# Allow the entire content to be one placeholder, with the intent of catching things like [`Foo`][].
# It doesn't catch [*Foo*][] though, just due to the priority order.
# https://github.com/Python-Markdown/markdown/blob/1858c1b601ead62ed49646ae0d99298f41b1a271/markdown/inlinepatterns.py#L78
if INLINE_PLACEHOLDER_RE.fullmatch(identifier):
identifier = self.unescape(identifier)

end = m.end(0)
return identifier, end, True

Expand Down
36 changes: 27 additions & 9 deletions tests/test_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,16 @@ def test_reference_explicit_with_markdown_text():
"""Check explicit references with Markdown formatting."""
run_references_test(
url_map={"Foo": "foo.html#Foo"},
source="This [`Foo`][Foo].",
source="This [**Foo**][Foo].",
output='<p>This <a href="foo.html#Foo"><strong>Foo</strong></a>.</p>',
)


def test_reference_implicit_with_code():
"""Check implicit references (identifier only, wrapped in backticks)."""
run_references_test(
url_map={"Foo": "foo.html#Foo"},
source="This [`Foo`][].",
output='<p>This <a href="foo.html#Foo"><code>Foo</code></a>.</p>',
)

Expand Down Expand Up @@ -121,18 +130,27 @@ def test_missing_reference_with_markdown_text():
def test_missing_reference_with_markdown_id():
"""Check unmapped explicit references with Markdown in the identifier."""
run_references_test(
url_map={"NotFoo": "foo.html#NotFoo"},
source="[Foo][*oh*]",
output="<p>[Foo][*oh*]</p>",
unmapped=["*oh*"],
url_map={"Foo": "foo.html#Foo", "NotFoo": "foo.html#NotFoo"},
source="[Foo][*NotFoo*]",
output="<p>[Foo][*NotFoo*]</p>",
unmapped=["*NotFoo*"],
)


def test_missing_reference_with_markdown_implicit():
"""Check that implicit references are not fixed when the identifier is not the exact one."""
run_references_test(
url_map={"Foo": "foo.html#Foo"},
source="[`Foo`][]",
output="<p>[<code>Foo</code>][]</p>",
unmapped=[],
url_map={"Foo-bar": "foo.html#Foo-bar"},
source="[*Foo-bar*][] and [`Foo`-bar][]",
output="<p>[<em>Foo-bar</em>][*Foo-bar*] and [<code>Foo</code>-bar][]</p>",
unmapped=["*Foo-bar*"],
)


def test_ignore_reference_with_special_char():
"""Check that references are not considered if there is a space character inside."""
run_references_test(
url_map={"a b": "foo.html#Foo"},
source="This [*a b*][].",
output="<p>This [<em>a b</em>][].</p>",
)

0 comments on commit 2d3182d

Please sign in to comment.