Skip to content

Commit

Permalink
Render li elements separately
Browse files Browse the repository at this point in the history
  • Loading branch information
Cosmo committed Feb 5, 2024
1 parent c5c49d9 commit fa4ce1b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions lib/downmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def convert
def html_to_markdown(node)
markdown = ""

node.children.each do |child|
node.children.each.with_index do |child, _index|
case child.name
when "a"
markdown += "[#{child.text.strip}](#{child["href"]})" if child["href"]
Expand All @@ -30,8 +30,15 @@ def html_to_markdown(node)
markdown += "#{html_to_markdown(child).strip}\n\n"
when "span", "u"
markdown += " #{html_to_markdown(child).strip} "
when "li"
if child.parent.name == "ol"
value = child["value"].nil? ? child.parent.css("li").index(child) + 1 : child["value"]
markdown += "#{value}. #{html_to_markdown(child).strip}\n"
elsif child.parent.name == "ul"
markdown += "* #{html_to_markdown(child).strip}\n"
end
when "ul"
child.css("li").each { |li| markdown += "* #{html_to_markdown(li).strip.strip}\n" }
markdown += "#{html_to_markdown(child).strip}\n"
markdown += "\n\n"
when "ol"
child.css("li").each_with_index do |li, index|
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/numbered_lists/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@


1. Test 3
2. Test 4
2. Test 4

0 comments on commit fa4ce1b

Please sign in to comment.