From bffaf6374f265cec96ef743d42b46fbec3b59797 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Sun, 11 Dec 2022 07:09:40 +0800 Subject: [PATCH] feat: support LQIP for images --- _includes/refactor-content.html | 112 +++++++++++++++++------------- _layouts/post.html | 16 +++-- _sass/addon/commons.scss | 35 ++++++---- _sass/colors/dark-typography.scss | 6 +- _sass/layout/post.scss | 2 +- 5 files changed, 100 insertions(+), 71 deletions(-) diff --git a/_includes/refactor-content.html b/_includes/refactor-content.html index 4425712..b3a1523 100644 --- a/_includes/refactor-content.html +++ b/_includes/refactor-content.html @@ -51,23 +51,43 @@ {% assign _img_content = nil %} {% assign _img_snippets = _content | split: IMG_TAG %} + + {% if site.img_cdn %} + {% if site.img_cdn contains '//' %} + {% assign _path_prefix = site.img_cdn %} + {% else %} + {% assign _path_prefix = site.img_cdn | relative_url %} + {% endif %} + {% else %} + {% assign _path_prefix = site.baseurl %} + {% endif %} + + + {% if page.img_path %} + {% assign _path = page.img_path | append: '/' | replace: '//', '/' %} + {% assign _path_prefix = _path_prefix | append: _path %} + {% endif %} + {% for _img_snippet in _img_snippets %} {% if forloop.first %} {% assign _img_content = _img_snippet %} {% continue %} {% endif %} - {% assign _width = nil %} - {% assign _height = nil %} - {% assign _src = nil %} - {% assign _left = _img_snippet | split: '>' | first %} {% assign _right = _img_snippet | remove: _left %} - {% assign _left = _left | remove: ' /' %} - {% assign _left = _left | replace: ' w=', ' width=' | replace: ' h=', ' height=' %} + {% unless _left contains 'src=' %} + {% continue %} + {% endunless %} + + {% assign _left = _left | remove: ' /' | replace: ' w=', ' width=' | replace: ' h=', ' height=' %} {% assign _attrs = _left | split: ' ' %} + {% assign _width = nil %} + {% assign _height = nil %} + {% assign _lqip = nil %} + {% for _attr in _attrs %} {% assign _pair = _attr | split: '="' %} {% if _pair.size < 2 %} @@ -75,7 +95,7 @@ {% endif %} {% capture _key %}{{ _pair | first }}{% endcapture %} - {% capture _value %}{{ _pair | last | replace: '"', '' }}{% endcapture %} + {% capture _value %}{{ _pair | last | remove: '"' }}{% endcapture %} {% case _key %} {% when 'width' %} @@ -84,67 +104,61 @@ {% assign _height = _value %} {% when 'src' %} {% assign _src = _value %} + {% when 'lqip' %} + {% assign _lqip = _value %} {% endcase %} - {% if _width and _height and _src %} - {% break %} - {% endif %} {% endfor %} - {% if _src %} - {% unless _src contains '://' %} - - {% if site.img_cdn %} - {% if site.img_cdn contains '//' %} - {% assign _src_prefix = site.img_cdn %} - {% else %} - {% assign _src_prefix = site.img_cdn | relative_url %} - {% endif %} - {% else %} - {% assign _src_prefix = site.baseurl %} - {% endif %} - - - {% if page.img_path %} - {% assign _path = page.img_path | append: '/' | replace: '//', '/' %} - {% assign _src_prefix = _src_prefix | append: _path %} - {% endif %} - - {% assign _final_src = _src_prefix | append: _src %} - {% assign _left = _left | replace: _src, _final_src %} + {% unless _src contains '//' %} + {% assign _final_src = _path_prefix | append: _src %} + {% capture _src_from %}"{{ _src }}"{% endcapture %} + {% capture _src_to %}"{{ _final_src }}"{% endcapture %} + {% assign _left = _left | replace: _src_from, _src_to %} + {% endunless %} + {% if _lqip %} + {% unless _lqip contains ':' %} + {% assign _final_lqip = _path_prefix | append: _lqip %} + {% capture _lqip_from %}"{{ _lqip }}"{% endcapture %} + {% capture _lqip_to %}"{{ _final_lqip }}"{% endcapture %} + {% assign _left = _left | replace: _lqip_from, _lqip_to %} {% endunless %} - - - - {% if _left contains 'class=' %} - {% assign _left = _left | replace: 'class="', 'class="lazyload ' %} - {% else %} - {% assign _left = _left | replace: 'src=', 'class="lazyload" src=' %} - {% endif %} - - {% assign _left = _left | replace: 'src=', 'data-src=' %} - {% endif %} - + + {% if _left contains 'class=' %} + {% assign _left = _left | replace: 'class="', 'class="lazyload ' %} + {% else %} + {% assign _left = _left | replace: 'src=', 'class="lazyload" src=' %} + {% endif %} - {% if _width and _height %} - {%- capture _svg -%} - src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 {{ _width }} {{ _height }}'%3E%3C/svg%3E" - {%- endcapture -%} + {% assign _left = _left | replace: 'src=', 'data-src=' %} - {% assign _left = _svg | append: ' ' | append: _left %} + + {% if _lqip %} + {% assign _left = _left | replace: ' lqip=', ' data-lqip="true" src=' %} + {% else %} + {% if _width and _height %} + + {%- capture _svg -%} + src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 {{ _width }} {{ _height }}'%3E%3C/svg%3E" + {%- endcapture -%} + {% assign _left = _svg | append: ' ' | append: _left %} + {% endif %} {% endif %} {% assign _left = _left | append: ' data-proofer-ignore' %} + {% assign _img_content = _img_content | append: IMG_TAG | append: _left | append: _right %} {% endfor %} - {% assign _content = _img_content %} + {% if _img_content %} + {% assign _content = _img_content %} + {% endif %} {% endif %} diff --git a/_layouts/post.html b/_layouts/post.html index 18b0503..3a32921 100644 --- a/_layouts/post.html +++ b/_layouts/post.html @@ -27,12 +27,9 @@ tail_includes: {% endif %} {% if page.image %} - {% capture bg %} - {% unless page.image.no_bg %}{{ 'bg' }}{% endunless %} - {% endcapture %} -