Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support [identifier][] with pymdownx.inlinehilite enabled #40

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ classifiers = [
]
dependencies = [
"Markdown>=3.3",
"markupsafe>=2.0.1",
"mkdocs>=1.1",
]

Expand Down Expand Up @@ -90,6 +91,7 @@ tests = [
"pytest-cov>=4.1",
"pytest-randomly>=3.15",
"pytest-xdist>=3.3",
"pymdown-extensions>=10.0",
]
typing = [
"mypy>=1.5",
Expand Down
5 changes: 4 additions & 1 deletion src/mkdocs_autorefs/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
from urllib.parse import urlsplit
from xml.etree.ElementTree import Element

import markupsafe
from markdown.extensions import Extension
from markdown.inlinepatterns import REFERENCE_RE, ReferenceInlineProcessor
from markdown.util import INLINE_PLACEHOLDER_RE
from markdown.util import HTML_PLACEHOLDER_RE, INLINE_PLACEHOLDER_RE

if TYPE_CHECKING:
from markdown import Markdown
Expand Down Expand Up @@ -88,6 +89,8 @@ def evalId(self, data: str, index: int, text: str) -> EvalIDType: # noqa: N802
# https://github.com/Python-Markdown/markdown/blob/1858c1b601ead62ed49646ae0d99298f41b1a271/markdown/inlinepatterns.py#L78
if INLINE_PLACEHOLDER_RE.fullmatch(identifier):
identifier = self.unescape(identifier)
if match := HTML_PLACEHOLDER_RE.fullmatch(identifier):
identifier = markupsafe.Markup(self.md.htmlStash.rawHtmlBlocks[int(match.group(1))]).striptags()

end = m.end(0)
return identifier, end, True
Expand Down
25 changes: 24 additions & 1 deletion tests/test_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import annotations

from typing import Mapping

import markdown
import pytest

Expand Down Expand Up @@ -44,6 +46,7 @@ def run_references_test(
output: str,
unmapped: list[str] | None = None,
from_url: str = "page.html",
extensions: Mapping = {},
) -> None:
"""Help running tests about references.

Expand All @@ -54,7 +57,7 @@ def run_references_test(
unmapped: The expected unmapped list.
from_url: The source page URL.
"""
md = markdown.Markdown(extensions=[AutorefsExtension()])
md = markdown.Markdown(extensions=[AutorefsExtension(), *extensions], extension_configs=extensions)
content = md.convert(source)

def url_mapper(identifier: str) -> str:
Expand Down Expand Up @@ -92,6 +95,26 @@ def test_reference_implicit_with_code() -> None:
)


def test_reference_implicit_with_code_inlinehilite_plain() -> None:
"""Check implicit references (identifier in backticks, wrapped by inlinehilite)."""
run_references_test(
extensions={"pymdownx.inlinehilite": {}},
url_map={"pathlib.Path": "pathlib.html#Path"},
source="This [`pathlib.Path`][].",
output='<p>This <a class="autorefs autorefs-internal" href="pathlib.html#Path"><code>pathlib.Path</code></a>.</p>',
)


def test_reference_implicit_with_code_inlinehilite_python() -> None:
"""Check implicit references (identifier in backticks, syntax-highlighted by inlinehilite)."""
run_references_test(
extensions={"pymdownx.inlinehilite": {"style_plain_text": "python"}},
url_map={"pathlib.Path": "pathlib.html#Path"},
source="This [`pathlib.Path`][].",
output='<p>This <a class="autorefs autorefs-internal" href="pathlib.html#Path"><code class="highlight"><span class="n">pathlib</a><span class="o">.</span><span class="n">Path</span></code></span>.</p>',
pawamoy marked this conversation as resolved.
Show resolved Hide resolved
)


def test_reference_with_punctuation() -> None:
"""Check references with punctuation."""
run_references_test(
Expand Down