Search code examples
pythonmarkdownslug

Convert markdown title to anchor in Python


I'm trying to convert a markdown title to a "slug" (HTTP safe) name (What is a slug?).

Here's my attempt:

# this is a heuristic based on what we see in output files - not definitive
def markdown_title_name(t):
    # Strip leading and trailing spaces, use lowercase
    title = t.lower().strip()
    # change non-alphanumeric to dash -, unless we already have a dash
    for i in range(0, len(title)):
        if not title[i].isalnum():
            title = title[0:i] + '-' + title[i+1:]
    # replace any repeated dashes
    while '--' in title:
        title = title.replace('--', '-')
    # remove any leading & trailing dashes
    title = title.strip('-')
    return title

Example:

>>> markdown_title_name('The Quick! Brown Fox\n')
'the-quick-brown-fox'

Is there a better way (e.g. using a reliable published library) to do this? Note I don't want to render the whole text, I just want to know what the name will resolve to.

I'm concerned that Python's definition of non-alphanumeric might differ from Markdown's definition. The compression of repeated dashes and leading/trailing dashes is another area where more precision would be nice.


Solution

  • You should try python-slugify library for your purposes.

    The above code can be replaced by

    import slugify
    
    def markdown_title_name(t):
        return slugify.slugify(t)