1
1
from __future__ import annotations
2
2
3
3
import black
4
+ import re
4
5
import quartodoc .ast as qast
5
6
6
7
from contextlib import contextmanager
@@ -26,6 +27,28 @@ def _has_attr_section(el: dc.Docstring | None):
26
27
return any ([isinstance (x , ds .DocstringSectionAttributes ) for x in el .parsed ])
27
28
28
29
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
+
29
52
@dataclass
30
53
class ParamRow :
31
54
name : str | None
@@ -269,10 +292,11 @@ def render_header(self, el: layout.Doc) -> str:
269
292
270
293
@dispatch
271
294
def render_header (self , el : ds .DocstringSection ) -> str :
272
- title = el .title or el .kind .value
273
- _classes = [".doc-section" , ".doc-section-" + title .replace (" " , "-" )]
295
+ title = el .title or el .kind .value .title ()
296
+ anchor_part = _sanitize_title (title .lower ())
297
+ _classes = [".doc-section" , f".doc-section-{ anchor_part } " ]
274
298
_str_classes = " " .join (_classes )
275
- return f"{ '#' * self .crnt_header_level } { title . title () } {{{ _str_classes } }}"
299
+ return f"{ '#' * self .crnt_header_level } { title } {{{ _str_classes } }}"
276
300
277
301
# render method -----------------------------------------------------------
278
302
0 commit comments