forked from cotes2020/jekyll-theme-chirpy
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathrefactor-content.html
269 lines (213 loc) · 8.96 KB
/
refactor-content.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<!-- Refactor the HTML structure -->
{% assign _content = include.content %}
<!--
In order to allow a wide table to scroll horizontally,
we suround the markdown table with `<div class="table-wrapper">` and `</div>`
-->
{% if _content contains '<table' %}
{% assign _content = _content
| replace: '<table', '<div class="table-wrapper"><table'
| replace: '</table>', '</table></div>'
| replace: '<code><div class="table-wrapper">', '<code>'
| replace: '</table></div></code>', '</table></code>'
%}
{% endif %}
<!--
Fixed kramdown code highlight rendering:
https://github.com/penibelst/jekyll-compress-html/issues/101
https://github.com/penibelst/jekyll-compress-html/issues/71#issuecomment-188144901
-->
{% if _content contains '<pre class="highlight">' %}
{% assign _content = _content
| replace: '<div class="highlight"><pre class="highlight"><code', '<div class="highlight"><code'
| replace: '</code></pre></div>', '</code></div>'
%}
{% endif %}
<!-- Change the icon of checkbox -->
{% if _content contains '<input type="checkbox"' %}
{% assign _content = _content
| replace: '<input type="checkbox" class="task-list-item-checkbox" disabled="disabled" checked="checked" />',
'<i class="iconfont icon-iccheckcircle checked"></i>'
| replace: '<input type="checkbox" class="task-list-item-checkbox" disabled="disabled" />',
'<i class="iconfont icon-yuan_fuzhi"></i>'
%}
{% endif %}
<!-- Handle images -->
{% assign IMG_TAG = '<img ' %}
{% if _content contains IMG_TAG %}
{% assign _img_content = null %}
{% assign _img_snippets = _content | split: IMG_TAG %}
{% for _img_snippet in _img_snippets %}
{% if forloop.first %}
{% assign _img_content = _img_snippet %}
{% continue %}
{% endif %}
{% assign _left = _img_snippet | split: '>' | first %}
{% assign _right = _img_snippet | remove: _left %}
{% unless _left contains 'src=' %}
{% continue %}
{% endunless %}
{% assign _left = _left | remove: ' /' | replace: ' w=', ' width=' | replace: ' h=', ' height=' %}
{% assign _attrs = _left | split: '" ' %}
{% assign _src = null %}
{% assign _lqip = null %}
{% assign _class = null %}
{% for _attr in _attrs %}
{% unless _attr contains '=' %}
{% continue %}
{% endunless %}
{% assign _pair = _attr | split: '="' %}
{% capture _key %}{{ _pair | first }}{% endcapture %}
{% capture _value %}{{ _pair | last | remove: '"' }}{% endcapture %}
{% case _key %}
{% when 'src' %}
{% assign _src = _value %}
{% when 'lqip' %}
{% assign _lqip = _value %}
{% when 'class' %}
{% assign _class = _value %}
{% endcase %}
{% endfor %}
<!-- take out classes -->
{% if _class %}
{% capture _old_class %}class="{{ _class }}"{% endcapture %}
{% assign _left = _left | remove: _old_class %}
{% endif %}
{% assign _final_src = null %}
{% assign _lazyload = true %}
{%- capture _img_url -%}
{% include media-url.html src=_src subpath=page.media_subpath %}
{%- endcapture -%}
{% assign _path_prefix = _img_url | remove: _src %}
{% unless _src contains '//' %}
{% assign _final_src = _path_prefix | append: _src %}
{% assign _src_alt = 'src="' | append: _path_prefix %}
{% assign _left = _left | replace: 'src="', _src_alt %}
{% endunless %}
{% if _lqip %}
{% assign _lazyload = false %}
{% assign _class = _class | append: ' blur' %}
{% unless _lqip contains 'data:' %}
{% assign _lqip_alt = 'lqip="' | append: _path_prefix %}
{% assign _left = _left | replace: 'lqip="', _lqip_alt %}
{% endunless %}
<!-- add image placeholder -->
{% assign _left = _left | replace: 'src=', 'data-src=' | replace: ' lqip=', ' data-lqip="true" src=' %}
{% else %}
{% assign _class = _class | append: ' shimmer' %}
{% endif %}
<!-- lazy-load images -->
{% if _lazyload %}
{% assign _left = _left | append: ' loading="lazy"' %}
{% endif %}
{% if page.layout == 'home' %}
<!-- create the image wrapper -->
{% assign _wrapper_start = '<div class="preview-img ' | append: _class | append: '">' %}
{% assign _img_content = _img_content | append: _wrapper_start %}
{% assign _right = _right | prepend: '></div' %}
{% else %}
<!-- make sure the `<img>` is wrapped by `<a>` -->
{% assign _parent = _right | slice: 1, 4 %}
{% if _parent == '</a>' %}
<!-- add class to exist <a> tag -->
{% assign _size = _img_content | size | minus: 1 %}
{% capture _class %}
class="img-link{% unless _lqip %} shimmer{% endunless %}"
{% endcapture %}
{% assign _img_content = _img_content | slice: 0, _size | append: _class | append: '>' %}
{% else %}
<!-- create the image wrapper -->
{% assign _wrapper_start = _final_src
| default: _src
| prepend: '<a href="'
| append: '" class="popup img-link '
| append: _class
| append: '">'
%}
{% assign _img_content = _img_content | append: _wrapper_start %}
{% assign _right = '></a' | append: _right %}
{% endif %}
{% endif %}
<!-- combine -->
{% assign _img_content = _img_content | append: IMG_TAG | append: _left | append: _right %}
{% endfor %}
{% if _img_content %}
{% assign _content = _img_content %}
{% endif %}
{% endif %}
<!-- Add header for code snippets -->
{% if _content contains '<div class="highlight"><code>' %}
{% assign _code_spippets = _content | split: '<div class="highlight"><code>' %}
{% assign _new_content = '' %}
{% for _snippet in _code_spippets %}
{% if forloop.last %}
{% assign _new_content = _new_content | append: _snippet %}
{% else %}
{% assign _left = _snippet | split: '><' | last %}
{% if _left contains 'file="' %}
{% assign _label_text = _left | split: 'file="' | last | split: '"' | first %}
{% assign _label_icon = 'iconfont icon-file-code' %}
{% else %}
{% assign _lang = _left | split: 'language-' | last | split: ' ' | first %}
{% capture _label_text %}{% include language-alias.html language=_lang %}{% endcapture %}
{% assign _label_icon = 'iconfont icon-code small' %}
{% endif %}
{% capture _label %}
<span data-label-text="{{ _label_text | strip }}"><i class="{{ _label_icon }}"></i></span>
{% endcapture %}
{% if _left contains 'run="' %}
{% assign _code = _left | split: 'run="' | last | split: '"' | first %}
{% capture _play_button %}
<button class="button-run-code button-run-{{ _code }}" aria-label="run"
output-title="{{ site.data.locales[site.lang].post.button.run.output }}"
wait-message="{{ site.data.locales[site.lang].post.button.run.wait }}"
error-prompt="{{ site.data.locales[site.lang].post.button.run.error_prompt }}">
<i class="iconfont icon-playfill"></i>
</button>
{% endcapture %}
{% else %}
{% assign _play_button = '' %}
{% endif %}
{% assign _new_content = _new_content | append: _snippet
| append: '<div class="code-header">'
| append: _label
| append: _play_button
| append: '<button class="button-copy" aria-label="copy" data-title-succeed="'
| append: site.data.locales[include.lang].post.button.copy_code.succeed
| append: '"><i class="iconfont icon-copy"></i></button></div>'
| append: '<div class="highlight"><code>'
%}
{% endif %}
{% endfor %}
{% assign _content = _new_content %}
{% endif %}
<!-- Create heading anchors -->
{% assign heading_levels = '2,3,4,5' | split: ',' %}
{% assign _heading_content = _content %}
{% for level in heading_levels %}
{% assign mark_start = '<h' | append: level | append: ' id="' %}
{% assign mark_end = '</h' | append: level | append: '>' %}
{% if _heading_content contains mark_start %}
{% assign _new_content = null %}
{% assign heading_snippets = _heading_content | split: mark_start %}
{% for snippet in heading_snippets %}
{% if forloop.first %}
{% assign _new_content = snippet %}
{% continue %}
{% endif %}
{% assign id = snippet | split: '"' | first %}
{% assign anchor = '<a href="#'
| append: id
| append: '" class="anchor text-muted"><i class="iconfont icon-hashtag"></i></a>'
%}
{% assign left = snippet | split: mark_end | first %}
{% assign right = snippet | slice: left.size, snippet.size %}
{% assign left = left | replace_first: '">', '"><span class="me-2">' | append: '</span>' %}
{% assign _new_content = _new_content | append: mark_start | append: left | append: anchor | append: right %}
{% endfor %}
{% assign _heading_content = _new_content %}
{% endif %}
{% endfor %}
{% assign _content = _heading_content %}
<!-- return -->
{{ _content }}