|
51 | 51 | {% assign _img_content = nil %} |
52 | 52 | {% assign _img_snippets = _content | split: IMG_TAG %} |
53 | 53 |
|
| 54 | + <!-- CDN URL --> |
| 55 | + {% if site.img_cdn %} |
| 56 | + {% if site.img_cdn contains '//' %} |
| 57 | + {% assign _path_prefix = site.img_cdn %} |
| 58 | + {% else %} |
| 59 | + {% assign _path_prefix = site.img_cdn | relative_url %} |
| 60 | + {% endif %} |
| 61 | + {% else %} |
| 62 | + {% assign _path_prefix = site.baseurl %} |
| 63 | + {% endif %} |
| 64 | + |
| 65 | + <!-- Add image path --> |
| 66 | + {% if page.img_path %} |
| 67 | + {% assign _path = page.img_path | append: '/' | replace: '//', '/' %} |
| 68 | + {% assign _path_prefix = _path_prefix | append: _path %} |
| 69 | + {% endif %} |
| 70 | + |
54 | 71 | {% for _img_snippet in _img_snippets %} |
55 | 72 | {% if forloop.first %} |
56 | 73 | {% assign _img_content = _img_snippet %} |
57 | 74 | {% continue %} |
58 | 75 | {% endif %} |
59 | 76 |
|
60 | | - {% assign _width = nil %} |
61 | | - {% assign _height = nil %} |
62 | | - {% assign _src = nil %} |
63 | | - |
64 | 77 | {% assign _left = _img_snippet | split: '>' | first %} |
65 | 78 | {% assign _right = _img_snippet | remove: _left %} |
66 | 79 |
|
67 | | - {% assign _left = _left | remove: ' /' %} |
68 | | - {% assign _left = _left | replace: ' w=', ' width=' | replace: ' h=', ' height=' %} |
| 80 | + {% unless _left contains 'src=' %} |
| 81 | + {% continue %} |
| 82 | + {% endunless %} |
| 83 | + |
| 84 | + {% assign _left = _left | remove: ' /' | replace: ' w=', ' width=' | replace: ' h=', ' height=' %} |
69 | 85 | {% assign _attrs = _left | split: ' ' %} |
70 | 86 |
|
| 87 | + {% assign _width = nil %} |
| 88 | + {% assign _height = nil %} |
| 89 | + {% assign _lqip = nil %} |
| 90 | + |
71 | 91 | {% for _attr in _attrs %} |
72 | 92 | {% assign _pair = _attr | split: '="' %} |
73 | 93 | {% if _pair.size < 2 %} |
74 | 94 | {% continue %} |
75 | 95 | {% endif %} |
76 | 96 |
|
77 | 97 | {% capture _key %}{{ _pair | first }}{% endcapture %} |
78 | | - {% capture _value %}{{ _pair | last | replace: '"', '' }}{% endcapture %} |
| 98 | + {% capture _value %}{{ _pair | last | remove: '"' }}{% endcapture %} |
79 | 99 |
|
80 | 100 | {% case _key %} |
81 | 101 | {% when 'width' %} |
|
84 | 104 | {% assign _height = _value %} |
85 | 105 | {% when 'src' %} |
86 | 106 | {% assign _src = _value %} |
| 107 | + {% when 'lqip' %} |
| 108 | + {% assign _lqip = _value %} |
87 | 109 | {% endcase %} |
88 | 110 |
|
89 | | - {% if _width and _height and _src %} |
90 | | - {% break %} |
91 | | - {% endif %} |
92 | 111 | {% endfor %} |
93 | 112 |
|
94 | | - {% if _src %} |
95 | | - {% unless _src contains '://' %} |
96 | | - <!-- Add CDN URL --> |
97 | | - {% if site.img_cdn %} |
98 | | - {% if site.img_cdn contains '//' %} |
99 | | - {% assign _src_prefix = site.img_cdn %} |
100 | | - {% else %} |
101 | | - {% assign _src_prefix = site.img_cdn | relative_url %} |
102 | | - {% endif %} |
103 | | - {% else %} |
104 | | - {% assign _src_prefix = site.baseurl %} |
105 | | - {% endif %} |
106 | | - |
107 | | - <!-- Add image path --> |
108 | | - {% if page.img_path %} |
109 | | - {% assign _path = page.img_path | append: '/' | replace: '//', '/' %} |
110 | | - {% assign _src_prefix = _src_prefix | append: _path %} |
111 | | - {% endif %} |
112 | | - |
113 | | - {% assign _final_src = _src_prefix | append: _src %} |
114 | | - {% assign _left = _left | replace: _src, _final_src %} |
| 113 | + {% unless _src contains '//' %} |
| 114 | + {% assign _final_src = _path_prefix | append: _src %} |
| 115 | + {% capture _src_from %}"{{ _src }}"{% endcapture %} |
| 116 | + {% capture _src_to %}"{{ _final_src }}"{% endcapture %} |
| 117 | + {% assign _left = _left | replace: _src_from, _src_to %} |
| 118 | + {% endunless %} |
115 | 119 |
|
| 120 | + {% if _lqip %} |
| 121 | + {% unless _lqip contains ':' %} |
| 122 | + {% assign _final_lqip = _path_prefix | append: _lqip %} |
| 123 | + {% capture _lqip_from %}"{{ _lqip }}"{% endcapture %} |
| 124 | + {% capture _lqip_to %}"{{ _final_lqip }}"{% endcapture %} |
| 125 | + {% assign _left = _left | replace: _lqip_from, _lqip_to %} |
116 | 126 | {% endunless %} |
117 | | - |
118 | | - <!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> --> |
119 | | - |
120 | | - {% if _left contains 'class=' %} |
121 | | - {% assign _left = _left | replace: 'class="', 'class="lazyload ' %} |
122 | | - {% else %} |
123 | | - {% assign _left = _left | replace: 'src=', 'class="lazyload" src=' %} |
124 | | - {% endif %} |
125 | | -
|
126 | | - {% assign _left = _left | replace: 'src=', 'data-src=' %} |
127 | | -
|
128 | 127 | {% endif %} |
129 | 128 |
|
130 | | - <!-- Add SVG placehoder to prevent layout reflow --> |
| 129 | + <!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> --> |
| 130 | + {% if _left contains 'class=' %} |
| 131 | + {% assign _left = _left | replace: 'class="', 'class="lazyload ' %} |
| 132 | + {% else %} |
| 133 | + {% assign _left = _left | replace: 'src=', 'class="lazyload" src=' %} |
| 134 | + {% endif %} |
131 | 135 |
|
132 | | - {% if _width and _height %} |
133 | | - {%- capture _svg -%} |
134 | | - src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 {{ _width }} {{ _height }}'%3E%3C/svg%3E" |
135 | | - {%- endcapture -%} |
| 136 | + {% assign _left = _left | replace: 'src=', 'data-src=' %} |
136 | 137 |
|
137 | | - {% assign _left = _svg | append: ' ' | append: _left %} |
| 138 | + <!-- add placeholder --> |
| 139 | + {% if _lqip %} |
| 140 | + {% assign _left = _left | replace: ' lqip=', ' data-lqip="true" src=' %} |
| 141 | + {% else %} |
| 142 | + {% if _width and _height %} |
| 143 | + <!-- Add SVG placehoder to prevent layout reflow --> |
| 144 | + {%- capture _svg -%} |
| 145 | + src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 {{ _width }} {{ _height }}'%3E%3C/svg%3E" |
| 146 | + {%- endcapture -%} |
| 147 | + {% assign _left = _svg | append: ' ' | append: _left %} |
| 148 | + {% endif %} |
138 | 149 | {% endif %} |
139 | 150 |
|
140 | 151 | <!-- Bypass the HTML-proofer test --> |
141 | 152 | {% assign _left = _left | append: ' data-proofer-ignore' %} |
142 | 153 |
|
| 154 | + <!-- Combine --> |
143 | 155 | {% assign _img_content = _img_content | append: IMG_TAG | append: _left | append: _right %} |
144 | 156 |
|
145 | 157 | {% endfor %} |
146 | 158 |
|
147 | | - {% assign _content = _img_content %} |
| 159 | + {% if _img_content %} |
| 160 | + {% assign _content = _img_content %} |
| 161 | + {% endif %} |
148 | 162 |
|
149 | 163 | {% endif %} |
150 | 164 |
|
|
0 commit comments