-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjekyll-bustify.rb
72 lines (53 loc) · 2.38 KB
/
jekyll-bustify.rb
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
require 'digest/md5'
module Jekyll
module Bustify
DEFAULT_LENGTH = 5
def rename_string_file_extension(source, extension)
dirname = File.dirname(source)
basename = File.basename(source, ".*")
extname = File.extname(source)
if extname == ""
if basename[0,1]=="."
target = dirname + "/." + extension
else
target = source + "." + extension
end
else
target = dirname + "/" + basename + "." + extension
end
target
end
def bustify(input, length=DEFAULT_LENGTH)
file_path = File.join(File.dirname(__FILE__), '..', '_site', input).strip
# the right way would be to use this line below instead, but my lack of knowleadge make me do a hack.
#file_path = File.join(File.dirname(__FILE__), '..', input).strip
# The hack, of reading the asset file in the _site directory, leads to having to run `jekyll build` to times to achive the desired hash result when an asset is modified :// (not always needed, only when a modification is made on an asset dependency of other asset, such as sass dependency file that is called as an import on other sass file)
asset_file_extension = File.extname(file_path)
file_path_of__site = file_path
output_path = input
# if is a SASS CSS file, transform to css (because of the hack of using the _site directory :/ )
if [".scss", ".sass"].include? asset_file_extension
file_path_of__site = rename_string_file_extension(file_path, "css")
output_path = rename_string_file_extension(input, "css")
end
file_content = File.read(file_path_of__site)
# It would be here where to make a rendering of the jekyll asset (I called it Jekyll processing) but I can't make this implementation work.... :/ I have tried
# site = @context.registers[:site]
# content_parsed = site.find_converter_instance(Jekyll::Converters::Identity).convert(file_content.to_s)
# content_parsed1 = Jekyll::Renderer.new(@site, file_path, @payload)
# content_parsed = content_parsed1.render_document
# file_hash = Digest::MD5.hexdigest content_parsed
file_hash = Digest::MD5.hexdigest file_content
# deal with limits of lenght
if length > 32
length = 32
elsif length < 1
length = DEFAULT_LENGTH
end
output_hash = file_hash[-length..-1]
# print cache bust link
"#{output_path}?cb=#{output_hash}"
end
end
end
# Liquid::Template.register_filter(Jekyll::Bustify)