Convert an HTML document into markdown. Args: html: Raw HTML source as a string. Pass already-decoded text — the fetch layer hands back ``bytes`` so callers should ``decode()`` first using whatever charset is appropriate. strip_boilerplate: When ``True``
(
html: str,
*,
strip_boilerplate: bool = True,
)
| 23 | |
| 24 | |
| 25 | async def html_to_markdown( |
| 26 | html: str, |
| 27 | *, |
| 28 | strip_boilerplate: bool = True, |
| 29 | ) -> str: |
| 30 | """Convert an HTML document into markdown. |
| 31 | |
| 32 | Args: |
| 33 | html: Raw HTML source as a string. Pass already-decoded text — the |
| 34 | fetch layer hands back ``bytes`` so callers should ``decode()`` |
| 35 | first using whatever charset is appropriate. |
| 36 | strip_boilerplate: When ``True`` (default), trafilatura aggressively |
| 37 | removes nav/footer/aside content (``favor_recall=False``). Set |
| 38 | to ``False`` to keep more peripheral content at the cost of |
| 39 | noise. |
| 40 | |
| 41 | Returns: |
| 42 | Markdown string. Returns an empty string if trafilatura yielded |
| 43 | nothing extractable (e.g. the page is a redirect or login wall). |
| 44 | """ |
| 45 | if not html.strip(): |
| 46 | return "" |
| 47 | return await asyncio.to_thread( |
| 48 | _extract_sync, html, strip_boilerplate=strip_boilerplate |
| 49 | ) |
no outgoing calls