Skip to content

Commit

Permalink
fix: Don't add codehilite CSS class to inline code
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Aug 28, 2023
1 parent b6ddf37 commit 7690d41
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/mkdocstrings/handlers/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ def __init__(self, md: Markdown):
md: The Markdown instance to read configs from.
"""
config: dict[str, Any] = {}
self._highlighter: str | None = None
for ext in md.registeredExtensions:
if isinstance(ext, HighlightExtension) and (ext.enabled or not config):
self._highlighter = "highlight"
config = ext.getConfigs()
break # This one takes priority, no need to continue looking
if isinstance(ext, CodeHiliteExtension) and not config:
self._highlighter = "codehilite"
config = ext.getConfigs()
config["language_prefix"] = config["lang_prefix"]
self._css_class = config.pop("css_class", "highlight")
Expand Down Expand Up @@ -116,7 +119,11 @@ def highlight(
self.linenums = old_linenums

if inline:
return Markup(f'<code class="{kwargs["css_class"]} language-{language}">{result.text}</code>')
# From the maintainer of codehilite, the codehilite CSS class, as defined by the user,
# should never be added to inline code, because codehilite does not support inline code.
# See https://github.com/Python-Markdown/markdown/issues/1220#issuecomment-1692160297.
css_class = "" if self._highlighter == "codehilite" else kwargs["css_class"]
return Markup(f'<code class="{css_class} language-{language}">{result.text}</code>')
return Markup(result)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_highlighter_without_pygments(extension_name: str) -> None:
)
assert (
hl.highlight("import foo", language="python", inline=True)
== '<code class="hiiii language-python">import foo</code>'
== f'<code class="{"" if extension_name == "codehilite" else "hiiii"} language-python">import foo</code>'
)


Expand Down

0 comments on commit 7690d41

Please sign in to comment.