Overview
TheHtmlConverter class converts HTML and XHTML documents to Markdown. It uses BeautifulSoup for parsing and a custom Markdownify implementation for conversion. This converter is also used as a base class for other converters (DOCX, PPTX, EPUB) that produce HTML as an intermediate format.
Dependencies
Included in base install:beautifulsoup4, markdownify
Accepted Formats
list
text/htmlapplication/xhtml*
list
.html.htm
Class Definition
Methods
accepts()
True for HTML/XHTML files based on extension or MIME type.
convert()
BinaryIO
required
Binary stream of the HTML file
StreamInfo
required
Metadata about the file (must include charset if not UTF-8)
DocumentConverterResult with Markdown and extracted title
convert_string()
str
required
HTML content as string
str
Optional URL for the HTML content (used in StreamInfo)
DocumentConverterResult with Markdown
Features
HTML Cleaning
Before conversion, the HTML is cleaned:- Script Removal - All
<script>tags removed - Style Removal - All
<style>tags removed - Body Extraction - Only
<body>content processed (if present)
Converted Elements
- Headings -
<h1>-<h6>→#headings - Paragraphs -
<p>→ Paragraphs with blank lines - Lists -
<ul>,<ol>→ Markdown lists - Links -
<a>→[text](url) - Images -
<img>→ - Tables -
<table>→ Markdown tables - Code -
<code>,<pre>→ Inline/block code - Emphasis -
<em>,<strong>→*italic*,**bold** - Blockquotes -
<blockquote>→>quotes - Horizontal Rules -
<hr>→---
Title Extraction
Extracted from<title> tag and returned in DocumentConverterResult.title.
Character Encoding
Respectsstream_info.charset, defaults to UTF-8:
Example Usage
From File
From String
With Tables
Usage as Base Class
Many converters extendHtmlConverter to leverage HTML-to-Markdown conversion:
DocxConverter- Word documents via mammothPptxConverter- Tables from PowerPointXlsxConverter/XlsConverter- Excel tables via pandas HTMLEpubConverter- EPUB content files
Implementation Details
Source Location
~/workspace/source/packages/markitdown/src/markitdown/converters/_html_converter.py:20
Conversion Pipeline
-
Parse HTML
-
Clean Content
-
Extract Body
-
Extract Title
Custom Markdownify
Uses_CustomMarkdownify class (from _markdownify.py) which extends the markdownify library with custom conversion rules for better Markdown output quality.
Advanced Options
The converter accepts options passed to_CustomMarkdownify:
Use Cases
Web Scraping
Documentation Conversion
Email Conversion
Limitations
- Complex CSS layouts not preserved
- JavaScript-rendered content not processed
- Forms and interactive elements lost
- Nested tables may have formatting issues
- Some HTML5 semantic elements treated as divs
- Embedded media (video, audio) becomes links only