Simplify minify regex

This commit is contained in:
Pat Hawks 2016-10-15 14:24:13 -05:00
parent 7b5ade75ae
commit 07eeffb6d9
No known key found for this signature in database
GPG Key ID: F1746FF5F18B3D1B
2 changed files with 14 additions and 6 deletions

View File

@ -4,7 +4,16 @@ module Jekyll
class SeoTag < Liquid::Tag
attr_accessor :context
MINIFY_REGEX = %r!([>,]\n|[%}]})\s+?(<|{[{%]|[ ]+\")!
# Matches all whitespace that follows either
# 1. A '}', which closes a Liquid tag
# 2. A '{', which opens a JSON block
# 3. A '>' followed by a newline, which closes an XML tag or
# 4. A ',' followed by a newline, which ends a JSON line
# We will strip all of this whitespace to minify the template
# We will not strip any whitespace if the next character is a '-'
# so that we do not interfere with the HTML comment at the
# very begining
MINIFY_REGEX = %r!(?<=[{}]|[>,]\n)\s+(?\!-)!
def initialize(_tag_name, text, _tokens)
super
@ -51,7 +60,7 @@ module Jekyll
def template_contents
@template_contents ||= begin
File.read(template_path).gsub(MINIFY_REGEX, '\1\2').chomp
File.read(template_path).gsub(MINIFY_REGEX, "")
end
end

View File

@ -241,10 +241,9 @@ EOS
it "minifies JSON-LD" do
expected = <<-EOS
{
"@context": "http://schema.org",
"@type": "BlogPosting",
"headline": "post",
{"@context": "http://schema.org",
"@type": "BlogPosting",
"headline": "post",
EOS
expect(output).to match(expected)
end