mirror of
https://github.com/discourse/discourse.git
synced 2025-05-21 18:12:32 +08:00
FIX: HtmlToMarkdown should not convert empty/bad <img> tags
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user