Skip to content

Commit 9ccd56b

Browse files
committed
fix: do not sanitize interlinks in table descriptions
1 parent a518462 commit 9ccd56b

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

quartodoc/autosummary.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ def get_object(
9999
>>> get_function("quartodoc", "get_function")
100100
<Function('get_function', ...
101101
102+
Returns
103+
-------
104+
x:
105+
abc
102106
"""
103107

104108
if object_name is not None:

quartodoc/renderers/base.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ def escape(val: str):
1010
return f"`{val}`"
1111

1212

13-
def sanitize(val: str):
14-
return (
15-
val.replace("\n", " ")
16-
.replace("|", "\\|")
17-
.replace("[", "\\[")
18-
.replace("]", "\\]")
19-
)
13+
def sanitize(val: str, allow_markdown=False):
14+
# sanitize common tokens that break tables
15+
res = val.replace("\n", " ").replace("|", "\\|")
16+
17+
# sanitize elements that can get interpreted as markdown links
18+
# or citations
19+
if not allow_markdown:
20+
return res.replace("[", "\\[").replace("]", "\\]")
21+
22+
return res
2023

2124

2225
def convert_rst_link_to_md(rst):

quartodoc/renderers/md_renderer.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ def render(self, el: ds.DocstringParameter) -> Tuple[str]:
419419
default = "required" if el.default is None else escape(el.default)
420420

421421
annotation = self.render_annotation(el.annotation)
422-
return (escape(el.name), annotation, sanitize(el.description), default)
422+
clean_desc = sanitize(el.description, allow_markdown=True)
423+
return (escape(el.name), annotation, clean_desc, default)
423424

424425
# attributes ----
425426

@@ -436,7 +437,7 @@ def render(self, el: ds.DocstringAttribute):
436437
row = [
437438
sanitize(el.name),
438439
self.render_annotation(annotation),
439-
sanitize(el.description or "")
440+
sanitize(el.description or "", allow_markdown=True)
440441
]
441442
return row
442443

@@ -490,7 +491,7 @@ def render(self, el: Union[ds.DocstringSectionReturns, ds.DocstringSectionRaises
490491
def render(self, el: Union[ds.DocstringReturn, ds.DocstringRaise]):
491492
# similar to DocstringParameter, but no name or default
492493
annotation = self.render_annotation(el.annotation)
493-
return (annotation, el.description)
494+
return (annotation, sanitize(el.description, allow_markdown=True))
494495

495496
# unsupported parts ----
496497

@@ -512,7 +513,7 @@ def render(self, el):
512513

513514
@staticmethod
514515
def _summary_row(link, description):
515-
return f"| {link} | {sanitize(description)} |"
516+
return f"| {link} | {sanitize(description, allow_markdown=True)} |"
516517

517518
@dispatch
518519
def summarize(self, el):

quartodoc/tests/test_renderers.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import pytest
2+
import griffe.docstrings.dataclasses as ds
23

34
from quartodoc.renderers import MdRenderer
45
from quartodoc import get_object
56

6-
# TODO: tests in test_basic.py also use the renderer, so we should move them here.
7-
87

98
@pytest.fixture
109
def renderer():
@@ -44,3 +43,20 @@ def test_render_param_kwonly(src, dst, renderer):
4443

4544
res = renderer.render(f.parameters)
4645
assert res == dst
46+
47+
48+
@pytest.mark.parametrize(
49+
"pair",
50+
[
51+
[ds.DocstringSectionParameters, ds.DocstringParameter],
52+
[ds.DocstringSectionAttributes, ds.DocstringAttribute],
53+
[ds.DocstringSectionReturns, ds.DocstringReturn],
54+
],
55+
)
56+
def test_render_table_description_interlink(renderer, pair):
57+
interlink = "[](`abc`)"
58+
cls_section, cls_par = pair
59+
pars = cls_section([cls_par(name="x", description=interlink)])
60+
61+
res = renderer.render(pars)
62+
assert interlink in res

0 commit comments

Comments
 (0)