Sha256: f0846abea7dd7d5cecd9ab60c027dd2979973dee482e091e53b5ba32ae0bbd8c

Contents?: true

Size: 801 Bytes

Versions: 2

Compression:

Stored size: 801 Bytes

Contents

# frozen_string_literal: true

module Jekyll
  module Filters
    module SocialNetwork
      # Takes a URL and returns a Hash of attributes, useful when you
      # want to generate social network buttons from an undetermined
      # list of URLs.
      #
      # @param [String]
      # @return [Hash]
      def social_network(url)
        return {} unless url.is_a? String

        require 'uri'

        uri  = URI url
        uri.host.sub! 'www.', ''
        name = %r{/@\w+} =~ uri.query ? 'mastodon' : uri.host.split('.', 2).first

        { 'host' => uri.host, 'name' => name, 'url' => url }.to_liquid
      rescue URI::InvalidURIError => e
        Jekyll.logger.warn e.message

        {}.to_liquid
      end
    end
  end
end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sutty-liquid-0.2.1 lib/jekyll/filters/social_network.rb
sutty-liquid-0.2.0 lib/jekyll/filters/social_network.rb