Skip to content

Allow html tags that contain only spaces #155

Open
@tthkbw

Description

@tthkbw

markdownify converts this:

one<i> </i>two

into:

oneone

because the function chomp returns the text inside the tags as an empty string.

One can argue that empty tags like this should not be allowed, but the epub The Last Dangerous Visions from Blackstone Publishing has this structure in dozens of locations and the space inside the tag is the space between two words of the text. This results in my epub reader showing a bunch of words without spaces between them when I use markdownify to convert the text of the epub files into markdown.

I don't pretend to understand how the markdownify code works in detail, but modifying chomp to look like this fixes the issue for me:

def chomp(text):
    """
    If the text in an inline tag like b, a, or em contains a leading or trailing
    space, strip the string and return a space as suffix of prefix, if needed.
    This function is used to prevent conversions like
        <b> foo</b> => ** foo**
    """
    #change to allow empty tags: "<i> </i>" and maintain the space
    if text.isspace():
        prefix = ''
        suffix = ''
        text = ' '
    else:
        prefix = ' ' if text and text[0] == ' ' else ''
        suffix = ' ' if text and text[-1] == ' ' else ''
        text = text.strip()
    return (prefix, suffix, text)

An unfortunate side effect of my fix is that this turns into something similar to &nbsp.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions