Skip to content

Instantly share code, notes, and snippets.

@JimmyRittenborg
Last active June 17, 2023 12:11
Show Gist options
  • Select an option

  • Save JimmyRittenborg/6f0bb74a996f9676ead5 to your computer and use it in GitHub Desktop.

Select an option

Save JimmyRittenborg/6f0bb74a996f9676ead5 to your computer and use it in GitHub Desktop.

Revisions

  1. JimmyRittenborg revised this gist Apr 7, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion compress.liquid
    Original file line number Diff line number Diff line change
    @@ -61,7 +61,7 @@
    - There is an 'missing semicolon' issue with Shopify's Enhanced Ecommerce for Google Analytics, which gets corrected here as well.
    - There is an 'missing semicolon' issue with Shopify's inline Admin Panel script, which gets corrected here as well.
    {% endcomment %}{% assign _content = _content | replace: '<script >', '<script>' | replace: '<script ', '<script ' | replace: 'analytics.async = true;', ';analytics.async = true;' | replace: 'if (window.addEventListener){ addEventListener("message", ', ';if (window.addEventListener){ addEventListener("message", ' %}{% comment %}
    {% endcomment %}{% assign _content = _content | replace: '<script >', '<script>' | replace: '<script ', '<script ' | replace: 'analytics.onload = onload', ';analytics.onload = onload' | replace: 'analytics.async = true;', ';analytics.async = true;' | replace: 'if (window.addEventListener){ addEventListener("message", ', ';if (window.addEventListener){ addEventListener("message", ' %}{% comment %}
    Compress whitespace outside of <pre>
    - We have to ensure a space after every </pre> otherwise Liquid won't match, and blow up everything if two elements comes straight after eachother: <pre></pre><div></div>
  2. JimmyRittenborg revised this gist Mar 17, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion compress.liquid
    Original file line number Diff line number Diff line change
    @@ -59,8 +59,9 @@
    Cleanup
    - There is an 'missing semicolon' issue with Shopify's Enhanced Ecommerce for Google Analytics, which gets corrected here as well.
    - There is an 'missing semicolon' issue with Shopify's inline Admin Panel script, which gets corrected here as well.
    {% endcomment %}{% assign _content = _content | replace: '<script >', '<script>' | replace: '<script ', '<script ' | replace: 'analytics.async = true;', ';analytics.async = true;' %}{% comment %}
    {% endcomment %}{% assign _content = _content | replace: '<script >', '<script>' | replace: '<script ', '<script ' | replace: 'analytics.async = true;', ';analytics.async = true;' | replace: 'if (window.addEventListener){ addEventListener("message", ', ';if (window.addEventListener){ addEventListener("message", ' %}{% comment %}
    Compress whitespace outside of <pre>
    - We have to ensure a space after every </pre> otherwise Liquid won't match, and blow up everything if two elements comes straight after eachother: <pre></pre><div></div>
  3. JimmyRittenborg revised this gist Mar 17, 2015. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions compress.liquid
    Original file line number Diff line number Diff line change
    @@ -4,13 +4,12 @@
    {% endcomment %}{% capture _content %}{{ content }}{% endcapture %}{% comment %}
    Remove redundant end tags
    - This is really not something a bot always can or should decide, so it's mostly here as a reminder
    Remove redundant closing tags
    {% endcomment %}{% comment %}{% assign _endings = "html head body li dt dd p rt rp optgroup option colgroup caption thead tbody tfoot tr td th" | split: " " %}{% for _element in _endings %}
    {% endcomment %}{% assign _endings = "html head body li dt dd p rt rp optgroup option colgroup caption thead tbody tfoot tr td th" | split: " " %}{% for _element in _endings %}
    {% capture _end %}</{{ _element }}>{% endcapture %}
    {% assign _content = _content | remove: _end %}
    {% endfor %}{% endcomment %}{% comment %}
    {% endfor %}{% comment %}
    Create a variable to match newlines
  4. JimmyRittenborg revised this gist Mar 16, 2015. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions compress.liquid
    Original file line number Diff line number Diff line change
    @@ -5,11 +5,12 @@
    {% endcomment %}{% capture _content %}{{ content }}{% endcapture %}{% comment %}
    Remove redundant end tags
    - This is really not something a bot always can or should decide, so it's mostly here as a reminder
    {% endcomment %}{% assign _endings = "html head body li dt dd p rt rp optgroup option colgroup caption thead tbody tfoot tr td th" | split: " " %}{% for _element in _endings %}
    {% endcomment %}{% comment %}{% assign _endings = "html head body li dt dd p rt rp optgroup option colgroup caption thead tbody tfoot tr td th" | split: " " %}{% for _element in _endings %}
    {% capture _end %}</{{ _element }}>{% endcapture %}
    {% assign _content = _content | remove: _end %}
    {% endfor %}{% comment %}
    {% endfor %}{% endcomment %}{% comment %}
    Create a variable to match newlines
  5. JimmyRittenborg revised this gist Mar 16, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion compress.liquid
    Original file line number Diff line number Diff line change
    @@ -76,7 +76,7 @@
    {% endfor %}{% comment %}
    Remove html comments
    - Matching just "<!--" and "-->" will whipe out IE Conditional Comments, which you probable don't want
    - Matching just "<!--" and "-->" will whipe out IE Conditional Comments, which you probably don't want
    {% endcomment %}{% assign _comment_befores = _content | split: "<!-- " %}{% for _comment_before in _comment_befores %}
    {% assign _comment_content = _comment_before | split: " -->" | first %}
  6. JimmyRittenborg revised this gist Mar 16, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions compress.liquid
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@
    {% endcapture %}{% comment %}
    Prepare inline <script>'s for compression
    - We have to ensure a space after every </script> otherwise Liquid won't match, and blow up everything up if two elements comes straight after eachother: <script></script><div></div>
    - We have to ensure a space after every </script> otherwise Liquid won't match, and blow up everything if two elements comes straight after eachother: <script></script><div></div>
    {% endcomment %}{% assign _content = _content | replace: '</script>', '</script><!-- [SPACE] -->' | replace: '<!-- [SPACE] -->', " " %}{% assign _script_befores = _content | split: "<script" %}{% assign _content = "" %}{% for _script_before in _script_befores %}
    {% assign _scripts = _script_before | split: "</script>" %}
    @@ -63,7 +63,7 @@
    {% endcomment %}{% assign _content = _content | replace: '<script >', '<script>' | replace: '<script ', '<script ' | replace: 'analytics.async = true;', ';analytics.async = true;' %}{% comment %}
    Compress whitespace outside of <pre>
    - We have to ensure a space after every </pre> otherwise Liquid won't match, and blow up everything up if two elements comes straight after eachother: <pre></pre><div></div>
    - We have to ensure a space after every </pre> otherwise Liquid won't match, and blow up everything if two elements comes straight after eachother: <pre></pre><div></div>
    {% endcomment %}{% assign _content = _content | replace: '</pre>', '</pre><!-- [SPACE] -->' | replace: '<!-- [SPACE] -->', " " %}{% assign _pre_befores = _content | split: "<pre" %}{% assign _content = "" %}{% for _pre_before in _pre_befores %}
    {% assign _pres = _pre_before | split: "</pre>" %}
  7. JimmyRittenborg revised this gist Mar 16, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion compress.liquid
    Original file line number Diff line number Diff line change
    @@ -76,7 +76,7 @@
    {% endfor %}{% comment %}
    Remove html comments
    - Matching just "<!--" will whipe out IE Conditional Comments, which you probable don't want. "-->"
    - Matching just "<!--" and "-->" will whipe out IE Conditional Comments, which you probable don't want
    {% endcomment %}{% assign _comment_befores = _content | split: "<!-- " %}{% for _comment_before in _comment_befores %}
    {% assign _comment_content = _comment_before | split: " -->" | first %}
  8. JimmyRittenborg revised this gist Mar 16, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion compress.liquid
    Original file line number Diff line number Diff line change
    @@ -76,7 +76,7 @@
    {% endfor %}{% comment %}
    Remove html comments
    - Matching just "<!--" will whipe out IE Conditional Comments, which you probable don't want
    - Matching just "<!--" will whipe out IE Conditional Comments, which you probable don't want. "-->"
    {% endcomment %}{% assign _comment_befores = _content | split: "<!-- " %}{% for _comment_before in _comment_befores %}
    {% assign _comment_content = _comment_before | split: " -->" | first %}
  9. JimmyRittenborg created this gist Mar 16, 2015.
    98 changes: 98 additions & 0 deletions compress.liquid
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@
    {% comment %}
    Input
    {% endcomment %}{% capture _content %}{{ content }}{% endcapture %}{% comment %}
    Remove redundant end tags
    {% endcomment %}{% assign _endings = "html head body li dt dd p rt rp optgroup option colgroup caption thead tbody tfoot tr td th" | split: " " %}{% for _element in _endings %}
    {% capture _end %}</{{ _element }}>{% endcapture %}
    {% assign _content = _content | remove: _end %}
    {% endfor %}{% comment %}
    Create a variable to match newlines
    {% endcomment %}{% capture nl %}
    {% endcapture %}{% comment %}
    Prepare inline <script>'s for compression
    - We have to ensure a space after every </script> otherwise Liquid won't match, and blow up everything up if two elements comes straight after eachother: <script></script><div></div>
    {% endcomment %}{% assign _content = _content | replace: '</script>', '</script><!-- [SPACE] -->' | replace: '<!-- [SPACE] -->', " " %}{% assign _script_befores = _content | split: "<script" %}{% assign _content = "" %}{% for _script_before in _script_befores %}
    {% assign _scripts = _script_before | split: "</script>" %}
    {% case _scripts.size %}
    {% when 2 %}
    {% assign _script = _scripts.first |

    replace: "// //", "/*[COMMENT]*/ " |
    replace: "// //", "/*[COMMENT]*/ " |
    replace: "// ", "/*[COMMENT]*/ " |
    replace: " //", "/*[COMMENT]*/ " |
    replace: " //", "/*[COMMENT]*/ " |
    replace: "// ", "/*[COMMENT]*/ " |
    replace: "//<![CDATA[", "/*[COMMENT]*/ " |
    replace: "//]]>", "/*[COMMENT]*/ " |

    %}

    {% assign _script_comment_befores = _script | split: "/*[COMMENT]*/ " %}{% for _script_comment_before in _script_comment_befores %}
    {% assign _script_comment_content = _script_comment_before | split: nl | first %}
    {% if _script_comment_content %}
    {% capture _script_comment %}{{ "/*[COMMENT]*/ " }}{{ _script_comment_content }}{{ nl }}{% endcapture %}
    {% assign _script = _script | remove: _script_comment %}
    {% endif %}
    {% endfor %}
    {% assign _script_comment_befores = _script | split: "/*" %}{% for _script_comment_before in _script_comment_befores %}
    {% assign _script_comment_content = _script_comment_before | split: "*/" | first %}
    {% if _script_comment_content %}
    {% capture _script_comment %}{{ "/*" }}{{ _script_comment_content }}{{ "*/" }}{% endcapture %}
    {% assign _script = _script | remove: _script_comment %}
    {% endif %}
    {% endfor %}

    {% capture _content %}{{ _content }}<script {{ _script }}</script>{{ _scripts.last }}{% endcapture %}
    {% when 1 %}
    {% capture _content %}{{ _content }}{{ _scripts.last }}{% endcapture %}
    {% endcase %}
    {% endfor %}{% comment %}
    Cleanup
    - There is an 'missing semicolon' issue with Shopify's Enhanced Ecommerce for Google Analytics, which gets corrected here as well.
    {% endcomment %}{% assign _content = _content | replace: '<script >', '<script>' | replace: '<script ', '<script ' | replace: 'analytics.async = true;', ';analytics.async = true;' %}{% comment %}
    Compress whitespace outside of <pre>
    - We have to ensure a space after every </pre> otherwise Liquid won't match, and blow up everything up if two elements comes straight after eachother: <pre></pre><div></div>
    {% endcomment %}{% assign _content = _content | replace: '</pre>', '</pre><!-- [SPACE] -->' | replace: '<!-- [SPACE] -->', " " %}{% assign _pre_befores = _content | split: "<pre" %}{% assign _content = "" %}{% for _pre_before in _pre_befores %}
    {% assign _pres = _pre_before | split: "</pre>" %}
    {% case _pres.size %}
    {% when 2 %}
    {% capture _content %}{{ _content }}<pre{{ _pres.first }}</pre>{{ _pres.last | split: " " | join: " " }}{% endcapture %}
    {% when 1 %}
    {% capture _content %}{{ _content }}{{ _pres.last | split: " " | join: " " }}{% endcapture %}
    {% endcase %}
    {% endfor %}{% comment %}
    Remove html comments
    - Matching just "<!--" will whipe out IE Conditional Comments, which you probable don't want
    {% endcomment %}{% assign _comment_befores = _content | split: "<!-- " %}{% for _comment_before in _comment_befores %}
    {% assign _comment_content = _comment_before | split: " -->" | first %}
    {% if _comment_content %}
    {% capture _comment %}{{ "<!-- " }}{{ _comment_content }}{{ " -->" }}{% endcapture %}
    {% assign _content = _content | remove: _comment %}
    {% endif %}
    {% endfor %}{% comment %}
    Clip whitespace around elements
    {% endcomment %}{% assign _clippings = "html head title base link meta style body article section nav aside h1 h2 h3 h4 h5 h6 hgroup header footer address p hr blockquote ol ul li dl dt dd figure figcaption main div table caption colgroup col tbody thead tfoot tr td th option img script" | split: " " %}{% for _element in _clippings %}
    {% assign _edges = " <e;<e; </e>;</e>;</e> ;</e>" | replace: "e", _element | split: ";" %}
    {% assign _content = _content | replace: _edges[0], _edges[1] | replace: _edges[2], _edges[3] | replace: _edges[4], _edges[5] %}
    {% endfor %}{% comment %}
    Output
    {% endcomment %}{{ _content }}