Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
60 lines (44 sloc) 2.19 KB
require 'active_support/core_ext/string/filters'
Jekyll::Hooks.register :site, :post_read do |site|
site.categories['short'].each do |post|
if['excerpt_separator'].nil? && post.content =~ /(<!--\s*more\s*-->)/["excerpt_separator"] = $1['excerpt'] =
has_excerpt_defined = !!['excerpt_separator']
# in Jekyll, all posts have an excerpt.
# I want to know if one was explicity defined
feed_excerpt = has_excerpt_defined ?["excerpt"].to_s : post.content
# not exactly the model for HTML santization,
# but enough when you are the author as well
feed_excerpt = feed_excerpt
.gsub(/<.*?>/m, '')
.gsub(/\r?\n/, ' ')
# hard coded the domain that we get a true warning about length.
published_url = "{post.url}"
feed_description = "#{['title']} - #{feed_excerpt}"
# since this is for twitter, default to the shortened url if
# it exists
external_url =['short_url'] ||['link_url']
if external_url && !external_url.empty?
# if we are linking else where, just link to it
feed_description = "#{feed_description} #{external_url}"
elsif has_excerpt_defined
# is there likely more to this post?
feed_description = "#{feed_description} #{published_url}"
feed_description = feed_description.strip
# slight change published_url is already in the post.
# It will likely be removed via truncating and then re-added
if feed_description.length > 280
# Tweet max length of 280 minus 3 elipses plus blank space + url length
lenght_to_truncate_at = (280 - (4 + published_url.length))
# try to truncate at a word break
truncated_feed_description = feed_description.truncate(lenght_to_truncate_at, separator: /\s/)
Jekyll.logger.warn "Warning:", "Feed description is too long: #{['title']}. #{feed_description.length} characters"
Jekyll.logger.warn "Truncated:", truncated_feed_description
feed_description = "#{truncated_feed_description.strip} #{published_url}"
end['feed_description'] = feed_description