import re def parse(markdown): res = '' in_list = False for line in markdown.splitlines(): match = re.match(r'^(#{1,6}) (.*)', line) if match: level = len(match.group(1)) res += f'{match.group(2)}' continue line = re.sub(r'__(.*)__', r'\1', line) line = re.sub(r'_(.*)_', r'\1', line) match = re.match(r'\* (.*)', line) if match: if not in_list: in_list = True res += '' res += '

' + line + '

' if in_list: res += '' return res