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 },
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ SocialShareTag

Returns a new instance of SocialShareTag.



22
23
24
25
# File 'lib/jekyll/social_share_tag.rb', line 22

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

Instance Method Details

#render(context) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jekyll/social_share_tag.rb', line 27

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["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