-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathkramdown_enhancer.rb
More file actions
173 lines (152 loc) · 5.02 KB
/
kramdown_enhancer.rb
File metadata and controls
173 lines (152 loc) · 5.02 KB
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
begin
require "webp-ffi"
rescue LoadError; end
module KramdownEnhancer
class << self
def webp
@webp ||= {}
end
def file
@file ||= {}
end
def baseurl
@baseurl
end
def baseurl=(input)
@baseurl =
if input.is_a?(String) && !input.empty?
str = input.start_with?("/") ? input : "/#{input}"
str.chomp("/")
else
""
end
end
def blockquote_types
@blockquote_types ||= {
:note => "notice--info",
:tip => "notice--success",
:important => "notice--primary",
:warning => "notice--warning",
:caution => "notice--danger",
}
end
end
class WebpFile < Jekyll::StaticFile
def write(dest)
true
end
end
module Html
def convert_a(el, indent)
if el.attr["href"].is_a?(String) && !el.options[:relative]
el.attr["href"] = relative_url(el.attr["href"])
el.options[:relative] = true
end
super
end
def convert_img(el, indent)
if el.attr["src"].is_a?(String) && !el.options[:relative]
src = el.attr["src"]
el.attr["src"] = relative_url(src)
el.options[:relative] = true
if KramdownEnhancer.webp[src] && !el.options[:webp] && !el.options[:picture]
webp_src = KramdownEnhancer.webp[src]
pic = Kramdown::Element.new(:html_element, "picture")
pic.children << Kramdown::Element.new(:html_element, "source", { "srcset" => relative_url(webp_src), "type" => "image/webp" })
el.options[:picture] = true
el.options[:webp] = true
pic.children << el
return convert_html_element(pic, indent)
end
end
super
end
def convert_blockquote(el, indent)
p = el.children.first
return super if p&.type != :p || p.children.empty?
first = p.children.first
return super unless first&.type == :text
text = first.value.downcase
KramdownEnhancer.blockquote_types.each do |type, class_name|
prefix = "[!#{type}]"
prefix_with_newline = "#{prefix}\n"
# case A: <p>[!NOTE]</p>
if text == prefix
el.attr["class"] = [el.attr["class"], class_name].compact.join(" ")
p.children.shift
break
# case B: <p>[!NOTE]\n some text</p>
elsif text.start_with?(prefix_with_newline)
el.attr["class"] = [el.attr["class"], class_name].compact.join(" ")
first.value = first.value[prefix_with_newline.length..-1] || ""
break
end
end
super
end
def convert_html_element(el, indent)
unless el.options[:relative]
if el.value == "a" && el.attr["href"].is_a?(String)
el.attr["href"] = relative_url(el.attr["href"])
el.options[:relative] = true
elsif el.value == "img" && el.attr["src"].is_a?(String)
src = el.attr["src"]
el.attr["src"] = relative_url(el.attr["src"])
el.options[:relative] = true
if KramdownEnhancer.webp[src] && !el.options[:webp] && !el.options[:picture]
webp_src = KramdownEnhancer.webp[src]
pic = Kramdown::Element.new(:html_element, "picture")
pic.children << Kramdown::Element.new(:html_element, "source", { "srcset" => relative_url(webp_src), "type" => "image/webp" })
el.options[:webp] = true
pic.children << el
return convert_html_element(pic, indent)
end
elsif el.value == "picture"
el.children.each do |child|
child.options[:picture] = true
end
end
end
super(el, indent)
end
private
def relative_url(input)
if input.is_a?(String) && input.start_with?("/")
input = input.start_with?("/") ? input : "/#{input}"
uri = Addressable::URI.parse(input)
if uri
if uri.path.length > 1
file = KramdownEnhancer.file[uri.path[1..]]
uri.path = file.url if file
end
uri.path = "#{KramdownEnhancer.baseurl}#{uri.path}"
return uri.to_s
end
end
input
end
end
end
Jekyll::Hooks.register :site, :post_read do |site|
KramdownEnhancer.baseurl = site.config["baseurl"]
webp_list = []
webp_enabled = defined?(WebP)
site.each_site_file do |file|
KramdownEnhancer.file[file.relative_path] = file
if file.is_a?(Jekyll::StaticFile)
url = "#{file.url}.webp"
source = "#{file.path}.webp"
destination = File.join(site.dest, url)
if File.exist?(source)
KramdownEnhancer.webp[file.url] = url
elsif webp_enabled && %w[.png .jpg .jpeg .tif .tiff].include?(file.extname.downcase)
FileUtils.mkdir_p(File.dirname(destination))
WebP.encode(file.path, destination)
webp_list.push(KramdownEnhancer::WebpFile.new(site, site.dest, File.dirname(url), File.basename(url)))
KramdownEnhancer.webp[file.url] = url
end
end
end
site.static_files.concat(webp_list)
Kramdown::Converter::Html.prepend(KramdownEnhancer::Html)
end