SBaronda.com a little place I like to call home.


Removing images in Middleman article summaries

Jun. 2, 2019

Middleman has been a awesome blog engine that is pretty easily customizable. One thing that I noticed recently is that when I try to render images withing the summary section the HTML was wonky. Typically, I don't need any images at all and actually wanna prevent images from getting rendered.

Within your config.rb file you can remove images by creating a custom #summary_generator. This assumes that you have nokogiri as a required Gem. Just paste this in within the activate :blog section:

blog.summary_generator = Proc.new { |article, rendered, length, ellipsis|
  summary = article.default_summary_generator(rendered, length, ellipsis)
  f = Nokogiri::HTML.fragment(summary)
  f.search('.//img').remove
  f.to_html
}

Removes images from your article summaries. This worked on an older version of Middleman, 3.5.2 to be exact, but should work on newer versions as well.

The key to this is https://github.com/middleman/middleman-blog/blob/3e57af64280962eeba418ae325beb6fa1e8a4adc/lib/middleman-blog/blog_article.rb#L116-L152 which calls the Proc if one is available.


comments powered by Disqus