mirror of
https://github.com/discourse/discourse.git
synced 2025-08-21 19:11:18 +08:00
FIX: HtmlToMarkdown should not convert empty/bad <img> tags
This commit is contained in:
parent
c880af8120
commit
bff36de130
2 changed files with 19 additions and 8 deletions
|
@ -134,20 +134,21 @@ class HtmlToMarkdown
|
|||
end
|
||||
|
||||
def visit_img(node)
|
||||
if @opts[:keep_img_tags]
|
||||
@stack[-1].markdown << node.to_html
|
||||
else
|
||||
title = node["alt"].presence || node["title"].presence
|
||||
@stack[-1].markdown << ""
|
||||
if is_valid_url?(node["src"])
|
||||
if @opts[:keep_img_tags]
|
||||
@stack[-1].markdown << node.to_html
|
||||
else
|
||||
title = node["alt"].presence || node["title"].presence
|
||||
@stack[-1].markdown << ""
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def visit_a(node)
|
||||
href = node["href"]
|
||||
if href.present? && (href.start_with?("http") || href.start_with?("www."))
|
||||
if is_valid_url?(node["href"])
|
||||
@stack[-1].markdown << "["
|
||||
traverse(node)
|
||||
@stack[-1].markdown << "](#{href})"
|
||||
@stack[-1].markdown << "](#{node["href"]})"
|
||||
else
|
||||
traverse(node)
|
||||
end
|
||||
|
@ -203,4 +204,8 @@ class HtmlToMarkdown
|
|||
(lines + [""]).join("\n")
|
||||
end
|
||||
|
||||
def is_valid_url?(url)
|
||||
url.present? && (url.start_with?("http") || url.start_with?("www."))
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue