Skip to content

Instantly share code, notes, and snippets.

@VuongNQ
Last active November 27, 2024 06:37
Show Gist options
  • Save VuongNQ/89018bf149895fa1364fb8ca05043770 to your computer and use it in GitHub Desktop.
Save VuongNQ/89018bf149895fa1364fb8ca05043770 to your computer and use it in GitHub Desktop.
Minify HTML snippet template shopify
{% capture breakLine %}
{% endcapture %}
{% liquid
if content_for_header contains 'Shopify.designMode'
echo content
else
assign listSpace = ' , , , , , , , , ' | split: ','
assign clearCommentTag = ''
assign listCommentTagsAfter = content | split: '<!--'
for afterItem in listCommentTagsAfter
if afterItem contains '-->'
assign listAfterRemove = afterItem | split: '-->'
assign checkLength = listAfterRemove | size
if checkLength > 1
assign afterEndContent = listAfterRemove | last
assign clearCommentTag = clearCommentTag | append: afterEndContent
endif
else
if forloop.first == true
assign clearCommentTag = clearCommentTag | append: afterItem
endif
endif
endfor
assign clearCommentCss = ''
assign listCommentCssAfter = clearCommentTag | split: '/*'
for afterItem in listCommentCssAfter
assign afterContent = afterItem | split: '*/' | last
assign clearCommentCss = clearCommentCss | append: afterContent
endfor
assign structureHTML = clearCommentCss | split: '</head>'
assign cleanHTML = ''
assign headContent = structureHTML | first
assign cleanHead = ''
assign cleanBody = ''
assign headRaw = headContent | prepend: '!![POSITION-SCRIPT]!!'
assign listScriptTagAfter = headRaw | split: '<script'
for afterItem in listScriptTagAfter
assign contentClean = ''
if forloop.first == true and afterItem contains '!![POSITION-SCRIPT]!!'
assign contentClean = contentClean | append: afterItem | replace: '!![POSITION-SCRIPT]!!', '' | strip_newlines | strip | replace: '> ', '>' | replace: ' <', '<'
for space in listSpace
assign contentClean = contentClean | replace: space, ' '
endfor
elsif afterItem contains '</script>'
assign listContentClearScript = afterItem | split: '</script>'
if listContentClearScript.size > 1
assign contentScript = listContentClearScript | first
assign contentClean = contentScript | prepend: '<script' | append: '</script>'
assign contentAfterScript = listContentClearScript | last
assign contentAfterScriptClean = contentAfterScript | strip_newlines | strip
for space in listSpace
assign contentAfterScriptClean = contentAfterScriptClean | replace: space, ' '
endfor
assign clearStyleTag = ''
assign cleanStyleTag = contentAfterScriptClean | prepend: '!![POSITION-STYLE]!!'
assign listStyleTagAfter = cleanStyleTag | split: '<style'
for styleAfter in listStyleTagAfter
assign styleClean = ''
if forloop.first == true and styleAfter contains '!![POSITION-STYLE]!!'
assign styleClean = styleClean | append: styleAfter | replace: '!![POSITION-STYLE]!!', '' | strip_newlines | strip | replace: '> ', '>' | replace: ' <', '<'
elsif styleAfter contains '</style>'
assign listContentClearStyle = styleAfter | split: '</style>'
if listContentClearStyle.size > 1
assign contentStyle = listContentClearStyle | first
assign styleClean = contentStyle | prepend: '<style' | append: '</style>' | strip_newlines | strip | replace: '{ ', '{' | replace: '{ ', '{' | replace: ' }', '}' | replace: ' }', '}'
assign contentAfterStyle = listContentClearStyle | last
assign contentAfterStyleClean = contentAfterStyle | strip_newlines | strip | replace: '> ', '>' | replace: ' <', '<'
assign styleClean = styleClean | append: contentAfterStyleClean
else
assign contentStyle = listContentClearStyle | last | remove: breakLine
assign styleClean = contentStyle | prepend: '<style' | append: '</style>' | strip_newlines | strip | replace: '{ ', '{' | replace: '{ ', '{' | replace: ' }', '}' | replace: ' }', '}'
endif
endif
assign clearStyleTag = clearStyleTag | append: styleClean
endfor
assign contentClean = contentClean | append: clearStyleTag
else
assign contentScript = listContentClearScript | last
assign contentClean = contentScript | prepend: '<script' | append: '</script>'
endif
endif
assign cleanHead = cleanHead | append: contentClean
endfor
assign bodyContent = structureHTML | last
assign bodyRaw = bodyContent | prepend: '!![POSITION-SCRIPT]!!'
assign listScriptTagAfter = bodyRaw | split: '<script'
for afterItem in listScriptTagAfter
assign contentClean = ''
if forloop.first == true and afterItem contains '!![POSITION-SCRIPT]!!'
assign contentClean = contentClean | append: afterItem | replace: '!![POSITION-SCRIPT]!!', '' | strip_newlines | strip
for space in listSpace
assign contentClean = contentClean | replace: space, ' '
endfor
elsif afterItem contains '</script>'
assign listContentClearScript = afterItem | split: '</script>'
if listContentClearScript.size > 1
assign contentScript = listContentClearScript | first
assign contentClean = contentScript | prepend: '<script' | append: '</script>'
assign contentAfterScript = listContentClearScript | last
assign contentAfterScriptClean = contentAfterScript | strip_newlines | strip
for space in listSpace
assign contentAfterScriptClean = contentAfterScriptClean | replace: space, ' '
endfor
assign contentClean = contentClean | append: contentAfterScriptClean | replace: '> ', '>'
else
assign contentScript = listContentClearScript | last
assign contentClean = contentScript | prepend: '<script' | append: '</script>'
endif
endif
assign cleanBody = cleanBody | append: contentClean
endfor
if cleanHead != blank and cleanBody != blank
assign cleanHTML = cleanHTML | prepend: cleanHead | append: '</head>' | append: cleanBody
assign cleanV1 = '' | prepend: cleanHead | append: '</head>' | append: bodyContent
echo cleanV1
else
echo content
endif
endif
%}
{% comment %}
source https://gist.github.com/VuongNQ/89018bf149895fa1364fb8ca05043770/edit
using function by wrapper all html like example below
{%- capture minified -%}
{% endcapture %}
{%- render 'swift_compress', content: minified -%}
{% endcomment %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment