1 module SocialShareButtonHelper
4 SOCIAL_SHARE_CONFIG = {
5 :email => "social_icons/email.svg",
6 :facebook => "social_icons/facebook.svg",
7 :linkedin => "social_icons/linkedin.svg",
8 :mastodon => "social_icons/mastodon.svg",
9 :telegram => "social_icons/telegram.svg",
10 :x => "social_icons/x.svg"
13 def self.filter_allowed_sites(sites)
14 valid_sites = sites.empty? ? SOCIAL_SHARE_CONFIG.keys : sites.select { |site| valid_site?(site) }
15 invalid_sites = sites - valid_sites
16 [valid_sites, invalid_sites]
19 def self.icon_path(site)
20 SOCIAL_SHARE_CONFIG[site.to_sym] || ""
23 def self.valid_site?(site)
24 SOCIAL_SHARE_CONFIG.key?(site.to_sym)
27 def self.generate_share_url(site, params)
31 to = params[:to] || ""
32 subject = CGI.escape(params[:title])
33 body = CGI.escape(params[:url])
34 "mailto:#{to}?subject=#{subject}&body=#{body}"
36 via_str = params[:via] ? "&via=#{URI.encode_www_form_component(params[:via])}" : ""
37 hashtags_str = params[:hashtags] ? "&hashtags=#{URI.encode_www_form_component(params[:hashtags].join(','))}" : ""
38 "https://x.com/intent/tweet?url=#{URI.encode_www_form_component(params[:url])}&text=#{URI.encode_www_form_component(params[:title])}#{hashtags_str}#{via_str}"
40 "https://www.linkedin.com/sharing/share-offsite/?url=#{URI.encode_www_form_component(params[:url])}"
42 "https://www.facebook.com/sharer/sharer.php?u=#{URI.encode_www_form_component('params[:url]')}&t=#{URI.encode_www_form_component(params[:title])}"
44 "https://mastodonshare.com/?text=#{URI.encode_www_form_component(params[:title])}&url=#{URI.encode_www_form_component(params[:url])}"
46 "https://t.me/share/url?url=#{URI.encode_www_form_component(params[:url])}&text=#{URI.encode_www_form_component(params[:title])}"
48 raise ArgumentError, "Unsupported platform: #{platform}"