Last active
November 26, 2024 06:15
-
-
Save hamelsmu/e7bc44e3f8318847a841e4e103bc1320 to your computer and use it in GitHub Desktop.
html to markdown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from html2text import HTML2Text | |
from textwrap import dedent | |
import re | |
def get_md(cts, extractor='h2t'): | |
h2t = HTML2Text(bodywidth=5000) | |
h2t.ignore_links,h2t.mark_code,h2t.ignore_images = (True,)*3 | |
res = h2t.handle(cts) | |
def _f(m): return f'```\n{dedent(m.group(1))}\n```' | |
return re.sub(r'\[code]\s*\n(.*?)\n\[/code]', _f, res or '', flags=re.DOTALL).strip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage