Sha256: c2232a745ef5a859edb8528fc11068f6d03e82a600e10afe363a641b526ef2ce

Contents?: true

Size: 1.62 KB

Versions: 6

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module Jekyll
  module Filters
    module SocialNetwork
      MASTODON   = %r{\A/@\w+\z}.freeze
      NON_MASTODON = %w[youtube.com tiktok.com]
      SUBDOMAINS = /\A(www|chat)\./.freeze
      DOMAIN_MAP = {
        't.me' => 'telegram',
        'youtu.be' => 'youtube',
        'open.spotify.com' => 'spotify'
      }.freeze

      # Takes a URL and returns a Hash of attributes, useful when you
      # want to generate social network buttons from an undetermined
      # list of URLs.
      #
      # Example usage:
      #
      # {% assign mastodon = 'https://todon.nl/@sutty' | social_network %}
      # <a href="{{ mastodon.url }}">
      #   <i class="fa-{{ mastodon.name }}"></i>
      #
      #   {{ mastodon.name | capitalize }}
      # </a>
      #
      # @param [String]
      # @return [Hash]
      def social_network(url)
        begin
          require 'uri'

          uri  = URI url
          host = uri.host = uri.host.sub SUBDOMAINS, ''

          name   = DOMAIN_MAP[host]
          name ||= mastodon?(uri) ? 'mastodon' : host.split('.', 2).first
        rescue ArgumentError, URI::InvalidURIError => e
          Jekyll.logger.warn(e.message) if Jekyll.respond_to?(:logger)
        ensure
          host ||= url
          name ||= 'globe'
        end

        { 'host' => host, 'name' => name, 'url' => url }.to_liquid
      end

      private

      # Other social networks use @username on the URL too
      def mastodon?(uri)
        return false if NON_MASTODON.include? uri.host

        MASTODON =~ uri.path
      end
    end
  end
end

Liquid::Template.register_filter(Jekyll::Filters::SocialNetwork)

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sutty-liquid-0.12.3 lib/jekyll/filters/social_network.rb
sutty-liquid-0.12.2 lib/jekyll/filters/social_network.rb
sutty-liquid-0.12.1 lib/jekyll/filters/social_network.rb
sutty-liquid-0.12.0 lib/jekyll/filters/social_network.rb
sutty-liquid-0.12.0rc0 lib/jekyll/filters/social_network.rb
sutty-liquid-0.11.12 lib/jekyll/filters/social_network.rb