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 = { |article, rendered, length, ellipsis|
  summary = article.default_summary_generator(rendered, length, ellipsis)
  f = Nokogiri::HTML.fragment(summary)'.//img').remove

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 which calls the Proc if one is available.

comments powered by Disqus