Skip to content

Commit 72b68a9

Browse files
committed
Preserve the file name as a sub-directory
This makes it easier to see where images originate from.
1 parent e0f7754 commit 72b68a9

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

lib/jekyll/transcode-image-filters.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33

44
module Jekyll
55
module TranscodeImageFilters
6+
7+
# Computes a sanitized name for use as a directory.
8+
# @param name The (file) name to sanitize.
9+
# @return [String]
10+
def _sanitize_name(name)
11+
# Replace invalid characters with `_` (Windows and Unix-safe)
12+
name.gsub(/[<>:"\/\\|?*\n\r]/, '_').strip
13+
end
14+
615
# Computes the name of the file in the cache.
716
# @param absolute_path_source [String] Full path to the source file
817
# @param resolution [String] As an example: 900x900
@@ -22,7 +31,7 @@ def _compute_cache_filename(absolute_path_source, resolution, format)
2231
# Compute all necessary paths for the plugin.
2332
# @param absolute_path_site [String] As an example: "C:/jekyll/jekyll-transcode-image-filters"
2433
# @param relative_path_source [String] As an example: "/assets/image-a.png" or "/assets/style/main.css"
25-
# @param cache_dir [String] Directory of the cache folder, as an example: "cache/bmp/"
34+
# @param relative_cache_dir [String] Directory of the cache folder, as an example: "cache/bmp/"
2635
# @param resolution [String] As an example: 900x900
2736
# @param format [String] As an example: webp or jpg
2837
# @return [Array(String, String, String, String, String)]
@@ -31,15 +40,17 @@ def _compute_cache_filename(absolute_path_source, resolution, format)
3140
# * [String] absolute_path_cache - Full path to the cache directory
3241
# * [String] file_name_destination - Name of the destination file
3342
# * [String] relative_path_destination - Relative path to the destination file
34-
def _compute_paths(absolute_path_site, relative_path_source, cache_dir, resolution, format)
43+
def _compute_paths(absolute_path_site, relative_path_source, relative_cache_dir, resolution, format)
3544
absolute_path_source = File.join(absolute_path_site, relative_path_source)
3645
raise "No file found at #{absolute_path_source}" unless File.readable?(absolute_path_source)
3746

47+
file_name_source = _sanitize_name(File.basename(relative_path_source, ".*"))
48+
file_name_source_sanitized = _sanitize_name(file_name_source)
3849
file_name_destination = _compute_cache_filename(absolute_path_source, resolution, format)
3950

40-
absolute_path_cache = File.join(absolute_path_site, cache_dir)
41-
absolute_path_destination = File.join(absolute_path_cache, file_name_destination)
42-
relative_path_destination = File.join(cache_dir, file_name_destination)
51+
absolute_path_cache = File.join(absolute_path_site, relative_cache_dir, file_name_source_sanitized)
52+
absolute_path_destination = File.join(absolute_path_cache, file_name_source_sanitized, file_name_destination)
53+
relative_path_destination = File.join(relative_cache_dir, file_name_source_sanitized, file_name_destination)
4354

4455
[absolute_path_source, absolute_path_destination, absolute_path_cache, file_name_destination, relative_path_destination]
4556
end

0 commit comments

Comments
 (0)