def convert_markdown(text, convert_url=None, *, inline=False): convert_url = convert_url if convert_url else lambda x: x # Workaround for https://github.com/lepture/mistune/issues/125 NBSP_REPLACER = '\uf8ff' text = text.replace('\N{NO-BREAK SPACE}', NBSP_REPLACER) text = dedent(text) markdown = Markdown( escape=False, block=BlockLexer(), renderer=Renderer(convert_url), ) result = Markup(markdown(text)).strip() if inline and result.startswith('<p>') and result.endswith('</p>'): result = result[len('<p>'):-len('</p>')] # Workaround for https://github.com/lepture/mistune/issues/125 result = result.replace(NBSP_REPLACER, '\N{NO-BREAK SPACE}') return result