]*>|\s*$", "", raw, flags=re.I | re.S)
+ raw = re.sub(r"<[^>]+>", "", raw)
+ raw = html.unescape(raw)
+ raw = raw.strip("\n")
+ code_blocks.append(raw)
+ return f"\n@@CODEBLOCK_{len(code_blocks)-1}@@\n"
+
+ text = re.sub(r"]*>(.*?)", _extract_code, text, flags=re.I | re.S) + + # links + text = re.sub( + r"]*href=(?:\"([^\"]+)\"|'([^']+)'|([^\s>]+))[^>]*>(.*?)", + lambda m: f"[{strip_tags(m.group(4) or '')}]({(m.group(1) or m.group(2) or m.group(3) or '').strip()})", + text, + flags=re.I | re.S, + ) + + # headings + for n in range(6, 0, -1): + text = re.sub( + rf"
]*>(.*?)", lambda m: "\n> " + strip_tags(m.group(1) or "") + "\n", text, flags=re.I | re.S) + text = re.sub(r"