Created
September 12, 2018 13:28
-
-
Save judofyr/eee12a363a76cddfc8cb7f78ac11b7a2 to your computer and use it in GitHub Desktop.
Example of using Nokogiri to minify HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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