markitdown icon indicating copy to clipboard operation
markitdown copied to clipboard

HTML: <title> leaks into markdown body for documents without <body>; stray content outside <body> dropped

Open ManoharPaturi opened this issue 2 months ago • 1 comments

Description

HtmlConverter converts only the <body> element when present, and the whole document otherwise. Since BeautifulSoup's html.parser does not synthesize or relocate elements the way a browser does:

  • a valid HTML5 document without <body> tags leaks its <title> text into the markdown body
  • any content placed before/after <body> is silently dropped

Reproduction

from io import BytesIO
from markitdown import MarkItDown, StreamInfo

html = b'<html><head><title>Page Title</title></head><p>hello</p></html>'
MarkItDown().convert_stream(BytesIO(html), stream_info=StreamInfo(extension='.html')).markdown
# 'Page Title\n\nhello'   <-- title leaks into the body

html2 = b'before<body><p>in body</p></body>after'
# 'before' and 'after' are dropped entirely

Expected behavior

Head metadata stays out of the markdown (except when used as the doc title), and stray content outside <body> is preserved, matching what a browser considers rendered content.

Environment

markitdown main (cc0ca9e), Python 3.12

ManoharPaturi avatar Sep 13 '26 16:09 ManoharPaturi

Confirmed on markitdown==0.1.7 (PyPI), identical output to what you report from main:

from io import BytesIO
from markitdown import MarkItDown, StreamInfo

md = MarkItDown()
r = md.convert_stream(
    BytesIO(b'<html><head><title>Page Title</title></head><p>hello</p></html>'),
    stream_info=StreamInfo(extension='.html'),
)
r.markdown   # 'Page Title\n\nhello'
r.title      # 'Page Title'

Two details that may save whoever fixes it some time:

1. The head is converted into the body even though it is already captured as metadata. The same converter returns title='Page Title' via soup.title.string on the DocumentConverterResult, so the string arrives twice — once as the document title, once as body text. The path is in markitdown/converters/_html_converter.py:

body_elm = soup.find("body")
if body_elm:
    webpage_text = _CustomMarkdownify(**kwargs).convert_soup(body_elm)
else:
    webpage_text = _CustomMarkdownify(**kwargs).convert_soup(soup)

BeautifulSoup(html, "html.parser") does not synthesize <body>, so a document that simply omits the tags takes the else branch and drags <head> in with it.

2. It is not only <title>. <script>/<style> are extracted before this point, so those are covered — but any other text-bearing head element leaks the same way, concatenated into the first line:

b'<html><head><title>T</title><meta name="d" content="C"><noscript>Enable JS</noscript></head><p>hello</p></html>'
# markdown: 'TEnable JS\n\nhello'

A minimal change is to convert <html> minus <head> when there is no <body>:

body_elm = soup.find("body")
if body_elm is None:
    head = soup.find("head")
    if head is not None:
        head.extract()  # title is already returned separately
webpage_text = _CustomMarkdownify(**kwargs).convert_soup(body_elm or soup)

The same applies to the RecursionError fallback a few lines below (target = body_elm if body_elm else soup) — there get_text() is what would splice the title into the first line of the plain-text result.

Your second case is the mirror image and needs the opposite move: when <body> is present, everything outside it is lost, because only that element is converted — the body's siblings are never visited. Converting soup with the body element replaced by its own children preserves both the stray text nodes and, combined with the head removal above, keeps metadata out of the body. Bigger diff, but it is the only shape that satisfies both examples.

(Disclosure: I maintain an HTML→markdown extractor, so this is the class of bug I spend my days on. No stake in markitdown either way.)

NG-PR0JECT avatar Sep 18 '26 06:09 NG-PR0JECT