How to sanitize user input for markdown?

Oh, I see! Thanks for clarifying.

At the moment there isn’t a good way to display clickable links without pushing text through the Markdown processor, so I see your problem.

I think your best solution then is to do some data cleaning on each title to get Markdown special-character escapes done. Something like:

cleaned_titles = []
MD_SPECIAL_CHARS = ["\`*_{}[]#+-."]
for title in titles:
    for char in MD_SPECIAL_CHARS:
        if char in MD_SPECIAL_CHARS:
            title = title.replace(char, "\"+char)
    cleaned_titles.append(title)

(If you need to make this loop more efficient, you might use re.sub from the regular expression libary instead.)

Does this seem like it addresses the problem? Let us know!

(edit: fixed typo in my code snippet)

1 Like