1 module SocialShareButtonHelper
4 SOCIAL_SHARE_CONFIG = {
5 :email => "social_icons/email.svg",
6 :bluesky => "social_icons/bluesky.svg",
7 :facebook => "social_icons/facebook.svg",
8 :linkedin => "social_icons/linkedin.svg",
9 :mastodon => "social_icons/mastodon.svg",
10 :telegram => "social_icons/telegram.svg",
11 :x => "social_icons/x.svg"
14 # Generates a set of social share buttons based on the specified options.
15 def render_social_share_buttons(opts = {})
16 sites = opts.fetch(:allow_sites, [])
17 valid_sites, invalid_sites = filter_allowed_sites(sites)
20 invalid_sites.each do |invalid_site|
21 Rails.logger.error("Invalid site or icon not configured: #{invalid_site}")
25 :class => "social-share-button d-flex gap-1 align-items-end flex-wrap mb-3"
27 valid_sites.map do |site|
29 :rel => ["nofollow", opts[:rel]].compact,
30 :class => "ssb-icon rounded-circle",
31 :title => I18n.t("application.share.#{site}.title"),
35 link_to generate_share_url(site, opts), link_options do
36 image_tag(icon_path(site), :alt => I18n.t("application.share.#{site}.alt"), :size => 28)
44 def filter_allowed_sites(sites)
45 valid_sites = sites.empty? ? SOCIAL_SHARE_CONFIG.keys : sites.select { |site| valid_site?(site) }
46 invalid_sites = sites - valid_sites
47 [valid_sites, invalid_sites]
51 SOCIAL_SHARE_CONFIG[site.to_sym] || ""
55 SOCIAL_SHARE_CONFIG.key?(site.to_sym)
58 def generate_share_url(site, params)
62 to = params[:to] || ""
63 subject = CGI.escape(params[:title])
64 body = CGI.escape(params[:url])
65 "mailto:#{to}?subject=#{subject}&body=#{body}"
67 via_str = params[:via] ? "&via=#{URI.encode_www_form_component(params[:via])}" : ""
68 hashtags_str = params[:hashtags] ? "&hashtags=#{URI.encode_www_form_component(params[:hashtags].join(','))}" : ""
69 "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}"
71 "https://www.linkedin.com/sharing/share-offsite/?url=#{URI.encode_www_form_component(params[:url])}"
73 "https://www.facebook.com/sharer/sharer.php?u=#{URI.encode_www_form_component(params[:url])}&t=#{URI.encode_www_form_component(params[:title])}"
75 "https://mastodonshare.com/?text=#{URI.encode_www_form_component(params[:title])}&url=#{URI.encode_www_form_component(params[:url])}"
77 "https://t.me/share/url?url=#{URI.encode_www_form_component(params[:url])}&text=#{URI.encode_www_form_component(params[:title])}"
79 "https://bsky.app/intent/compose?text=#{URI.encode_www_form_component(params[:title])}+#{URI.encode_www_form_component(params[:url])}"
81 raise ArgumentError, "Unsupported platform: #{platform}"