Skip to content

Commit ad54418

Browse files
authored
ALso sanitize dots and parenthesises for slug generation (#6904)
1 parent 05016ac commit ad54418

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

pages/extensions/markdown_toc_patch/custom_toc_tree_processor.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1+
import re
2+
import unicodedata
13
from markdown import util
24
from markdown.extensions.toc import TocExtension
35
from markdown.extensions.toc import TocTreeprocessor
46

7+
8+
def slugify(value: str, separator: str = '-', unicode: bool = False) -> str:
9+
""" Slugify a string, to make it URL friendly. """
10+
slug = value.strip().lower()
11+
slug = slug.replace(' ', '-')
12+
slug = re.sub(r'[^\w-]', '', slug, flags=re.UNICODE)
13+
return slug
14+
15+
516
class CustomTocTreeProcessor(TocTreeprocessor):
617
"""
718
Since html code that may be inside the headlines causes problems in the TOC we remove them
819
"""
920

21+
def __init__(self, md, config):
22+
super(CustomTocTreeProcessor, self).__init__(md, config)
23+
self.slugify = slugify
24+
1025
def strip_html_from_names(self, toc_list):
1126
for item in toc_list:
1227
text = item.get('name', '')

0 commit comments

Comments
 (0)