Skip to content

Instantly share code, notes, and snippets.

@blockloop
Created May 17, 2013 16:41
Show Gist options
  • Save blockloop/5600341 to your computer and use it in GitHub Desktop.
Save blockloop/5600341 to your computer and use it in GitHub Desktop.
Plain text Jekyll excerpt plugin. How it works: - Reads markdown into html - Converts the html to plain text - grabs the first X characters (default 160) - adds to the post (use post.excerpt) Reasons I wrote it: - supports the ability to add your own excerpt in the front matter (does not override) - creates text-only excerpt for a footer message…
require 'maruku'
require 'nokogiri'
module Jekyll
class ExcerptBuilder < Generator
safe true
priority :high
EXCERPT_LENGTH = 160
def generate(site)
site.posts.map! do |post|
unless post.data['excerpt']
html = ::Maruku.new(post.content).to_html
excerpt = ::Nokogiri::HTML(html).text[0..EXCERPT_LENGTH].gsub(/\n/, ' ')
post.data['excerpt'] = excerpt.strip
end
post.data['excerpt'] += '&hellip;'
post
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment