Skip to content

Instantly share code, notes, and snippets.

@judofyr
Created September 12, 2018 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save judofyr/eee12a363a76cddfc8cb7f78ac11b7a2 to your computer and use it in GitHub Desktop.
Save judofyr/eee12a363a76cddfc8cb7f78ac11b7a2 to your computer and use it in GitHub Desktop.
Example of using Nokogiri to minify HTML
require 'nokogiri'
def minify_html(html)
doc = Nokogiri::HTML.parse(html)
doc.xpath('//comment()').each { |comment| comment.remove }
doc.to_html(save_with: Nokogiri::XML::Node::SaveOptions::AS_HTML)
end
html_code = '<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Bla bla bla</title></head><body><p>Abc <!--stuff to remove--></p></body></html>'
puts minify_html(html_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment