FIX: use absolute URL for twitter:image tag

FIX: use absolute URL for twitter:image tag

diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index cd3ed04..82a5d5f 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -210,17 +210,10 @@ module ApplicationHelper
       opts[:image] = SiteSetting.site_apple_touch_icon_url
     end
 
-    # Use the correct scheme for open graph image
-    if opts[:image].present?
-      if opts[:image].start_with?("//")
-        uri = URI(Discourse.base_url)
-        opts[:image] = "#{uri.scheme}:#{opts[:image]}"
-      elsif opts[:image].start_with?("/uploads/")
-        opts[:image] = "#{Discourse.base_url}#{opts[:image]}"
-      elsif GlobalSetting.relative_url_root && opts[:image].start_with?(GlobalSetting.relative_url_root)
-        opts[:image] = "#{Discourse.base_url_no_prefix}#{opts[:image]}"
-      end
-    end
+    # Use the correct scheme for opengraph/twitter image
+    opts[:image] = get_absolute_image_url(opts[:image]) if opts[:image].present?
+    opts[:twitter_summary_large_image] =
+      get_absolute_image_url(opts[:twitter_summary_large_image]) if opts[:twitter_summary_large_image].present?
 
     # Add opengraph & twitter tags
     result = []
@@ -452,4 +445,17 @@ module ApplicationHelper
 
     setup_data
   end
+
+  def get_absolute_image_url(link)
+    absolute_url = link
+    if link.start_with?("//")
+      uri = URI(Discourse.base_url)
+      absolute_url = "#{uri.scheme}:#{link}"
+    elsif link.start_with?("/uploads/")
+      absolute_url = "#{Discourse.base_url}#{link}"
+    elsif GlobalSetting.relative_url_root && link.start_with?(GlobalSetting.relative_url_root)
+      absolute_url = "#{Discourse.base_url_no_prefix}#{link}"
+    end
+    absolute_url
+  end
 end

GitHub sha: 9919f160

1 Like