Skip to content

Commit

Permalink
Render codespans within link text. (#68)
Browse files Browse the repository at this point in the history
* Ignore E501 lints

* Properly render codespans within link text.

---------

Co-authored-by: Amethyst Reese <amethyst@n7.gg>
  • Loading branch information
psobot and amyreese committed May 16, 2024
1 parent a1949eb commit cb32f17
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ignore =
E2
E3
E4
E501
W503
max-line-length = 88
per-file-ignores =
Expand Down
11 changes: 11 additions & 0 deletions sphinx_mdinclude/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ def link(self, text: str, url: str, title: Optional[str] = None) -> str:
text = re.sub(r":target: (.*)\n", f":target: {url}\n", text)
return text

if text.startswith("``") and text.endswith("``"):
# Return raw HTML for inline code:
html = (
'<code class="docutils literal">'
'<span class="pre">{}</span>'
"</code>".format(text[2:-2].replace("`", "&#96;"))
)
return self._raw_html(
'<a href="{url}">{text}</a>'.format(url=url, text=html)
)

underscore = "_"
if title:
return self._raw_html(
Expand Down
11 changes: 11 additions & 0 deletions sphinx_mdinclude/tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ def test_inline_code_with_opening_and_closing_space_and_backtick(self) -> None:
'<span class="pre">&#96;a&#96;</span></code>`',
)

def test_inline_code_within_link(self) -> None:
src = "[`foobar`](https://example.com)"
out = self.conv(src)
self.assertEqual(
out.strip(),
".. role:: raw-html-md(raw)\n"
" :format: html\n\n\n"
':raw-html-md:`<a href="https://example.com"><code class="docutils literal">'
'<span class="pre">foobar</span></code></a>`',
)

def test_strikethrough(self) -> None:
src = "~~a~~"
self.conv(src)
Expand Down

0 comments on commit cb32f17

Please sign in to comment.