Skip to content

Commit c114288

Browse files
committed
fix: sanitize css class names in headers; use doctest
1 parent 75c8477 commit c114288

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ include = ["quartodoc"]
99

1010
[tool.pytest.ini_options]
1111
markers = []
12-
testpaths = ["quartodoc/tests"]
12+
testpaths = ["quartodoc"]
1313

1414
[project]
1515
name = "quartodoc"

quartodoc/ast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,10 @@ def preview(
375375
>>> obj = get_object("quartodoc", "get_object")
376376
377377
>>> preview(obj.docstring.parsed)
378-
...
378+
...
379379
380380
>>> preview(obj)
381-
...
381+
...
382382
383383
"""
384384

quartodoc/renderers/md_renderer.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import black
4+
import re
45
import quartodoc.ast as qast
56

67
from contextlib import contextmanager
@@ -26,6 +27,28 @@ def _has_attr_section(el: dc.Docstring | None):
2627
return any([isinstance(x, ds.DocstringSectionAttributes) for x in el.parsed])
2728

2829

30+
def _sanitize_title(title: str):
31+
"""Replace characters so that title can be used as an anchor, by
32+
33+
Approach:
34+
* replace spaces with -, then
35+
* stripping non alphanumeric characters
36+
37+
Examples
38+
--------
39+
40+
>>> _sanitize_title("a b c")
41+
'a-b-c'
42+
>>> _sanitize_title("a b! c")
43+
'a-b-c'
44+
>>> _sanitize_title("a`b`c{")
45+
'abc'
46+
47+
"""
48+
49+
return re.sub(r"[^a-zA-Z0-9-]+", "", title.replace(" ", "-"))
50+
51+
2952
@dataclass
3053
class ParamRow:
3154
name: str | None
@@ -269,7 +292,7 @@ def render_header(self, el: layout.Doc) -> str:
269292

270293
@dispatch
271294
def render_header(self, el: ds.DocstringSection) -> str:
272-
title = el.title or el.kind.value
295+
title = _sanitize_title(el.title or el.kind.value)
273296
_classes = [".doc-section", ".doc-section-" + title.replace(" ", "-")]
274297
_str_classes = " ".join(_classes)
275298
return f"{'#' * self.crnt_header_level} {title.title()} {{{_str_classes}}}"

0 commit comments

Comments
 (0)