Skip to content

Commit 9382dc5

Browse files
authored
Merge pull request #13 from ejscunha/ecunha/fix-breaking-behaviour
fix: breaking behaviour with inline tags
2 parents 691ad52 + a481858 commit 9382dc5

3 files changed

Lines changed: 45 additions & 5 deletions

File tree

lib/html2markdown/converter.ex

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,22 @@ defmodule Html2Markdown.Converter do
4949
if acc == [] do
5050
[iodata]
5151
else
52-
[acc, "\n\n", iodata]
52+
[acc, get_spacing_between_elements(node), iodata]
5353
end
5454
end
5555
end)
5656
end
5757

58+
defp get_spacing_between_elements({tag, _, _}) when is_binary(tag) do
59+
if ElementTypes.block_element?(tag) do
60+
"\n\n"
61+
else
62+
" "
63+
end
64+
end
65+
66+
defp get_spacing_between_elements(_node), do: " "
67+
5868
# Process nodes to iolist for better performance
5969
defp process_node_to_iolist({"h1", _, children}, opts),
6070
do: ["# ", process_children_to_iolist(children, opts)]
@@ -426,7 +436,7 @@ defmodule Html2Markdown.Converter do
426436
defp process_ordered_list_item_to_iolist(other, _index, opts),
427437
do: process_node_to_iolist(other, opts)
428438

429-
# Context-aware processing for better spacing control
439+
# Context-aware processing for better spacing control
430440
defp process_children_with_context(children, opts, context) do
431441
final_context = determine_context(children, context)
432442

test/html2markdown_test.exs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,4 +543,36 @@ defmodule Html2MarkdownTest do
543543
assert Html2Markdown.convert(html) == expected
544544
end
545545
end
546+
547+
describe "HTML tags with normal text" do
548+
test "emphasis tag" do
549+
html = """
550+
<em>Emphasis</em> normal text
551+
"""
552+
553+
expected = "*Emphasis* normal text"
554+
555+
assert Html2Markdown.convert(html) == expected
556+
end
557+
558+
test "strong tag" do
559+
html = """
560+
<strong>Bold</strong> normal text
561+
"""
562+
563+
expected = "**Bold** normal text"
564+
565+
assert Html2Markdown.convert(html) == expected
566+
end
567+
568+
test "span tag" do
569+
html = """
570+
<span>Span text</span> normal text
571+
"""
572+
573+
expected = "Span text normal text"
574+
575+
assert Html2Markdown.convert(html) == expected
576+
end
577+
end
546578
end

test/support/fixtures/elixir.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ Here is an example of **strong text** and *emphasized text*.
7979

8080
Subscript: H <sub>2</sub> O
8181

82-
Superscript: E = mc <sup>2</sup>
83-
84-
82+
Superscript: E = mc <sup>2</sup>
8583

8684
---
8785

0 commit comments

Comments
 (0)