Skip to content

Commit cb32f17

Browse files
psobotamyreese
andauthored
Render codespans within link text. (#68)
* Ignore E501 lints * Properly render codespans within link text. --------- Co-authored-by: Amethyst Reese <[email protected]>
1 parent a1949eb commit cb32f17

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

.flake8

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ ignore =
88
E2
99
E3
1010
E4
11+
E501
1112
W503
1213
max-line-length = 88
1314
per-file-ignores =

sphinx_mdinclude/render.py

+11
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,17 @@ def link(self, text: str, url: str, title: Optional[str] = None) -> str:
241241
text = re.sub(r":target: (.*)\n", f":target: {url}\n", text)
242242
return text
243243

244+
if text.startswith("``") and text.endswith("``"):
245+
# Return raw HTML for inline code:
246+
html = (
247+
'<code class="docutils literal">'
248+
'<span class="pre">{}</span>'
249+
"</code>".format(text[2:-2].replace("`", "&#96;"))
250+
)
251+
return self._raw_html(
252+
'<a href="{url}">{text}</a>'.format(url=url, text=html)
253+
)
254+
244255
underscore = "_"
245256
if title:
246257
return self._raw_html(

sphinx_mdinclude/tests/test_renderer.py

+11
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ def test_inline_code_with_opening_and_closing_space_and_backtick(self) -> None:
147147
'<span class="pre">&#96;a&#96;</span></code>`',
148148
)
149149

150+
def test_inline_code_within_link(self) -> None:
151+
src = "[`foobar`](https://example.com)"
152+
out = self.conv(src)
153+
self.assertEqual(
154+
out.strip(),
155+
".. role:: raw-html-md(raw)\n"
156+
" :format: html\n\n\n"
157+
':raw-html-md:`<a href="https://example.com"><code class="docutils literal">'
158+
'<span class="pre">foobar</span></code></a>`',
159+
)
160+
150161
def test_strikethrough(self) -> None:
151162
src = "~~a~~"
152163
self.conv(src)

0 commit comments

Comments
 (0)