Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/markitdown/src/markitdown/converters/_markdownify.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ def convert_a(
**kwargs,
):
"""Same as usual converter, but removes JavaScript links and escapes URIs."""
original_text = text
prefix, suffix, text = markdownify.chomp(text) # type: ignore
if not text:
return ""
return original_text

if el.find_parent("pre") is not None:
return text
return original_text

href = el.get("href")
title = el.get("title")
Expand Down Expand Up @@ -95,14 +96,14 @@ def convert_a(
and not self.options["default_title"]
):
# Shortcut syntax
return "<%s>" % href
return "%s<%s>%s" % (prefix, href, suffix)
if self.options["default_title"] and not title:
title = href
title_part = ' "%s"' % title.replace('"', r"\"") if title else ""
return (
"%s[%s](%s%s)%s" % (prefix, text, href, title_part, suffix)
if href
else text
else original_text
)

def convert_img(
Expand Down
46 changes: 46 additions & 0 deletions packages/markitdown/tests/test_html_converter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import io

import pytest

from markitdown import MarkItDown


Expand Down Expand Up @@ -63,3 +65,47 @@ def test_html_href_does_not_quote_query_or_fragment() -> None:
markdown = _convert_html(f'<a href="{href}">example</a>')

assert f"[example]({expected_href})" in markdown


@pytest.mark.parametrize("attributes", ["", 'name="bookmark"', 'href=""'])
@pytest.mark.parametrize("text", [" middle ", " "])
def test_html_anchor_without_destination_preserves_word_boundaries(
attributes: str, text: str
) -> None:
markdown = _convert_html(f"<p>before<a {attributes}>{text}</a>after</p>")

assert markdown == f"before{text}after"


@pytest.mark.parametrize("href", ["", 'href="https://example.com"'])
def test_html_anchor_in_pre_preserves_whitespace(href: str) -> None:
markdown = _convert_html(f"<pre>before<a {href}> middle\n </a>after</pre>")

assert markdown == "```\nbefore middle\n after\n```"


def test_html_autolink_preserves_surrounding_spaces() -> None:
markdown = _convert_html(
'<p>before<a href="https://example.com"> https://example.com </a>after</p>'
)

assert markdown == "before <https://example.com> after"


def test_html_link_with_whitespace_only_text_preserves_word_boundary() -> None:
markdown = _convert_html('<p>before<a href="https://example.com"> </a>after</p>')

assert markdown == "before after"


@pytest.mark.parametrize(
"href, expected",
[
("https://example.com", "before [middle](https://example.com) after"),
("javascript:void(0)", "before middle after"),
],
)
def test_html_link_preserves_surrounding_spaces(href: str, expected: str) -> None:
markdown = _convert_html(f'<p>before<a href="{href}"> middle </a>after</p>')

assert markdown == expected