diff --git a/packages/markitdown/src/markitdown/converters/_rss_converter.py b/packages/markitdown/src/markitdown/converters/_rss_converter.py index d24eea4a81..a4ff2b74e9 100644 --- a/packages/markitdown/src/markitdown/converters/_rss_converter.py +++ b/packages/markitdown/src/markitdown/converters/_rss_converter.py @@ -327,6 +327,12 @@ def _get_data_by_tag_name( ) -> Union[str, None]: """Get data from first child element with the given tag name. Returns None when no such element is found. + + An element's text is not necessarily a single node: a value written as + ``\\n \\n`` reaches the parser + as whitespace, then the CDATA section, then more whitespace. Reading + only the first of those returns the layout and drops the value, so all + of the element's own text and CDATA children are joined. """ if element.namespaceURI == ATOM_NAMESPACE: nodes = element.getElementsByTagNameNS(ATOM_NAMESPACE, tag_name) @@ -334,8 +340,11 @@ def _get_data_by_tag_name( nodes = element.getElementsByTagName(tag_name) if not nodes: return None - fc = nodes[0].firstChild - if fc: - if hasattr(fc, "data"): - return fc.data - return None + parts = [ + child.data + for child in nodes[0].childNodes + if child.nodeType in (Node.TEXT_NODE, Node.CDATA_SECTION_NODE) + ] + if not parts: + return None + return "".join(parts) diff --git a/packages/markitdown/tests/test_rss_converter.py b/packages/markitdown/tests/test_rss_converter.py index ddc690e430..8530edf2e3 100644 --- a/packages/markitdown/tests/test_rss_converter.py +++ b/packages/markitdown/tests/test_rss_converter.py @@ -338,3 +338,97 @@ def test_atom_plain_text_layout_whitespace_is_removed() -> None: "", "Then check status.", ] + + +def test_rss_description_survives_a_cdata_section_on_its_own_line() -> None: + """A pretty-printed CDATA payload is a later child, not the first one.""" + feed = b""" + + Example feed + Example feed description + + Example item + + The body of the item.

]]> +
+
+
+""" + + result = RssConverter().convert(io.BytesIO(feed), StreamInfo(extension=".rss")) + + assert "The **body** of the item." in result.markdown + + +def test_rss_title_survives_a_cdata_section_in_the_middle() -> None: + """Text either side of a CDATA section belongs to the same value.""" + feed = b""" + + Example feed + Example feed description + + Quarterly <![CDATA[R&D]]> report + Body. + + +""" + + result = RssConverter().convert(io.BytesIO(feed), StreamInfo(extension=".rss")) + + assert "## Quarterly R&D report" in result.markdown + + +def test_atom_summary_survives_a_cdata_section_on_its_own_line() -> None: + feed = b""" + + Example feed + + Example entry + + A structured summary.

]]> +
+
+
+""" + + result = RssConverter().convert( + io.BytesIO(feed), StreamInfo(mimetype="application/atom+xml") + ) + + assert "A *structured* summary." in result.markdown + + +def test_rss_cdata_only_description_is_unchanged() -> None: + """The case that already worked must keep working.""" + feed = b""" + + Example feed + Example feed description + + Example item + Only a CDATA section.

]]>
+
+
+""" + + result = RssConverter().convert(io.BytesIO(feed), StreamInfo(extension=".rss")) + + assert "Only a CDATA section." in result.markdown + + +def test_rss_item_without_a_description_is_still_converted() -> None: + """An absent element must stay absent, not become an empty string.""" + feed = b""" + + Example feed + Example feed description + + Example item + + +""" + + result = RssConverter().convert(io.BytesIO(feed), StreamInfo(extension=".rss")) + + assert result.title == "Example feed" + assert "## Example item" in result.markdown