Merge pull request #125 from jekyll/pr/regex

Simplify minify regex
This commit is contained in:
Pat Hawks 2016-10-17 12:59:24 -05:00 committed by GitHub
commit 39c2e06232
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