jekyll-hash-file 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dbdfaa0a4c60cff8f5a30edf36edcd389ea83027cbbd51ef8b589c87c8bc75e1
4
+ data.tar.gz: c7210ac987887948c115942d46584e47d211cf9ccb99d71ea1269d3879a6b9ac
5
+ SHA512:
6
+ metadata.gz: dc7b46eff4d9e71519eaf6c46706b13c8186e873bf64d9d751b4f7db301ecf434e4fac8325d14cada97626b0b339cc8e6a05ae76a8e815c1682353fcb57b88c7
7
+ data.tar.gz: 751ff9ea0654477a8ab61cfe20f51a92fefea8e1b18f589017de8e4dc91a8428e193102e576dddcb06b790dea6fa64e8bc5064918110ef1515a06197c8fded96
@@ -0,0 +1,84 @@
1
+ require "digest"
2
+
3
+ module Jekyll
4
+ module HashFile
5
+ CACHE_DIR = "cache/hashed-files/"
6
+ HASH_LENGTH = 32
7
+
8
+ # Defines the hashes file name.
9
+ # @param absolute_path_source [String] Full path to the source file
10
+ # @return [String]
11
+ def _compute_filename(absolute_path_source)
12
+ hash = Digest::SHA256.file(absolute_path_source)
13
+ short_hash = hash.hexdigest()[0, HASH_LENGTH]
14
+ extension = File.extname(absolute_path_source)
15
+
16
+ "#{short_hash}#{extension}"
17
+ end
18
+
19
+ # Compute all necessary paths for the plugin.
20
+ # @param absolute_path_site [String] As an example: "C:/jekyll/jekyll-hash-file"
21
+ # @param relative_path_source [String] As an example: "/assets/image-a.png" or "/assets/style/main.css"
22
+ # @return [Array(String, String, String, String, String)]
23
+ # * [String] absolute_path_source - Full path to the source file
24
+ # * [String] absolute_path_destination - Full path to the destination file
25
+ # * [String] absolute_path_cache - Full path to the cache directory
26
+ # * [String] file_name_destination - Name of the destination file
27
+ # * [String] relative_path_destination - Relative path to the destination file
28
+ def _compute_paths(absolute_path_site, relative_path_source)
29
+ absolute_path_source = File.join(absolute_path_site, relative_path_source)
30
+ raise "No file found at #{absolute_path_source}" unless File.readable?(absolute_path_source)
31
+
32
+ file_name_destination = _compute_filename(absolute_path_source)
33
+
34
+ absolute_path_cache = File.join(absolute_path_site, CACHE_DIR)
35
+ absolute_path_destination = File.join(absolute_path_cache, file_name_destination)
36
+ relative_path_destination = File.join(CACHE_DIR, file_name_destination)
37
+
38
+ [absolute_path_source, absolute_path_destination, absolute_path_cache, file_name_destination, relative_path_destination]
39
+ end
40
+
41
+ # Determine whether the file exists in the cache.
42
+ # @param absolute_path_source [String] As an example: "C:/jekyll/jekyll-hash-file"
43
+ # @param absolute_path_destination [String] As an example: "/assets/image-a.png" or "/assets/style/main.css"
44
+ # @return [Boolean] If true, the file exists in the cache.
45
+ def _in_cache? (absolute_path_source, absolute_path_destination)
46
+ File.exist?(absolute_path_destination)
47
+ end
48
+
49
+ # Determine whether the file in the cache is still valid. It does so by comparing the modification times of the files.
50
+ # @param absolute_path_source [String] As an example: "C:/jekyll/jekyll-hash-file"
51
+ # @param absolute_path_destination [String] As an example: "/assets/image-a.png" or "/assets/style/main.css"
52
+ # @return [Boolean] If true, the file should be skipped.
53
+ def _valid_cache? (absolute_path_source, absolute_path_destination)
54
+ File.mtime(absolute_path_destination) >= File.mtime(absolute_path_source)
55
+ end
56
+
57
+ # Creates a copy of the source file with a hashed name to for cache busting. Processed files are cached to speed up builds.
58
+ # @param relative_source_path [String] As an example: "/assets/image-a.png" or "/assets/style/main.css"
59
+ # @return [String] As an example: "f69a4d50f20bb781f908db2b2b2c7739.js"
60
+ def hash_file(relative_source_path)
61
+
62
+ site = @context.registers[:site]
63
+
64
+ absolute_path_source, absolute_path_destination, absolute_path_cache, file_name_destination, relative_path_destination = _compute_paths(site.source, relative_source_path)
65
+
66
+ # Create the cache directory
67
+ FileUtils.mkdir_p(absolute_path_cache)
68
+
69
+ if _in_cache?(absolute_path_source, absolute_path_destination) && _valid_cache?(absolute_path_source, absolute_path_destination)
70
+ # if the file is cached and valid we can just return the relative path
71
+ return relative_path_destination
72
+ else
73
+ # otherwise, we process the file and add it to the cache
74
+ puts "Hashing '#{relative_source_path}' to '#{relative_path_destination}'"
75
+ FileUtils.cp(absolute_path_source, absolute_path_destination)
76
+ site.static_files << Jekyll::StaticFile.new(site, site.source, CACHE_DIR, file_name_destination)
77
+ end
78
+
79
+ relative_path_destination
80
+ end
81
+ end
82
+ end
83
+
84
+ Liquid::Template.register_filter(Jekyll::HashFile)
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module HashFile
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require "jekyll/hash-file.rb"
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-hash-file
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Willem 'Jip' Wijnia
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-12-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.3'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.3'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ description: ''
34
+ email:
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - lib/jekyll-hash-file.rb
40
+ - lib/jekyll-hash-file/version.rb
41
+ - lib/jekyll/hash-file.rb
42
+ homepage: https://github.com/Garanas/jekyll-hash-file
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.5.22
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Liquid filter to hash file references to bust the browser cache in Jekyll
65
+ 3 and 4
66
+ test_files: []