eshack94 / md-to-jira

Simple Python utilities for converting from Github-flavored Markdown syntax to Atlassian markup syntax, and vice-versa. Useful for code maintainers and doc writers who use GitHub, JIRA, and Confluence.
MIT License
10 stars 1 forks source link

Bold gets turned into italic #2

Open markszabo opened 1 month ago

markszabo commented 1 month ago

I found a bug in md_to_jira.py resulting in bold text always turning into italic:

# Convert bold and italic
line = re.sub(r'\*\*(.+?)\*\*', r'*\1*', line)
line = re.sub(r'__(.+?)__', r'*\1*', line)
line = re.sub(r'\*(.+?)\*', r'_\1_', line)
line = re.sub(r'_(.+?)_', r'_\1_', line)

The first two lines take text marked as bold in markdown (**bold** and __bold__) and turn them into *bold* (the way jira defines bold). However the 3rd line then takes this *bold* and turns it into _bold_, which results in the text showing up as italic in Jira.

Potential solution

Swapping the bold and italic replacement calls is not a solution, as starting with replacing *text* will result in **text** being turned into *_text_*.

One potential solution is to do a 2-step replace like this:

# Convert bold
# Jira's bold (*...*) is the same as md's italic, so we have to replace in two steps
bold_placeholder = "bold78c10ae05c702eb1cbold" # random text unlikely to be found in a real content
line = re.sub(r'\*\*(.+?)\*\*', r'{placeholder}\1{placeholder}'.format(placeholder=bold_placeholder), line)
line = re.sub(r'__(.+?)__', r'{placeholder}\1{placeholder}'.format(placeholder=bold_placeholder), line)
# Convert italic
line = re.sub(r'\*(.+?)\*', r'_\1_', line)
line = re.sub(r'_(.+?)_', r'_\1_', line)
# Convert placeholder back to bold
line = re.sub(r'{placeholder}(.+?){placeholder}'.format(placeholder=bold_placeholder), r'*\1*'.format(placeholder=bold_placeholder), line)

More generally: this project needs some unit tests to cover all the potential use-cases (e.g. check that **bold** turns into *bold* and that *italic* turns into _italic_)

eshack94 commented 1 month ago

@markszabo Thanks for the report! Fixing this is on my TODO list. Also on my TODO list is adding comprehensive unit tests to the project. I'll try to make some time to work on this soon!