From e38014772b50fc387300b13a31800b92afc7ea6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 3 May 2017 19:40:34 +0200 Subject: [PATCH] FIX: skip hidden (no tracking for you) --- lib/html_to_markdown.rb | 11 +++++++++-- spec/components/html_to_markdown_spec.rb | 7 +++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/html_to_markdown.rb b/lib/html_to_markdown.rb index 40997fa5c94..30c394648d6 100644 --- a/lib/html_to_markdown.rb +++ b/lib/html_to_markdown.rb @@ -37,7 +37,7 @@ class HtmlToMarkdown end def visit(node) - return if node["style"] && node["style"][/display[[:space:]]*:[[:space:]]*none/] + return if node["style"] && node["style"][/display\s*:\s*none/] if node.description&.block? && node.parent&.description&.block? && @stack[-1].markdown.size > 0 block = @stack[-1].dup @@ -136,7 +136,7 @@ class HtmlToMarkdown end def visit_img(node) - if is_valid_url?(node["src"]) + if is_valid_url?(node["src"]) && is_visible_img?(node) if @opts[:keep_img_tags] @stack[-1].markdown << node.to_html else @@ -210,4 +210,11 @@ class HtmlToMarkdown url.present? && (url.start_with?("http") || url.start_with?("www.")) end + def is_visible_img?(img) + return false if img["width"].present? && img["width"].to_i == 0 + return false if img["height"].present? && img["height"].to_i == 0 + return false if img["style"].present? && img["style"][/(width|height)\s*:\s*0/] + true + end + end diff --git a/spec/components/html_to_markdown_spec.rb b/spec/components/html_to_markdown_spec.rb index 52c0eb670a1..091bc79ee80 100644 --- a/spec/components/html_to_markdown_spec.rb +++ b/spec/components/html_to_markdown_spec.rb @@ -71,6 +71,13 @@ describe HtmlToMarkdown do expect(html_to_markdown(%Q{})).to eq("") end + it "skips hidden " do + expect(html_to_markdown(%Q{})).to eq("") + expect(html_to_markdown(%Q{})).to eq("") + expect(html_to_markdown(%Q{})).to eq("") + expect(html_to_markdown(%Q{})).to eq("") + end + (1..6).each do |n| it "converts " do expect(html_to_markdown("Header #{n}")).to eq("#" * n + " Header #{n}")