Class: Jekyll::SocialShareTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll/social_share_tag.rb

Constant Summary collapse

DEFAULT_SERVICES =
%w[bluesky mastodon facebook reddit linkedin x threads email copy].freeze
SERVICE_CONFIG =
{
  "bluesky"     => { label: "Bluesky",      url_template: "https://bsky.app/intent/compose?text=%<title>s%%20%<url>s" },
  "mastodon"    => { label: "Mastodon",     url_template: :mastodon },
  "facebook"    => { label: "Facebook",     url_template: "https://www.facebook.com/sharer/sharer.php?u=%<url>s" },
  "reddit"      => { label: "Reddit",       url_template: "https://www.reddit.com/submit?url=%<url>s&title=%<title>s" },
  "linkedin"    => { label: "LinkedIn",     url_template: "https://www.linkedin.com/sharing/share-offsite/?url=%<url>s" },
  "x"           => { label: "X",            url_template: "https://twitter.com/intent/tweet?url=%<url>s&text=%<title>s" },
  "threads"     => { label: "Threads",      url_template: "https://www.threads.net/intent/post?text=%<title>s%%20%<url>s" },
  "email"       => { label: "Email",        url_template: :email },
  "copy"        => { label: "Copy link",    url_template: :copy },
  "whatsapp"    => { label: "WhatsApp",     url_template: "https://wa.me/?text=%<title>s%%20%<url>s" },
  "telegram"    => { label: "Telegram",     url_template: "https://t.me/share/url?url=%<url>s&text=%<title>s" },
  "hackernews"  => { label: "Hacker News",  url_template: "https://news.ycombinator.com/submitlink?u=%<url>s&t=%<title>s" },
  "pinterest"   => { label: "Pinterest",    url_template: "https://pinterest.com/pin/create/button/?url=%<url>s&description=%<title>s" },
  "pocket"      => { label: "Pocket",       url_template: "https://getpocket.com/edit?url=%<url>s&title=%<title>s" },
  "tumblr"      => { label: "Tumblr",       url_template: "https://www.tumblr.com/widgets/share/tool?canonicalUrl=%<url>s&title=%<title>s" },
  "flipboard"   => { label: "Flipboard",    url_template: "https://share.flipboard.com/?v=flipit&title=%<title>s&url=%<url>s" },
  "line"        => { label: "LINE",         url_template: "https://social-plugins.line.me/lineit/share?url=%<url>s" },
  "print"       => { label: "Print",        url_template: :print },
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ SocialShareTag

Returns a new instance of SocialShareTag.



31
32
33
34
# File 'lib/jekyll/social_share_tag.rb', line 31

def initialize(tag_name, markup, tokens)
  super
  @params = parse_params(markup.strip)
end

Instance Method Details

#render(context) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jekyll/social_share_tag.rb', line 36

def render(context)
  site    = context.registers[:site]
  page    = context.registers[:page]
  config  = site.config["social_share"] || {}

  return "" if page["share"] == false

  services    = resolve_services(@params["services"] || config["services"] || DEFAULT_SERVICES)
  icon_only   = bool_param(@params, config, "icon_only",  false)
  text_only   = bool_param(@params, config, "text_only",  false)
  label       = @params.key?("label") ? @params["label"] : (config.key?("label") ? config["label"] : "Share")
  show_label  = label != false && label != "false"
  mastodon_relay = config["mastodon_instance"] || "https://toot.kytta.dev"

  page_url   = absolute_url(page["url"] || "", site)
  page_title = URI.encode_www_form_component(@params["title"] || page["title"] || site.config["title"] || "")

  items = services.map do |svc|
    cfg = SERVICE_CONFIG[svc]
    next nil unless cfg

    href = build_url(svc, cfg[:url_template], page_url, page_title, mastodon_relay)
    icon = SocialShare::ICONS[svc] || ""
    render_item(svc, cfg[:label], href, icon, icon_only, text_only)
  end.compact

  render_wrapper(label, show_label, items)
end